Re: Still problems with storing preferences
Re: Still problems with storing preferences
- Subject: Re: Still problems with storing preferences
- From: "M. Uli Kusterer" <email@hidden>
- Date: Fri, 10 Oct 2003 15:07:45 +0200
At 0:46 Uhr +0200 10.10.2003, Eivind Andersen wrote:
Is there anyone who can explain to me how I can store a
NSMutableArray, containing objects from a self-made class? If so, I
would be MOST thankful!
(If anyone wonderes...I'm totally new to both cocoa and objective-c:)
As has been said before on this list, you can store only certain
built-in objects (i.e. objects defined in Foundation) in
NSUserDefaults. This includes NSNumbers, NSStrings, NSDictionaries
and NSArrays of all sorts (even mutable ones, though they'll be
stored as immutable copies, which means you'll have to mutableCopy
them and write them back in to change them), but not ones you've
defined yourself.
As a workaround, I'd suggest you do one of two things:
a) implement the NSCoding protocol in your buddy objects to write out
their data members, and then use NSArchiver and NSUnarchiver to
create NSData objects from your objects, since an NSData object can
be stored in NSUserDefaults just fine.
b) add a method to your buddy objects that can create an NSDictionary
with all the data from your buddy objects (as NSStrings, NSData,
NSNumbers etc.), which you can then write to NSUserDefaults. Then add
an initializer to your buddy objects that allows re-creating a buddy
object from an NSDictionary as created by the other method.
Personally, I'm partial to b because it results in human-readable
.plist files (Property List Editor can easily edit NSNumbers and
NSStrings, but NSData just looks like a cryptic character sequence),
but both approaches have their merits.
--
Cheers,
M. Uli Kusterer
------------------------------------------------------------
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.