Re: Create NSArray from persistent storage
Re: Create NSArray from persistent storage
- Subject: Re: Create NSArray from persistent storage
- From: Ivan Myrvold <email@hidden>
- Date: Mon, 18 Jun 2001 13:03:16 +0400
Yes, this worked. I substituted as you suggested the NSDictionary with
NSArray in the restoreFromStorage: example in Vermont Recipes, and
changed the name of setupDictionaryFromStorage: to setupArrayFromStorage
(and set it to return NSArray of course). Now the NSLog reports the
correct count.
It was really the NSArray *theArray = [string propertyList]; which I
did not know about, that the [string propertyList] also could return
NSArray.
Thanks!
Ivan
On Monday, June 18, 2001, at 09:26 AM, David P Henderson wrote:
>
On Monday, June 18, 2001, at 12:20 , Ivan Myrvold wrote:
>
>
> - (void)restoreFromStorage:(NSData *)data {
>
> NSArray *theArray;
>
> theArray=[NSArray arrayWithObject:data];
>
> NSLog(@"theArray size: %d", [theArray count]);
>
> [self setDataArray:theArray];
>
> }
>
>
>
> The strange thing here is that NSLog always says that I have an array
>
> of size 1, even if I have several entries (here I showed two). What
>
> should I do, to get the array correctly populated (with 1 NSDictionary
>
> instance in each element?
>
>
>
Actually, this isn't strange behavior at all. You are creating an array
>
of one element; your data object. +arrayWithObject: is what you want
>
here; you are thinking that +arrayWithObject: is doing +arrayFromData:
>
but no such method exists. What you need to do is convert the data
>
object representation of your property list into an array. Take a look
>
at what Bill Cheeseman does in Vermont Recipes in this situation.
>
>
- (void)restoreFromStorage:(NSData *)data {
>
NSDictionary *dictionary = [self setupDictionaryFromStorage:data];
>
[[self mySettings] restoreFromDictionary:dictionary];
>
}
>
>
- (NSDictionary *)setupDictionaryFromStorage:(NSData *)data {
>
NSString *string = [[NSString allocWithZone:[self zone]]
>
initWithData:data encoding: NSASCIIStringEncoding]; // Convert the data
>
into an NSString instance
>
NSDictionary *dictionary = [string propertyList]; // Use the
>
NSString -propertyList method to convert the string instance into the
>
appropriate container
>
[string release];
>
return dictionary;
>
}
>
>
In your case substitue:
>
NSArray *theArray = [string propertyList];
>
>
Dave
>
--
>
Chaos Assembly Werks
>
"Nothing is too good to be true, except, perhaps, the morality of a
>
bishop."
>
- Israel Zangwill