你好。
我在我的应用程序中创建了模型,其中有 ShoppingList 和 Items 类(NSObject)。
每个项目只有一个 ShoppingList 和 ShoppingList 有更多的项目
(购物 list <-->>商品)
我已经将所有数据作为 NSArray 和 NSDictionary 保存到 plist,但现在我在加载项目时遇到问题。
我的 list :
Root (NSArray)->>
___________Item 0(Dictionary)->>
________________________date(Date),name(String),items(Array)->> ____________________________________________________Item 0(Dictionary)->>
________________________________________________________________date(Date),name(String)
这是我编写和阅读此 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;
}
对于项目:
- (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;
}
所以这个代码工作停止在线工作
Item *item = [Item createFromPropertyListRepresentation:dict];
出现错误:
-[__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
感谢您的帮助。
最佳答案
NSDictionary 的 NSFastEnumeration 实现返回键,而不是对象。而不是 for (NSArray *array in loadDictionary)
你应该按照以下方式做一些事情:
NSArray *itemPlist = [loadDictionary objectForKey: @"items"];
NSArray *items = [Item loadPropertyList: itemPlist];
[shoppingLists addObject: items];
但是您可能也想对名称和日期做一些事情。
关于ios - 无法从 .plist 为我的模型 (NSObject) 加载我的所有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24915879/