Ios 仅将数组中的最后一个对象插入核心数据

Ios 仅将数组中的最后一个对象插入核心数据,ios,objective-c,iphone,xcode,core-data,Ios,Objective C,Iphone,Xcode,Core Data,我试图将数组中的数据插入核心数据,但只有数组中的最后一个对象进入数据库 这是我的密码: - (void)viewDidLoad { [super viewDidLoad]; _myArray = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil]; [self addToCoreData]; } -(void)addToCoreData { AppDelegate *appDelegate = [[UIAp

我试图将数组中的数据插入核心数据,但只有数组中的最后一个对象进入数据库

这是我的密码:

- (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
}
-(void)viewDidLoad{
[超级视图下载];
_myArray=[NSMutableArray arrayWithObjects:@“1”,“2”,“3”,nil];
[自添加存储数据];
}
-(void)addToCoreData{
AppDelegate*AppDelegate=[[UIApplication sharedApplication]委托];
NSManagedObjectContext*上下文=[appDelegate managedObjectContext];
n错误*错误;
NSManagedObject*对象;
object=[NSEntityDescription insertNewObjectForEntityForName:@“myEntity”inManagedObjectContext:context];
对于(int x=1;x<[u myArray count];x++){
[object setValue:_myArray[x]forKey:@“name”];
}
[上下文保存:&错误];
[自取数据];//将数据放入UITableview
}

我检查了SQLite文件,只插入了“3”。

尝试将托管对象的创建移动到循环中:

-(void)addToCoreData {

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context = [appDelegate managedObjectContext];

     NSError *error;
     NSManagedObject *object;


    for (int x = 1; x < [_myArray count]; x++) {
        object = [NSEntityDescription    insertNewObjectForEntityForName:@"myEntity" inManagedObjectContext:context];
        [object setValue:_myArray[x] forKey:@"name"];
    }
    [context save:&error];

    [self fetchData]; //Puts data into UITableview
}
-(void)addToCoreData{
AppDelegate*AppDelegate=[[UIApplication sharedApplication]委托];
NSManagedObjectContext*上下文=[appDelegate managedObjectContext];
n错误*错误;
NSManagedObject*对象;
对于(int x=1;x<[u myArray count];x++){
object=[NSEntityDescription insertNewObjectForEntityForName:@“myEntity”inManagedObjectContext:context];
[object setValue:_myArray[x]forKey:@“name”];
}
[上下文保存:&错误];
[自取数据];//将数据放入UITableview
}

得到一个SIGABRT:“[NSArrayM insertObject:atIndex::对象不能为零”没关系,SIGABRT部分不相关,这就解决了数组问题,谢谢你只有一个NSEntityDescription。因此只有一个值。