On Nov 14, 2015, at 10:18 , Alex Hall <[email protected]> wrote:
>
> In a playground, accessing a dictionary of [String:Bool] works fine, as
> expected. In the app, though, I get an error that I can't index [String:Bool]
> with type 'String'. I don't understand why that is, so wanted to see if there
> was anything special about what UserDefaults returns. In the header file it's
> just an optional dictionary, and my 'if let' takes care of that. I'm not sure
> why else my code would error out right there, though. I haven't played much
> with user preferences before, so I may well be missing something, but it
> seems easy enough to use and there shouldn't be anything funny going on. Any
> thoughts? Thanks!
You’ve got the types wrong. ‘dictionaryForKey’ returns type [String:
AnyObject]?, which means — once you’ve stripped the optionality off it — that
you’re trying to assign from AnyObject to Bool. The error message is confusing,
because it seems to be saying that your key isn’t a string, but what it
actually means is that a subscripting operator taking a String key and
returning a Bool doesn’t exist in class [String: AnyObject], and that’s quite
true.
You should probably file a bug report about the crappy error message. To fix
your code you can do something like:
someFilterSwitch.on = filter[someCategory.rawValue] as! Bool
or downcast the dictionary itself to [String: Bool].
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]