无法从.plist加载我的模型(NSObject)的所有对象

时间:2022-06-02 00:20:53

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类(NSObject)。每个项目只有一个ShoppingList,ShoppingList有更多项目

(ShoppingList<-->>Items)

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

我已经将所有数据保存为plist作为NSArray和NSDictionary,但现在我有问题加载我的项目。

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:

这是我编写和阅读此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];

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.

感谢您的帮助。

1 个解决方案

#1


0  

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

NSDictionary的NSFastEnumeration实现返回键,而不是对象。而不是for(NSDrray *在loadDictionary中的数组),你应该做的事情:

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.

但是你也可能想要对名称和日期做些什么。

#1


0  

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

NSDictionary的NSFastEnumeration实现返回键,而不是对象。而不是for(NSDrray *在loadDictionary中的数组),你应该做的事情:

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.

但是你也可能想要对名称和日期做些什么。