【发布时间】:2015-02-04 23:49:17
【问题描述】:
我正在尝试将数组中的数据插入 Core Data,但只有数组中的最后一个对象会进入数据库。
这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
_myArray = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil];
[self addToCoreData];
}
-(void)addToCoreData {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSError *error;
NSManagedObject *object;
object = [NSEntityDescription insertNewObjectForEntityForName:@"myEntity" inManagedObjectContext:context];
for (int x = 1; x < [_myArray count]; x++) {
[object setValue:_myArray[x] forKey:@"name"];
}
[context save:&error];
[self fetchData]; //Puts data into UITableview
}
我检查了 SQLite 文件,只插入了“3”。
【问题讨论】:
-
你只有一个 NSEntityDescription。因此只有一个值。
标签: ios objective-c iphone xcode core-data