Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.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 什么';我怎么';我从ModelObject访问NSSet?_Iphone_Cocoa Touch_Core Data - Fatal编程技术网

Iphone 什么';我怎么';我从ModelObject访问NSSet?

Iphone 什么';我怎么';我从ModelObject访问NSSet?,iphone,cocoa-touch,core-data,Iphone,Cocoa Touch,Core Data,我一直在努力掌握核心数据,但在获取结果时遇到了一些困难。问题是我有“分支”对象,它们与“电话”对象有对多关系。当我返回一个“分支”并尝试从返回的NSSet访问所有相关的“电话”对象时,我似乎只返回了一个对象。请参阅下面的代码,我正在使用它来尝试查看发生了什么 /*************************************************************************************** start testing the fetched ob

我一直在努力掌握核心数据,但在获取结果时遇到了一些困难。问题是我有“分支”对象,它们与“电话”对象有对多关系。当我返回一个“分支”并尝试从返回的
NSSet
访问所有相关的“电话”对象时,我似乎只返回了一个对象。请参阅下面的代码,我正在使用它来尝试查看发生了什么

 /***************************************************************************************
  start testing the fetched objects
  ***************************************************************************************/
 NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:nil];
 for (int i=0; i<[results count]; i++) {
     NSString *sortcode = [[results objectAtIndex:i] valueForKey:@"sortcode"];
     NSString *lat = [[results objectAtIndex:i] valueForKey:@"latitude"];
     NSString *lon = [[results objectAtIndex:i] valueForKey:@"longitude"];
     NSSet *phoneSet = [[results objectAtIndex:i] valueForKey:@"telephone"];

     int phoneCount = [phoneSet count];
     NSArray *phoneArray = [self arrayFromSet:[[results objectAtIndex:i] valueForKey:@"telephone"]];
     for (int j=0; j<[phoneArray count]; j++) {
         NSString *phoneNumber = [[phoneArray objectAtIndex:j] valueForKey:@"number"];
         NSLog(@"%@,%i,%i",phoneNumber,[phoneArray count],phoneCount);
     }
     NSLog(@"%@,%@,%@",sortcode,lat,lon);
 }
 /***************************************************************************************
  finish testing the fetched objects
  ***************************************************************************************/

在这里,我将对象添加到上下文中:

// Add all telephones to this branch
for (int i=0; i<[telephoneArray count]; i++) {
    [newTelephone setBranch:newBranch];
    [newTelephone setNumber:[[telephoneArray objectAtIndex:i] valueForKey:@"number"]];
    [newBranch addTelephoneObject:newTelephone];
    NSLog(@"i=%i and phone number=%@", i, [newTelephone valueForKey:@"number"]);
}

NSError *error;
if (![managedObjectContext save:&error]) {
    // Handle the error.
    NSLog(@"Save failed with error %@",error);
} else {
    NSLog(@"Save was successful");
}

此代码中存在问题:

// Add all telephones to this branch
for (int i=0; i<[telephoneArray count]; i++) {
    [newTelephone setBranch:newBranch];
    [newTelephone setNumber:[[telephoneArray objectAtIndex:i] valueForKey:@"number"]];
    [newBranch addTelephoneObject:newTelephone];
    NSLog(@"i=%i and phone number=%@", i, [newTelephone valueForKey:@"number"]);
}

该代码中的任何内容都不表示您的
电话集
包含多个对象;你怎么知道它是这样的呢?所有这些都表明,你用来添加电话号码的数组包含多个号码,而不是它们实际上最终出现在你的电话对象的集合中。从您显示的第二组代码的输出来看,似乎您每次都在覆盖数字,而不是将其添加到一个集合中。嗯,好吧,也许我误解了对多关系中对象之间的关系是如何工作的。我想我可以将“电话”对象添加到上下文中,如图所示将它们与分支相关联,这样只需插入与单个“分支”相关的对象(如表中的行)。我完全错了吗?谢谢@gerry3,是的,我意识到我做错了什么。因此,当我将多个
电话
对象关联到一个
分支
时,我需要添加一个
NSSet
,其中包含该
分支
的所有
电话
对象,对吗?您可以单独添加对象或添加一组对象或更改对象集。
2010-03-05 22:15:03.217 AIB[3175:6837] i=0 and phone number=059 9151204
2010-03-05 22:15:03.218 AIB[3175:6837] i=1 and phone number=059 9151179
2010-03-05 22:15:03.218 AIB[3175:6837] i=2 and phone number=059 9151727
2010-03-05 22:15:03.231 AIB[3175:6837] Save was successful
// Add all telephones to this branch
for (int i=0; i<[telephoneArray count]; i++) {
    [newTelephone setBranch:newBranch];
    [newTelephone setNumber:[[telephoneArray objectAtIndex:i] valueForKey:@"number"]];
    [newBranch addTelephoneObject:newTelephone];
    NSLog(@"i=%i and phone number=%@", i, [newTelephone valueForKey:@"number"]);
}
[newBranch addTelephoneObject:newTelephone];