简体   繁体   中英

Can't load all of my objects for my model (NSObject) from .plist

Hello.

I have created model in my app where i have ShoppingList and Items class(NSObject). Every item have just one ShoppingList and ShoppingList have more Items

(ShoppingList<-->>Items)

I already save all data to plist as NSArray and NSDictionary, but now i have problem load back my Items.

My plist:

Root (NSArray)->>

___________Item 0(Dictionary)->>

________________________date(Date),name(String),items(Array)->> ____________________________________________________Item 0(Dictionary)->>
________________________________________________________________date(Date),name(String)

And here is my code for write and read this plist:

- (NSDictionary *)propertyListRepresentation {
NSMutableArray *itemsAsPropertyLists = [NSMutableArray new];
for (Item *item in self.items) {
    NSDictionary *itemPropertyList = [item propertyListRepresentation];
    [itemsAsPropertyLists addObject:itemPropertyList];
}

return @{
         @"name": self.nameOfList ?: @"",
         @"date": self.date ?: [NSDate distantPast],
         @"items": itemsAsPropertyLists,
         };
}
+ (ShoppingList *)createFromPropertyListRepresentation:(NSDictionary *)plist {
ShoppingList *newList = [ShoppingList new];

newList.nameOfList = [plist objectForKey:@"name"];
newList.date = [plist objectForKey:@"date"];

return newList;
}
+ (NSArray *)loadPropertyList{
NSArray*loadData = [NSArray arrayWithContentsOfFile:@"/Users/Me/Desktop/saved.plist"];
NSMutableArray *shoppingLists = [NSMutableArray new];

for (NSDictionary *loadDictionary in loadData) {
     ShoppingList *shoppingList = [ShoppingList createFromPropertyListRepresentation:loadDictionary];
    [shoppingLists addObject:shoppingList];

     for (NSArray *array in loadDictionary) {
          NSArray *itemsArray = [NSArray new];
       itemsArray = [Item loadPropertyList:array];
       [shoppingLists addObject:itemsArray];
     }
}
return shoppingLists;

}

And for Item:

- (NSDictionary *)propertyListRepresentation {
return @{
         @"name": self.name ?: @" ",
        @"date" : self.date ?:[NSDate date],
         };
}
 +(Item*)createFromPropertyListRepresentation:(NSDictionary*)dict {
Item *newItems = [Item new];

newItems.name = [dict objectForKey:@"name"];
newItems.date = [dict objectForKey:@"date"];

return newItems;
}
+(NSArray*)loadPropertyList:(NSArray*)array {
NSMutableArray *listOfItems = [NSMutableArray new];
for (NSDictionary*dict in array) {
    Item *item = [Item createFromPropertyListRepresentation:dict];
    [listOfItems addObject:item];
    }

return listOfItems;
}

So this code work stop working on line

Item *item = [Item createFromPropertyListRepresentation:dict];

with the error:

-[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance

Thank for any help.

NSDictionary's implementation of NSFastEnumeration returns keys, not objects. Instead of for (NSArray *array in loadDictionary) you should do something along the lines of:

NSArray *itemPlist = [loadDictionary objectForKey: @"items"];
NSArray *items = [Item loadPropertyList: itemPlist];
[shoppingLists addObject: items];

But you might want to do something with the name and the date, too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM