Pulling Information from NSUserDefaults?

Discussion in 'iOS Programming' started by WillH16, May 6, 2010.

  1. WillH16 macrumors newbie

    Joined:
    Apr 16, 2010
    #1
    Hey all, so I'm creating this application where the user manipulates information that's stored in a dictionary. I want to be able to save these into NSUserDefaults and then retrieve them to display in a UITableView. Currently my code sets the dictionary to a key that's specific to the date that it is saved (i.e. Dictionary1 will be set with key "5-7-2010" in NSUserDefaults).

    Is there a way for me to pull all these out and display them into a UITableView? I'd imagine I would create a NSMutableDictionary that will hold the dates as keys and the dictionaries as the values and then iterating through them to populate the table. Of course, this assumes that NSUserDefaults stores the objects as a sort-of dictionary, which at first glance seems to be true but I'm probably wrong.

    Is this at all possible? Or was NSUserDefaults not meant to be utilized this way? I know I should probably use NSManagedObject and core data to do this, but I'm facing a deadline where I have to show at least some save functionality and I don't have enough time to read on it.

    If my questions/descriptions are unclear please let me know and I will elaborate further and/or show some code. Thanks a bunch!
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    How do you propose to iterate through the keys? Is their "range" known ahead of time? What about if you used an NSMutableArray instead?

    No, you're correct. It's a "sort-of" dictionary.
     
  3. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    It's not exactly difficult to get the keys as an array. If you are clever and store the dates as a string in YYYYMMDD format you can even sort them easily to get the table contents in order.

    That said I'd probably save out to a plist in the apps documents rather than into NSUserDefaults...
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Well, for you or me maybe... ;)
     

Share This Page