Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSSet仅从我的for集合中获取最后一个值_Iphone_Ios_Xcode_Core Data_Nsset - Fatal编程技术网

Iphone NSSet仅从我的for集合中获取最后一个值

Iphone NSSet仅从我的for集合中获取最后一个值,iphone,ios,xcode,core-data,nsset,Iphone,Ios,Xcode,Core Data,Nsset,我尝试从实体“Teilnehmer”获取成员,然后我想将其保存到另一个与“buchung”有关系的实体“Buchungsteilnehmer” 问题是,nsset函数只保存姓氏,并将其与我的实体“buchung”建立关系。但是我想将for语句中的所有成员存储到与“buchung”的关系中 你能帮我吗 NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSE

我尝试从实体“Teilnehmer”获取成员,然后我想将其保存到另一个与“buchung”有关系的实体“Buchungsteilnehmer”

问题是,nsset函数只保存姓氏,并将其与我的实体“buchung”建立关系。但是我想将for语句中的所有成员存储到与“buchung”的关系中

你能帮我吗

NSFetchRequest *request = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Teilnehmer" inManagedObjectContext:context];

        [request setEntity:entity];

        NSArray *events = [context executeFetchRequest:request error:nil];




        for (Teilnehmer *teil in events) {
         teilnehmer = [NSEntityDescription insertNewObjectForEntityForName:@"Buchungsteilnehmer" inManagedObjectContext:context];
            teilnehmer.name=teil.name;
            NSLog(@"Name der Teilnehmer lautet: %@",teil.name);
            NSError *error;
            if (![context save:&error]) 
            {
                NSLog(@"Fehler beim hinzufügen : %@", [error localizedDescription]);
            }

        }

        NSSet *set = [NSSet setWithObject:teilnehmer];    
        NSLog(@"SET: %@",set);
        buchung.buchungsteilnehmer=set;

        NSError *error;
        if (![context save:&error]) 
        {
            NSLog(@"Fehler beim hinzufügen : %@", [error localizedDescription]);
        }

在for循环开始之前创建NSMutableSet。每次插入新实体时,在集合上调用addObject。

您创建的集合只有一个对象,那么您如何期望它有多个对象

按如下方式更改代码:

  NSMutableSet *set = [[NSMutableSet alloc] init];                

  for (Teilnehmer *teil in events) {
         teilnehmer = [NSEntityDescription insertNewObjectForEntityForName:@"Buchungsteilnehmer" inManagedObjectContext:context];
            teilnehmer.name=teil.name;
            NSLog(@"Name der Teilnehmer lautet: %@",teil.name);
            NSError *error;
            if (![context save:&error]) 
            {
                NSLog(@"Fehler beim hinzufügen : %@", [error localizedDescription]);
            } else {
                [set addObject:teilnehmer];
            }

        }

   buchung.buchungsteilnehmer=set; // I assume you synthesized this member so it retains set
   [set release];