开发者

iPhone Persisting Data with NSUserDefaults

开发者 https://www.devze.com 2023-03-12 02:28 出处:网络
I am having an awful time saving these Mutable arrays into the NSUserDefaults. I have tried several ways to attempt this but just can\'t seem to get it. I have tried to add the dictionary and its obje

I am having an awful time saving these Mutable arrays into the NSUserDefaults. I have tried several ways to attempt this but just can't seem to get it. I have tried to add the dictionary and its objects, the array then the dictionary, the dictionary and actual strings the dictionary holds but none of it seems to work.

Here is the code I am using to try and save these to my user defaults...

- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier 
{


//tweets = [[NSMutableArray alloc] init];



for(NSDictionary *d in statuses) {

NSLog(@"See dictionary: %@", d);

    Tweet *tweet = [[Tweet alloc] initWithTweetDictionary:d];
    [tweets addObject:tweet];
    [retweetCount addObject:tweet];
    [authors addObject:tweet];
    [avatarsURL addObject:tweet];
    [friendsCount addObject:tweet];
    [followerCount addObject:tweet];
    [realNames addObject:tweet];
    开发者_如何学编程[userLocations addObject:tweet];
    [statusesCounts addObject:tweet];
    [favoriteCount addObject:tweet];
    [tweet autorelease];




    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setValuesForKeysWithDictionary:d];
    [userDefaults setObject:tweets forKey:@"text"];
    [userDefaults setObject:authors forKey:@"scree_name"];


    [userDefaults synchronize];
    NSLog(@"setting value");    


    NSArray *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, 
NSUserDomainMask, YES);
    NSLog(@"%@", path);


    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *setpath = [documentsDirectory   
stringByAppendingPathComponent:@"PropertyList.plist"]; 

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: setpath]) 
    {
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"PropertyList" 
ofType:@"plist"]; 

        [fileManager copyItemAtPath:bundle toPath: setpath error:&error];     
    }

}

[self.tableView reloadData];



}

And this is what the NSLog and Dump shows

2011-06-10 03:13:28.773 ThemeCatcher[9173:707] *** -[NSUserDefaults setObject:forKey:]:  
Attempt to insert non-property value '(
"<Tweet: 0x2b02c0>",
"<Tweet: 0x2c1ab0>",
"<Tweet: 0x4a9110>",
"<Tweet: 0x4b0e60>",
"<Tweet: 0x4ac440>",
"<Tweet: 0x2c69a0>",
"<Tweet: 0x2c58b0>",
"<Tweet: 0x49ad50>",
"<Tweet: 0x2c6b90>",
"<Tweet: 0x2c7be0>",
"<Tweet: 0x4979b0>",
"<Tweet: 0x49ee60>",
"<Tweet: 0x4b4ea0>",
"<Tweet: 0x2c9400>",
"<Tweet: 0x4b5080>",
"<Tweet: 0x2c9d20>",
"<Tweet: 0x2ca1d0>",
"<Tweet: 0x4b4a30>",
"<Tweet: 0x2caff0>",
"<Tweet: 0x4b55f0>"

)' of class '__NSArrayM'.
2011-06-10 03:13:28.775 ThemeCatcher[9173:707] *** -[NSUserDefaults setObject:forKey:]:   
Attempt to insert non-property value '(
"<Tweet: 0x2b02c0>",
"<Tweet: 0x2c1ab0>",
"<Tweet: 0x4a9110>",
"<Tweet: 0x4b0e60>",
"<Tweet: 0x4ac440>",
"<Tweet: 0x2c69a0>",
"<Tweet: 0x2c58b0>",
"<Tweet: 0x49ad50>",
"<Tweet: 0x2c6b90>",
"<Tweet: 0x2c7be0>",
"<Tweet: 0x4979b0>",
"<Tweet: 0x49ee60>",
"<Tweet: 0x4b4ea0>",
"<Tweet: 0x2c9400>",
"<Tweet: 0x4b5080>",
"<Tweet: 0x2c9d20>",
"<Tweet: 0x2ca1d0>",
"<Tweet: 0x4b4a30>",
"<Tweet: 0x2caff0>",
"<Tweet: 0x4b55f0>"

)' of class '__NSArrayM'.
2011-06-10 03:13:28.786 ThemeCatcher[9173:707] setting value
2011-06-10 03:13:28.789 ThemeCatcher[9173:707] (
"/var/mobile/Applications/0628FF60-6D3E-4FCA-A6F2-DFB8FC04EF6A/Library"
)

Any suggestions would be greatly appreciated

Thanks I have read the documents and really tried to study them a few times, but it truly is vague. The example they give do not really fit my criteria or objectives and I am still looking for the solution. I have implemented the NSCoding in the Tweet.m file but am not sure how to implement that in my controller where my NSUserDefaults are set...here is the code I am using to encode the objects, but how to I tell the objects when saving they have been encoded?

In the .h file I just

 @property (assign, readwrite) NSString*tweet;
 @property (assign, readwrite) NSString*author;

In .m file

  - (void)encodeWithCoder:(NSCoder *)encoder
{
//Encode properties, other class variables, etc
[encoder encodeObject:self.tweet forKey:@"text"];
[encoder encodeObject:self.author forKey:@"screen_name"];

}



- (id)initWithCoder:(NSCoder *)decoder
{
self = [super init];
if( self != nil )
{
    //decode properties, other class vars
    self.tweet = [decoder decodeObjectForKey:tweet];
    self.author = [decoder decodeObjectForKey:author];

}
return self;
}


You need to implement the NSCoding protocol on your Tweet class. From the documentation:

The NSUserDefaults class only supports the storage of objects that can be serialized to property lists. This limitation would seem to exclude many kinds of objects, such as NSColor and NSFont objects, from the user default system. But if objects conform to the NSCoding protocol they can be archived to NSData objects, which are property list–compatible objects.

0

精彩评论

暂无评论...
验证码 换一张
取 消