0

I have an array of custom objects which I want to store in array that updates the UITableView. The array is quite small ~10 objects at most. However, I discovered that I am unable to save an array of custom objects because:

 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 (

It looks like I am going to have to turn the objects into NSData objects using NSKeyArchiver. However, I am having trouble understanding how this will look. Could someone help me with an example? My code for the saving looks like this:

- (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];
}

This is not a duplicate of this question because that example doesn't explain how to retrieve the data, nor can I figure out how to apply it to my example which normally I don't have a problem with but I have just been struggling with this.

Community
  • 1
  • 1
ZB-dev
  • 219
  • 3
  • 12
  • A list of favourites should not be stored in NSUserDefaults. It should be stored in in a file in the Documents directory, perhaps using NSCoding. See https://developer.apple.com/icloud/documentation/data-storage/index.html A quick search should show you everything you need to know to learn how NSCoding works. – Abhi Beckert Jan 05 '15 at 02:21

1 Answers1

0

From what I understand your going to want to do something like this in your 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"];
}
MendyK
  • 1,643
  • 1
  • 17
  • 30