> On Nov 14, 2015, at 15:41, Quincey Morris > <quinceymor...@rivergatesoftware.com> wrote: > > On Nov 14, 2015, at 10:18 , Alex Hall <mehg...@icloud.com > <mailto:mehg...@icloud.com>> 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.
Of course! I keep thinking Swift will figure out what I want AnyObject to be and cast for me, which is very obviously not true and not a good idea. Forcing a cast solved it, thanks. I'll definitely file this, as that's one of the worst error messages I've seen from Swift. > > 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]. -- Have a great day, Alex Hall mehg...@icloud.com _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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 arch...@mail-archive.com