将非属性列表项保存到 NSUserDefaults

Saving non-property list items to 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 因为那个例子没有解释如何检索数据,我也不知道如何将它应用到我的例子中,通常我没有问题但我一直在为此苦苦挣扎。

据我了解,您想要在 MyFavorite class

中执行类似的操作
- (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"];
}