taufi
April 11, 2020, 7:09pm
1
Persisting doesn’t work because didSet in Settings.keyworks is never called. Does anyone has an idea what goes wrong?
miibpa
April 13, 2020, 11:36am
2
Apparently the line self.settings.keywords.append(new)
in SettingsView is not triggering the didSet
in the keywords
property inside Settings
model
taufi
April 13, 2020, 11:59am
3
Thanks miibpa,
do you think it’s a Swift bug?
taufi
miibpa
April 13, 2020, 3:27pm
4
Yep It seem to be a bug in Swift 5.2
opened 09:52PM - 27 Jan 20 UTC
closed 03:11AM - 13 Apr 20 UTC
bug
Compiler
PropertyWrappers
| | |
|------------------|-----------------|…
|Previous ID | SR-12089 |
|Radar | rdar://problem/58999150 |
|Original Reporter | lindsey89 (JIRA User) |
|Type | Bug |
|Status | Resolved |
|Resolution | Done |
Attachment: [Download](https://user-images.githubusercontent.com/2727770/164963618-a67d893f-ad5c-47f9-9830-54724f9b69a5.gz)
<details>
<summary>Environment</summary>
Xcode Version 11.2.1 (11B500)
Toolchain: Swift 5.2 Snapshot 2020-01-25 (a)
</details>
<details>
<summary>Additional Detail from JIRA</summary>
| | |
|------------------|-----------------|
|Votes | 3 |
|Component/s | Compiler |
|Labels | Bug, PropertyWrappers |
|Assignee | None |
|Priority | Medium |
md5: 21c46ed73b44e201c5901af9e79428cc
</details>
**is duplicated by**:
* [SR-12350](https://bugs.swift.org/browse/SR-12350) Projected PropertyWrappers don't trigger didSet when their wrappedValue is mutated
* [SR-12407](https://bugs.swift.org/browse/SR-12407) Calling `.toggle` on a wrapped boolean no longer calls didSet
**relates to**:
* [SR-12178](https://bugs.swift.org/browse/SR-12178) willSet not firing when used with a property wrapper
**Issue Description:**
Whilst running the Swift 5.2 toolchain 2020-01-25 I noticed that didSet did not get called correctly on a propertyWrapper backed property.
I've included an example project to illustrate the issue.
**<u>Reproduction steps</u>**
- navigate to contentView.swift
- add a breakpoint to line 24
- run the app in simulator
- add new expense by pressing the + button
- fill in the form
- click save
- notice that the breakpoint isn't called
- this is not the case with included swift in xcode
1 Like
Read on ‘stackoverflow’ that structs like ‘FilterKeywords’ aren’t KVO compatible. The value has to be a subclass of NSObject using @objc dynamic properties. So adding a value in the array won’t trigger ‘willSet/didSet’
My solution was to use a temporary array instead and replace the whole array in the ‘AddKeywordView’ closure which triggered the ‘didSet’ method as expected.
//Reassign array to trigger ‘didSet’
let saveKeywords = self.settings.keywords + [new]
self.settings.keywords = saveKeywords
Another possible way is to use the underlying ‘wrappedValue’ of the binding property, maybe a hack…
self.$settings.keywords.wrappedValue = new
1 Like
@afrank Thank you for sharing your solution - much appreciated!