核心数据:实体属性混合

时间:2009-12-06 13:30:57

标签: objective-c cocoa core-data

我有NSWindow我用来创建新记录。按下“添加”按钮后,将调用某个方法,其中我执行以下操作:

- (IBAction)addActionAddSheet:sender {
    NSManagedObjectContext *moc = [self managedObjectContext];
    NSManagedObject *newObject  = [NSEntityDescription insertNewObjectForEntityForName:@"Recipe" inManagedObjectContext:moc];

    [newObject setValue:[newRecipeName stringValue] forKey:@"name"];
    [newObject setValue:[newRecipeInstructions string] forKey:@"instructions"];

    NSLog(@"New object is: %@", newObject);

    [NSApp endSheet:addSheet];
    [addSheet orderOut:sender];
}

我执行两次后NSLog的(编辑)输出如下:

<NSManagedObject: 0x10045c6a0> (entity: Recipe; id: 0x1004564a0 <x-coredata:///Recipe/t18FEC674-8937-49DF-A18B-940EF82E83C32> ; data: {
    instructions = X;
    name = X;
})
<NSManagedObject: 0x1149069a0> (entity: Recipe; id: 0x100106a20 <x-coredata:///Recipe/t18FEC674-8937-49DF-A18B-940EF82E83C33> ; data: {
    instructions = Y;
    name = Y;
})

这完全符合预期。但是,我的tableview绑定到数组控制器,表明对象X和Y都有 X 作为instructions的值。

当我退出应用程序并保存数据时,我在xml文件中看到的内容再次不同:

    <attribute name="name" type="string">Y</attribute>
    <attribute name="instructions" type="string">Y</attribute>

    <attribute name="name" type="string">X</attribute>
    <attribute name="instructions" type="string">Y</attribute>

这次他们都有值 Y

NSLog的输出确实显示我在这里处理两个不同的对象,所以我不确定我可能会做什么可能会影响两者。

这些是我未经编辑的输出:

2009-12-07 14:46:18.409 Recipe[11578:a0f] managedObjectContext
2009-12-07 14:46:18.411 Recipe[11578:a0f] persistentStoreCoordinator
2009-12-07 14:46:18.411 Recipe[11578:a0f] managedObjectModel
2009-12-07 14:46:18.423 Recipe[11578:a0f] applicationSupportDirectory
2009-12-07 14:46:18.436 Recipe[11578:a0f] externalRecordsDirectory
2009-12-07 14:46:20.484 Recipe[11578:a0f] Show Add sheet
2009-12-07 14:46:20.485 Recipe[11578:a0f] Clear values
2009-12-07 14:46:20.494 Recipe[11578:a0f] call beginSheet
2009-12-07 14:46:23.632 Recipe[11578:a0f] addActionAddSheet: -- Add button clicked
2009-12-07 14:46:23.632 Recipe[11578:a0f] Create new mananged obj in context
2009-12-07 14:46:23.634 Recipe[11578:a0f] managedObjectContext
2009-12-07 14:46:23.635 Recipe[11578:a0f] Set values for name and instructions
2009-12-07 14:46:23.636 Recipe[11578:a0f] New object is: <NSManagedObject: 0x1001c89c0> (entity: Recipe; id: 0x1001c9470 <x-coredata:///Recipe/tE9BD4EE3-082C-4715-AB66-2C3580223F9E2> ; data: {
    instructions = A;
    name = A;
})
2009-12-07 14:46:23.636 Recipe[11578:a0f] call [NSApp endSheet:] and [addSheet orderOut:]
2009-12-07 14:46:23.637 Recipe[11578:a0f] addSheetDidEnd:returnCode:contextInfo: -- empty method
2009-12-07 14:46:35.327 Recipe[11578:a0f] Show Add sheet
2009-12-07 14:46:35.328 Recipe[11578:a0f] Clear values
2009-12-07 14:46:35.337 Recipe[11578:a0f] call beginSheet
2009-12-07 14:46:39.836 Recipe[11578:a0f] addActionAddSheet: -- Add button clicked
2009-12-07 14:46:39.836 Recipe[11578:a0f] Create new mananged obj in context
2009-12-07 14:46:39.838 Recipe[11578:a0f] managedObjectContext
2009-12-07 14:46:39.839 Recipe[11578:a0f] Set values for name and instructions
2009-12-07 14:46:39.843 Recipe[11578:a0f] New object is: <NSManagedObject: 0x102070ad0> (entity: Recipe; id: 0x10205e420 <x-coredata:///Recipe/tE9BD4EE3-082C-4715-AB66-2C3580223F9E3> ; data: {
    instructions = B;
    name = B;
})
2009-12-07 14:46:39.843 Recipe[11578:a0f] call [NSApp endSheet:] and [addSheet orderOut:]
2009-12-07 14:46:39.844 Recipe[11578:a0f] addSheetDidEnd:returnCode:contextInfo: -- empty method

食谱A:

http://web.me.com/bpeiren/recipeA.png

食谱B:

http://web.me.com/bpeiren/recipeB.png

我的阵列控制器:

http://web.me.com/bpeiren/Cocoa/so/array_controller.png

我的表格列的绑定:

http://web.me.com/bpeiren/Cocoa/so/tablecolumn.png

我的标签绑定:

http://web.me.com/bpeiren/Cocoa/so/label.png

1 个答案:

答案 0 :(得分:2)

如何将表/列绑定到阵列控制器?听起来好像您的“说明”列未绑定到 arrangeObjects.instructions ,但可能 selection.instructions