Just trying to be extra careful here...
If I have an app that saved a value in UserDefaults like this in Objective-C:
NSString *newString = textField.text;
[[NSUserDefaults standardUserDefaults] setObject: newString forKey:@"textKey"];
Would this be the proper way of checking whether this value exists when I release an update to the app that is now coded in Swift:
if (UserDefaults.standard.object(forKey: "textKey") as? String) != nil {
print("There is a string")
} else {
print("No string exists")
}
I try to use .string(forKey:) and .bool(forKey:) since they've been introduced, but is it safest since this was saved as an object to pull it out as an object and then test it with "as? String"?
Trickier version of the same question:
An NSMutableArray of NSDictionary objects was saved as an object in UserDefaults
if let oldData = UserDefaults.standard.object(forKey: "theData") as? [NSMutableDictionary] {
}
Will NSDictionary and NSMutableDictionary be interchangeable here?
Thanks!
[NS]UserDefaults
is backed by a plist and a dictionary. The Objective-C string was saved to the plist as a <string>Some Text</string>
.
That same plist loaded in Swift gives a string for that key.
So you should have no issue using UserDefaults.string
to read the value stored with Objective-C. Of course you still need to verify the value actually exists.
if let str = UserDefaults.standard.string(forKey: "textKey") {
print("Found \(str)")
} else {
print("No string for key")
}
Even if the original value isn't a string, using UserDefault.string
is safe. It will simply return nil
for non-string values.
With regard to NSDictionary
and NSMutableDictionary
, note that UserDefaults
only retrieves immutable NSDictionary
. You can't read an NSMutableDictionary
(or array) from UserDefaults
.
Note you can safely use a Swift dictionary to read the original NSDictionary
. No need to use Objective-C classes.