iPhone使用NSUserDefaults保留数据

时间:2011-06-10 08:24:26

标签: ios iphone cocoa-touch plist nsuserdefaults

我很难将这些Mutable数组保存到NSUserDefaults中。我尝试了几种尝试这种方法,但似乎无法得到它。我试图添加字典及其对象,数组然后是字典,字典和字典保存的实际字符串,但似乎没有任何工作。

以下是我用来尝试将这些代码保存到用户默认设置的代码...

- (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];



}

这就是NSLog和Dump显示的内容

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"
)

任何建议都将不胜感激

谢谢我已经阅读了这些文件并且真的试过几次研究它们,但它确实很模糊。他们提供的示例并不符合我的标准或目标,我仍然在寻找解决方案。我已经在Tweet.m文件中实现了NSCoding但是我不确定如何在我的NSUserDefaults设置的控制器中实现它...这里是我用来编码对象的代码,但是我怎么告诉对象什么时候保存它们已被编码?

在.h文件中我只是

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

在.m文件中

  - (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;
}

1 个答案:

答案 0 :(得分:3)

您需要在NSCoding课程上实施Tweet协议。 From the documentation

  

NSUserDefaults类仅支持   存储可以的对象   序列化到属性列表。这个   限制似乎排除了许多   各种对象,如NSColor和   NSFont对象,来自用户默认值   系统。但如果对象符合   NSCoding协议可以归档   NSData对象,属性   与列表兼容的对象。