0

我的桌子有问题!

我使用解析表视图,但是当我更改视图时,我的表会丢失数据。所以我决定将所有数据保存到nsuserdefault;但是,问题在这里,NSUserDefault 警告我: “请注意,属性列表中的字典和数组也必须只包含属性值。”

注意:itemsToDisplay 是一个 NSMutableArray,包含 parsedItems 的标题、url、数据和摘要。

好吧,这是我的代码:

self.itemsToDisplay = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"items"] mutableCopy];

if (!self.itemsToDisplay) {
    self.itemsToDisplay = [[NSMutableArray alloc] init];
}

self.itemsToDisplay = [[NSMutableArray alloc]init];

self.itemsToDisplay = [parsedItems sortedArrayUsingDescriptors:
                       [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"date"
                                                                             ascending:NO] autorelease]]];


[[NSUserDefaults standardUserDefaults] setObject:self.itemsToDisplay forKey:@"items"];
[[NSUserDefaults standardUserDefaults] synchronize];

我想问题是setObject:self.itemsToDisplay,但我不知道如何解决。

感谢你们..

4

2 回答 2

1

First lets mention that the table cannot lose data because it does not hold any user data. The data is either provided through bindings or through delegation see NSTableViewDataSource in Apples documentation).

Second, the first three assignments to self.itemsToDisplay serve no purpose (unless you have side-effects in the setter) because they are all overridden by the last assignment.

Finally, if this code is already in the delegate then the delegate should be instantiated in your NIB file for the data to survive past a view swap. If your delegate is an object that is instantiated with your view it will also die with it along with all of the data and writing to the user-defaults is a bad idea for what you are trying to achieve. Simply set the delegate to an object whose lifetime is greater than that of both views.

于 2013-04-25T19:03:18.460 回答
0
self.itemsToDisplay = [parsedItems sortedArrayUsingDescriptors:
                       [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"date"
                                                                             ascending:NO] autorelease]]];

//First lets encode it
NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:self.itemsToDisplay];
[userDefault setObject:myEncodedObject forKey:[NSString stringWithFormat:@"sample"]];
于 2013-04-26T06:48:50.960 回答