我正在寫NSUserDefaults的使用作爲數據存儲機制的應用程序,和我打一個問題,試圖挽救數據(即符合屬性列表協議)時:將NSUserDefaults的不保存的NSDictionary
+ (BOOL)storeAlbum:(Album *)album
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *albums = (NSMutableDictionary *)[prefs objectForKey:@"my_adventure_book_albums"];
NSLog(@"Existing albums: %@",albums);
if (!albums)
albums = [NSMutableDictionary dictionaryWithObject:album forKey:@"album"];
else
[albums setObject:album forKey:@"album"];
NSLog(@"%@",album);
[prefs setObject:albums forKey:@"my_adventure_book_albums"];
return [prefs synchronize];
}
我得到這樣的輸出:
2010-06-29 17:17:09.929 MyAdventureBook[39892:207] Existing albums: (null)
2010-06-29 17:17:09.930 MyAdventureBook[39892:207] test
2010-06-29 17:17:09.931 MyAdventureBook[39892:207] *** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '{
album = test;
}' of class 'NSCFDictionary'.
專輯的說明方法是這樣的:
- (NSString *)description
{
// Convert to a NSDictionary for serializing
if (!title) title = @"";
if (!date) date = [NSDate dateWithTimeIntervalSinceNow:0];
if (!coverImage) coverImage = @"";
if (!images) images = [[NSArray alloc] initWithObjects:@"",nil];
//NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:title,date,coverImage,images,nil] forKeys:[NSArray arrayWithObjects:@"title",@"date",@"coverImage",@"images",nil]];
//NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:title,nil] forKeys:[NSArray arrayWithObjects:@"title",nil]];
//return [dict description];
return @"test";
}
一所有註釋掉的行都會有相同的結果,所以我只是決定看看NSString「測試」是否會起作用,它當然不會。
不,結果相同: *** - [NSUserDefaults setObject:forKey:]:嘗試插入非屬性值'{ album = test;類'NSCFDictionary'的' }'。 – 2010-06-29 21:26:47