Re: Create NSArray from persistent storage
Re: Create NSArray from persistent storage
- Subject: Re: Create NSArray from persistent storage
- From: David P Henderson <email@hidden>
- Date: Mon, 18 Jun 2001 01:26:06 -0400
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]]
initWith
Data: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