Attempt to insert non-property value Objective C
Asked Answered
H

2

21

Hi l am trying to create a fourates lists from an restaurants Object, my application has a list of different restaurants, l want the ability for users to add favourate restaurants, and this code is not working

- (IBAction)toggleFav:(id)sender {

    Restaurant *resto = [self restaure];

    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:resto.price forKey:@"restoPrice"];
    [dic setObject:resto.restaurantId forKey:@"restaurantId"];
    [dic setObject:resto.restoAbout forKey:@"restoAbout"];
    [dic setObject:resto.restoAddress forKey:@"restoAddress"];
    [dic setObject:resto.restoBeverages forKey:@"restoBeverages"];
    [dic setObject:resto.restoCategory forKey:@"restoCategory"];
    [dic setObject:resto.restoEmail forKey:@"restoEmail"];
    [dic setObject:resto.restoLogo forKey:@"restoLogo"];
    [dic setObject:resto.restoName forKey:@"restoName"];
    [dic setObject:resto.restoPhone forKey:@"restoPhone"];
    [dic setObject:resto.restoCity forKey:@"restoCity"];

    NSArray *dicArray = [dic allKeys];


    if([sender isSelected]){
        //...
        [sender setSelected:NO];
        NSMutableArray *array = [[[NSUserDefaults standardUserDefaults] objectForKey:@"restoName"] mutableCopy];
        [array removeObject:dicArray];
        [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"restoName"];

    } else {
        //...
        [sender setSelected:YES];
        NSMutableArray *array = [[[NSUserDefaults standardUserDefaults] objectForKey:@"restoName"] mutableCopy];
        [array addObject:dicArray];
        [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"restoName"];
        [[NSUserDefaults standardUserDefaults] synchronize];

       //NSLog(@"%@",[[NSUserDefaults standardUserDefaults] stringForKey:@"restoName"]);
  }
}

' of class '__NSCFArray'. Note that dictionaries and arrays in property lists must also contain only property values.

Harding answered 7/4, 2013 at 0:9 Comment(1)
Check your dictionary is there any null values or not?Individualism
C
58

If you don't want to bother about ensuring all your dictionary values are the property list types, then you can simply convert the dictionary into NSData,

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:[NSKeyedArchiver archivedDataWithRootObject:self.myDictionary] forKey:@"MyData"];
[def synchronize];

And to get back into a dictionary from sandbox:

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSData *data = [def objectForKey:@"MyData"];
NSDictionary *retrievedDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];
self.myDictionary = [[NSDictionary alloc] initWithDictionary:retrievedDictionary];

Hope this helps someone. :D


ADDITIONAL NOTE: If you are getting a mutable dictionary, or mutable array, then remember to use "mutableCopy" method. Otherwise you can't add or delete objects from your retrieved dictionary/array. For example:

NSMutableDictionary *retrievedDictionary = 
[[NSKeyedUnarchiver unarchiveObjectWithData:data] mutableCopy];

Or

NSMutableArray *retrievedArray = 
[[NSKeyedUnarchiver unarchiveObjectWithData:data] mutableCopy];
Cason answered 30/1, 2014 at 9:5 Comment(0)
P
19

You can only store property list types (array, data, string, number, date, dictionary) or urls in NSUserDefaults. You'll need to convert your model object to those.

Postfree answered 7/4, 2013 at 0:12 Comment(3)
You could make an NSDictionary and put all the properties of your object in it, for example. Or you could implement NSCoding and use that, it'd be similar (should be plenty of docs on it; search for NSKeyedArchiver if NSCoding doesn't find them)Postfree
Hi mate see my code above l have created an array but still same problem, can't believe its 3 full days trying this, With the encoding method l am not sure where to to put NSObject<NScoding> because my detailView class implements this @interface DetailViewController :UIViewControllerHarding
The array you made doesn't even contain the values of your properties... just their names. As for NSCoding, it sounds like you need to go read a basic tutorial on protocols. You just need to declare conformance to NSCoding, not anything involving NSObject.Postfree

© 2022 - 2024 — McMap. All rights reserved.