iPhone 使用 NSUserDefaults 保存数据

发布于 2024-11-14 13:39:55 字数 4358 浏览 3 评论 0原文

我在将这些可变数组保存到 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 和转储显示的内容

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森末i 2024-11-21 13:39:55

您需要在您的 Tweet 类上实现 NSCoding 协议。 来自文档

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文