1
你好我試着去插入一個多對多的關係,但未能=(插入一個多對多的關係在Xcode 4.2使用coredata
這裏是代碼
categoria.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class SubCategoria;
@interface Categoria : NSManagedObject
@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSString * nombre;
@property (nonatomic, retain) NSSet *subcategoria;
@end
@interface Categoria (CoreDataGeneratedAccessors)
- (void)addSubcategoriaObject:(SubCategoria *)value;
- (void)removeSubcategoriaObject:(SubCategoria *)value;
- (void)addSubcategoria:(NSSet *)values;
- (void)removeSubcategoria:(NSSet *)values;
@end
categoria.m
#import "Categoria.h"
#import "SubCategoria.h"
@implementation Categoria
@dynamic id;
@dynamic nombre;
@dynamic subcategoria;
@end
然後我嘗試以下列方式插入。
NSManagedObjectContext *context = [self managedObjectContext];
Categoria *cat = [NSEntityDescription
insertNewObjectForEntityForName:@"Categoria"
inManagedObjectContext:context];
cat.id =[NSNumber numberWithInt:2];
cat.nombre = @"portatil";
SubCategoria *subCat = [NSEntityDescription
insertNewObjectForEntityForName:@"SubCategoria"
inManagedObjectContext:context];
subCat.id = [NSNumber numberWithInt:1];
subCat.nombre = @"toshiba";
SubCategoria *subCat2 = [NSEntityDescription
insertNewObjectForEntityForName:@"SubCategoria"
inManagedObjectContext:context];
subCat2.id =[NSNumber numberWithInt:1];
subCat2.nombre = @"sony";
[cat addSubcategoriaObject:subCat];
[cat addSubcategoriaObject:subCat2];
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
它插入類別和子類別,但當它試圖插入關係船給我這個例外。
-[Categoria subcategoria]: unrecognized selector sent to instance 0x6dd8290
爲什麼這個錯誤發生????? 非常感謝洙提前。
嘿,我解決了這個問題,真的不知道真的是如何,但我懷疑在多關係的關係中,我並沒有檢查級聯字段(我知道,由於是級聯刪除沒有那麼多的意思) 。任何方式工作,也許我只需要重新生成模型。但是非常感謝 – IturPablo 2011-12-28 17:18:06