0

我有两个实体:人员和网络(xcdatamodeld 屏幕 - http://rghost.ru/47818182/image.png)。在方法fetch我提取数组 peoples 并且我想打印network.url

错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSFaultingMutableSet url]: unrecognized selecto

代码:

    - (IBAction)insert:(id)sender
{
    NSMutableSet* networks = [NSMutableSet set];

    // People entity
    People *people = (People *)[NSEntityDescription insertNewObjectForEntityForName:@"People" inManagedObjectContext:self.managedObjectContext];
    people.name = @"Alex";
    people.age = @23;

    // Network entity
    Network *network = (Network *)[NSEntityDescription insertNewObjectForEntityForName:@"Network" inManagedObjectContext:self.managedObjectContext];
    network.url = @"http://facebook.com/alex";
    [networks addObject:network];
[people addNetwork:networks];

    // Save
    [self.managedObjectContext save:nil];
}

- (IBAction)fetch:(id)sender
{
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"People"];
    NSArray *peoples = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];

    for (People *people in peoples)
    {
        Network *network = (Network *)people.network;
        NSLog(@"%@", network.url);
    }
}
4

2 回答 2

0

“People ->> Network”是一对多关系,因此people.network 是一Network 对象,而不是单个 Network 对象。以下应该有效:

for (People *people in peoples)
{
    for (Network *network in people.network)
    {
        NSLog(@"%@", network.url);
    }
}
于 2013-08-01T10:59:12.930 回答
0

因为人与网络之间的关系太多了。所以如果你查看你的ManagedObject类,你的对象将返回 NSSet People。所以你的循环应该是这样的。

for (People *people in [peoples allObjects])
{
    Network *network = (Network *)people.network;
    NSLog(@"%@", network.url);
}
于 2013-08-01T11:24:51.423 回答