• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: UserDefaults not letting me access a dictionary?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: UserDefaults not letting me access a dictionary?


  • Subject: Re: UserDefaults not letting me access a dictionary?
  • From: Quincey Morris <email@hidden>
  • Date: Sat, 14 Nov 2015 12:41:04 -0800
  • Feedback-id: 167118m:167118agrif8a:167118sC_MHiuFRn:SMTPCORP

On Nov 14, 2015, at 10:18 , Alex Hall <email@hidden> 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@hidden)

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:

This email sent to email@hidden


  • Follow-Ups:
    • Re: UserDefaults not letting me access a dictionary?
      • From: Alex Hall <email@hidden>
References: 
 >UserDefaults not letting me access a dictionary? (From: Alex Hall <email@hidden>)

  • Prev by Date: Re: Identify image file count in directory
  • Next by Date: Re: Identify image file count in directory
  • Previous by thread: UserDefaults not letting me access a dictionary?
  • Next by thread: Re: UserDefaults not letting me access a dictionary?
  • Index(es):
    • Date
    • Thread