簡體   English   中英

iPhone 使用 NSUserDefaults 持久化數據

[英]iPhone Persisting Data with NSUserDefaults

我將這些可變的 arrays 保存到 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 的 controller 中實現它……這是我用來編碼對象的代碼,但是我如何告訴對象何時保存它們已被編碼?

在.h文件中我只是

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

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

您需要在Tweet class 上實現NSCoding協議。 從文檔中

NSUserDefaults class 僅支持存儲可序列化為屬性列表的對象。 這種限制似乎從用戶默認系統中排除了多種對象,例如 NSColor 和 NSFont 對象。 但是如果對象符合 NSCoding 協議,它們可以歸檔到 NSData 對象,這些對象是屬性列表兼容的對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM