I'm having issues when trying to pass data to my Apple Watch app through NSUserDefaults from my app. Whenever I try to retrieve the array that is stored, I am getting the error 'String' is not identical to 'AnyObject'
.
I've been trying to figure out a solution but I can't work out what the issue is since I am using the same method elsewhere in my app and it works without issue.
Here is what I have in the Apple Watch part:
var defaults = NSUserDefaults(suiteName: "group.AffordIt")
tempNames = defaults?.objectForKey("namesWatch") as NSArray
tempDates = defaults?.objectForKey("datesWatch") as NSArray
tempAmounts = defaults?.objectForKey("valuesWatch") as NSArray
And the containing app part:
defaults?.setObject(names, forKey: "namesWatch")
defaults?.setObject(dates, forKey: "datesWatch")
defaults?.setObject(values, forKey: "valuesWatch")
names, dates and values are String arrays.
Any ideas?
You need to cast the objects you are retrieving from your collection as being Strings:
IMO your problem is, that you try to cast an optional value, e.g.
Probably this SO question is related: Swift NSUserDefaults NSArray using objectForKey.