我有一个数组,我将其作为 Json 字符串放入下面的字典数组中
{
StockData = (
{
Id = 3;
Name = Banana;
Price = "1.5";
Requests = "";
Size = 1Kg;
Subname = "";
Type = Fruit;
},
{
Id = 4;
Name = Carrot;
Price = 2;
Requests = "";
Size = 1Kg;
Subname = "";
Type = Vegetable;
},
{
Id = 15;
Name = Kiwi;
Price = "1.3";
Requests = "";
Size = 1kg;
Subname = "";
Type = Fruit;
},
{
Id = 5;
Name = Parsnips;
Price = "1.75";
Requests = "";
Size = 1kg;
Subname = Large;
Type = Vegetable;
},
{
Id = 14;
Name = s;
Price = 0;
Requests = "";
Size = s;
Subname = s;
Type = s;
},
{
Id = 13;
Name = a;
Price = 0;
Requests = "";
Size = a;
Subname = a;
Type = a;
},
{
Id = 12;
Name = Brocoli;
Price = 2;
Requests = "";
Size = 1Kg;
Subname = "";
Type = Vegetable;
}
);
}
然后调用以下方法。我使用 NSManagedObjectSubclass (StockArray) 并尝试从数据模型中获取属性并用数组中的内容填充它们。
+ (StockArray *)stockWithStockInfo:(NSDictionary *)stockInfo
inManagedObjectContext:(NSManagedObjectContext *)context
{
StockArray *stock = nil;
MySingletonCentre *tmp = [MySingletonCentre sharedSingleton];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"StockData"];
//NSLog(@"Log request %@", request);
request.predicate =[NSPredicate predicateWithFormat:@"unique = %@", [tmp.stockDict objectForKey:@"Id"]];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSError *error =nil;
NSArray *matches = [context executeFetchRequest:request error:&error];
if(!matches || ([matches count] > 1)){
NSLog(@"Log StockArray+Stock.h%@", @"error");
// NSLog(@"Log stock array??????1 %@", stock);
}
else if
([matches count] ==0){
stock = [NSEntityDescription insertNewObjectForEntityForName:@"StockData" inManagedObjectContext:context];
NSLog(@"Log %@", stock);
stock.unique = [tmp.globalStockArray valueForKeyPath:@"Id" ] ;
//
stock.type =[tmp.globalStockArray valueForKeyPath:@"Type"];
stock.name =[tmp.globalStockArray valueForKeyPath:@"Name"];
stock.subName =[tmp.globalStockArray valueForKeyPath:@"Subname" ] ;
stock.price =[tmp.globalStockArray valueForKeyPath:@"Price"];
stock.size =[tmp.globalStockArray valueForKeyPath:@"Size"];
stock.requests =[tmp.globalStockArray valueForKeyPath:@"Requests"];
一旦完成,这些项目就是我认为适合核心数据的格式???
data: {
name = "(\n Banana,\n Carrot,\n Kiwi,\n Parsnips,\n s,\n a,\n Brocoli\n)";
price = "(\n \"1.5\",\n 2,\n \"1.3\",\n \"1.75\",\n 0,\n 0,\n 2\n)";
requests = "(\n \"\",\n \"\",\n \"\",\n \"\",\n \"\",\n \"\",\n \"\"\n)";
size = "(\n 1Kg,\n 1Kg,\n 1kg,\n 1kg,\n s,\n a,\n 1Kg\n)";
subName = "(\n \"\",\n \"\",\n \"\",\n Large,\n s,\n a,\n \"\"\n)";
type = "(\n Fruit,\n Vegetable,\n Fruit,\n Vegetable,\n s,\n a,\n Vegetable\n)";
unique = "(\n 3,\n 4,\n 15,\n 5,\n 14,\n 13,\n 12\n)";
})
我已经尝试了大约五天,在阅读和观看了许多教程之后,我仍然找不到该怎么做。我想知道格式是否错误。似乎我是如此接近,因为上述两种格式似乎都以正确的方式显示数据。
我只写了四个月的代码,我只完成了一个应用程序,所以这对我来说仍然是一个学习曲线。
我不仅不确定要使用哪种格式,我还想知道保存方法。
我现在有以下代码,它保存,但将其全部保存到一行。如果我在“for (NSDictionary *dict in self.arrayToUseOnTableView)”循环中放置一个日志,那么该日志会带回与数组中相同数量的日志,但似乎不是为每个字典提供一个实体它把它们合二为一。
-(void) saveJSONToCoreData{
Stock *stock = nil;
NSManagedObjectContext *context = [self managedObjectContext];
stock = [NSEntityDescription insertNewObjectForEntityForName:@"Stock" inManagedObjectContext:context];
// NSLog(@"Logstock 1 %@", stock);
for (NSDictionary *dict in self.arrayToUseOnTableView)
{
stock.unique = [self.arrayToUseOnTableView valueForKeyPath:@"Id" ] ;
stock.type =[self.arrayToUseOnTableView valueForKeyPath:@"Type"];
stock.name =[self.arrayToUseOnTableView valueForKeyPath:@"Name"];
stock.subName =[self.arrayToUseOnTableView valueForKeyPath:@"Subname" ] ;
stock.price =[self.arrayToUseOnTableView valueForKeyPath:@"Price"];
stock.size =[self.arrayToUseOnTableView valueForKeyPath:@"Size"];
stock.requests =[self.arrayToUseOnTableView valueForKeyPath:@"Requests"];
}
NSLog(@"Log %@", stock);
NSError *error;
if (![context save:&error]){
NSLog(@"Error");
}else{
NSLog(@"saved");
}
}