Thursday, August 16, 2018

#1 2018-06-07 02:45:07 pm

jcwelch
Member
Registered: 2009-09-11
Posts: 48

Saving records

So in the app I'm working on, I want to move from a hardcoded list of records to a model where I can add records to the list as needed. currently, the record looks like this:

{serverName:"Some name",serverURL:"http://something.com/",serverAPIKey:"somestring"}

so it SEEMS to me, based on poking around, that the way to store this is as an NSMutableArray of NSDictionaries? Since I'd want to be able to add and remove dictionaries, NSMutableArray seems to be the better option.


Looking at the docs for NSUserDefaults, this seems to make sense given arrayForKey:

The thing I'm not seeing is what's the key for the array? How do I set *that* when I store it, so I have a key with which to retrieve it?

Oh, also, I don't see a good way to set defaults based on either arrays or dicts

Last edited by jcwelch (2018-06-07 02:47:00 pm)

Offline

 

#2 2018-06-07 06:12:26 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5428

Re: Saving records

jcwelch wrote:

\so it SEEMS to me, based on poking around, that the way to store this is as an NSMutableArray of NSDictionaries?



When you store it, mutability is irrelevant. When you retrieve it it will be immutable, but you can make a mutable copy using `mutableCopy()`.

The thing I'm not seeing is what's the key for the array?



It's whatever you want it to be -- it's just a label of sorts, so make it something relevant.

Oh, also, I don't see a good way to set defaults based on either arrays or dicts



Use `setObject:`.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2018-06-07 08:52:49 pm

jcwelch
Member
Registered: 2009-09-11
Posts: 48

Re: Saving records

so basically, if I use arrayForKey, it's going to grab the first thing in there and assign it to the key I tell it to?

which is what I want so if that's how it works, that's cool.

Offline

 

#4 2018-06-07 09:09:31 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5428

Re: Saving records

jcwelch wrote:

so basically, if I use arrayForKey, it's going to grab the first thing in there and assign it to the key I tell it to?



No, it's going to grab the thing that was assigned with that key. You use a key to store a value -- using setObject:forKey: (or a binding to shared defaults using the key) -- and you use the same key to retrieve it -- via arrayForKey: or objectForKey:.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2018-06-08 07:02:34 am

jcwelch
Member
Registered: 2009-09-11
Posts: 48

Re: Saving records

okay, got it. Makes sense, and thanks again!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)