iPhone 使用 NSUserDefaults 保存数据
我在将这些可变数组保存到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在您的
Tweet
类上实现NSCoding
协议。 来自文档:You need to implement the
NSCoding
protocol on yourTweet
class. From the documentation: