将非属性列表项保存到NSUserDefaults

时间:2015-01-04 23:07:02

标签: ios objective-c arrays cocoa-touch nsuserdefaults

我有一个自定义对象数组,我想存储在更新UITableView的数组中。阵列非常小〜最多10个对象。但是,我发现我无法保存自定义对象数组,因为:

 Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')
2015-01-04 17:56:33.414 fanSyncDemo[13985:1264828] Attempt to set a non-property-list object (

看起来我将不得不使用NSKeyArchiver将对象转换为NSData对象。但是,我无法理解它的外观。有人可以帮我举个例子吗?我的保存代码如下所示:

- (IBAction)savePressed:(id)sender {

    if (_NotificationsSegmentedControl.selectedSegmentIndex == 0){
        _notificationBool = @"Yes";

    }
    else{
        _notificationBool = @"No";
    }

    MyFavorite *favorite = [[MyFavorite alloc] initWithName:_nameFavoriteTextField.text withLocation:@"FANLOCATION" withIsOnNotificationsScreen:_notificationBool];



    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray *favoritesList = [[NSMutableArray alloc] initWithArray: [defaults objectForKey:@"favoritesList"]];
    [favoritesList addObject:favorite];
    [defaults setObject:favoritesList forKey:@"favoritesList"];
    [defaults synchronize];
}

这不是重复的of this question,因为该示例并未解释如何检索数据,也无法弄清楚如何将其应用于我通常不会拥有的数据问题,但我一直在努力解决这个问题。

1 个答案:

答案 0 :(得分:0)

根据我的理解,您希望在MyFavorite班级

中执行此类操作
- (id)initWithCoder:(NSCoder *)aDecoder
{
    _location = [aDecoder decodeObjectForKey:@"location"];
    _isOnNotificationsScreen = [aDecoder decodeObjectForKey:@"notificationScreen"];
}


- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.location forKey:@"location"];
[aCoder encodeObject:self.isOnNotificationsScreen forKey:@"notificationScreen"];
}