Comments (6)
Thanks for the report @peterkos!
It's hard to say what's wrong without more context. Is it possible that something else in your app could be modifying the values for these keys? Are you sure there wasn't a corrupted plist involved?
All of the scenarios you described (defaulting bool to false, crashing for non-nil string) are unit tested and (I think) also demonstrated in the Example project in the repo.
Can you reproduce this in the unit tests, example, or in another project outside of your app?
from foil.
(I made a standalone project but am still trying to find the repro case 😅)
Some extra context: this is for a macOS app w/ SwiftData + sandboxing. When I try deleting the .plist
in ~/Library/Containers/[appname]/Data/Library/Preferences/[appname].plist
, I get the following error:
// x50-ish times
Couldn't read values in CFPrefsPlistSource<0x6000014d45a0>
(
Domain: me.peterkos.[appname],
User: kCFPreferencesCurrentUser,
ByHost: No,
Container: (null),
Contents Need Refresh: Yes
):
accessing preferences outside an application's container requires
user-preference-read or file-read-data sandbox access
// warning
Not updating lastKnownShmemState in CFPrefsPlistSource<0x6000029d0090> (Domain: me.peterkos.Cabinette, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null), Contents Need Refresh: Yes): 0 -> 20132
The app only calls into my AppStorage
class in two places (the App.swift
and a preferences window), commenting the other use out results in same crash. This logic is for determining which container to load for SwiftData, maybe it's something with that? (will keep digging)
Edit: It happens regardless of using the standard user defaults, or instantiating my own
from foil.
This is the nil-causing line: it's unable to convert the String
value? Hmm.
from foil.
Aha!
Yes so -- Previously, I changed the type of the didMigrate
var from a Bool
to a String
, which the above fetch<T>
code wasn't able to convert because it was a literal number (from Bool's NSNumber encoding?).
I assumed deleting the prefs .plist
in my container was the only place the key was stored (still unsure on that one) -- and that changing the type of the only variable would overwrite the previous instance, if it had the same name. Maybe that could be a feature? (Or if this is expected behavior, a defined error message vs. force-unwrapping)
The fix was to delete the old Bool-typed key from standard UserDefaults:
> defaults delete [appname] didMigrateFromv_0_2_to_0_3_VersionedSchema
from foil.
@peterkos glad you got it figured out! 💯
When I try deleting the .plist in ~/Library/Containers/[appname]/Data/Library/Preferences/[appname].plist, I get the following error
I'm not certain, but I think you might see some unexpected behavior by doing this. Instead, I would probably uninstall and reinstall the app.
Previously, I changed the type of the didMigrate var from a Bool to a String, which the above fetch code wasn't able to convert because it was a literal number (from Bool's NSNumber encoding?).
Yes, something like this is what I anticipated was happening. 😊
I assumed deleting the prefs .plist in my container was the only place the key was stored (still unsure on that one) -- and that changing the type of the only variable would overwrite the previous instance, if it had the same name. Maybe that could be a feature? (Or if this is expected behavior, a defined error message vs. force-unwrapping)
The .plist
in your container should be the only one. However, my suspicion is that you were probably running your app from Xcode as well? If that's the case, then your app binary and container are in Xcode's DerivedData/
. So then deleting the plist at ~/Library/Containers/[appname]/Data/Library/Preferences/[appname].plist
would not have any effect. The plist in ~/Library/Containers/...
exists for an app that is properly installed on your mac. (So you'd need to export the app archive from Xcode, install it, etc.)
from foil.
Regarding Foil
, there isn't really anything we can do at the library level here, unfortunately. You can misuse UserDefaults
directly in exactly the same way.
In the future, for your debugging purposes, instead of using terminal to run defaults delete ...
, you can use the UserDefaults
API directly in your app, by calling removeObject(forKey:)
. Just call that during app launch before anything else to "reset" UserDefaults
.
from foil.
Related Issues (14)
- Support more types by default. Expand UserDefaultsSerializable to support Int8, Int16, NSArray, NSNumber, etc. HOT 7
- Using UserDefaults Publisher? HOT 2
- Drop CocoaPods support? HOT 3
- Lower deployment target HOT 9
- Deployment targets inconsistency between Pod and SPM metafiles HOT 1
- [Suggestion] Succinct implicit initialization HOT 1
- [Bug]: user defaults returns outdated value after rebuilding project HOT 2
- Prepare and release 3.0
- [Feature]: A demo for SwiftUI project HOT 4
- [Feature]: Expose a `Binding` for SwiftUI if possible HOT 6
- Xcode 14 + infra updates + 4.0 release prep
- [Feature]: Be able to save `Codable` structs (or a workaround?) HOT 6
- Rename "WrappedDefault" and "WrappedDefaultOptional"? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from foil.