下面的代碼添加到核心數據問題中,但添加後,我無法保存,出現錯誤(乘法驗證錯誤發生)我在覈心數據收集中添加新數據後,無法保存(乘法驗證錯誤發生)
MySQLIXC *ixcDatabase = [[MySQLIXC alloc] init];
NSArray *destinationsForSaleList = [ixcDatabase destinationsForSaleList:carrier];
NSFetchRequest *request1 = [[[NSFetchRequest alloc] init] autorelease];
[request1 setEntity:[NSEntityDescription entityForName:@"DestinationsList"
inManagedObjectContext:managedObjectContext]];
for (NSDictionary *destinationsForSale in destinationsForSaleList) {
NSManagedObject *object1 = [NSEntityDescription
insertNewObjectForEntityForName:@"DestinationsList"
inManagedObjectContext:managedObjectContext];
NSLog(@"Moc: %@",managedObjectContext);
[object1 setValue:@"y" forKey:@"weAreSoldIt"];
// changeDate
NSString *chdate = [destinationsForSale objectForKey:@"chdate"];
NSDateFormatter *changeDate = [[[NSDateFormatter alloc] init] autorelease];
[object1 setValue:[changeDate dateFromString:chdate] forKey:@"changeDate"];
NSLog(@"Carrier :%@ Destination name:%@",carrier, destinationsForSale);
//Country
[object1 setValue:[destinationsForSale objectForKey:@"country"] forKey:@"country"];
//rate
NSNumberFormatter *rate = [[[NSNumberFormatter alloc]init ]autorelease];
[object1 setValue:[rate numberFromString:[destinationsForSale objectForKey:@"price"]] forKey:@"rate"];
不幸的是,我不能通過你建議的方式修復bug。 Bcs Entity DestinationList必須通過項目理解與Entity Carriers建立關係。 這就是我嘗試修復它:
[objectDestinationList setValue:objectCarrier forKey:@"carrier"];
我送到我的方法載體對象爲對象,但它不工作。
在這種情況下,我不知道如何解決它。 Bcs我看到錯誤,但沒有看到爲什麼錯誤是開始的情況。
你知道一個簡單的代碼來糾正與實體的關係嗎?所有我在互聯網上捕捉到的都是一本核心數據手冊,我的Marcus Zarra和他很難理解的例子。他展示了一個複雜的解決方案,我可以理解它,但是使用編程風格在這個時刻對我來說不是那麼容易(根據我在可可編程方面的一個月的經驗;)
這裏是附加信息:我如何創建Carrier實例。我有ManageObjectContext,我從AppDelegate上課。
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:[NSEntityDescription entityForName:@"Carrier"
inManagedObjectContext:managedObjectContext]];
NSManagedObject *carrier = [managedObjectContext executeFetchRequest:request error:&error]
以同樣的方式我準備NSManagedObject for DestinationsList實體。
之後,我添加所有值到NSManagedObject的destinationsList,我必須做Carrer NSManagedObject和destinationsList之間的關係。在這種情況下,我有麻煩。貝婁是我嘗試更新運營商實體關係:
NSSet *newDestSet = [NSSet setWithObjects:objectDestination,nil];
[objectCarrier setValue:newDestSet forKey:@"destinationsList"];
終於我有21 2010-11-03:22:56.968雪[20301:A0F] - [NSCFArray初始化]:無法識別的選擇發送到實例0x1c44e40
貝婁是我的類接口deescription:
@interface InitUpdateIXC : NSObject {
NSInteger destinationType;
}
-(void) updateCarrierList:(NSManagedObjectContext *)managedObjectContext;
-(void)updateDestinationList:(NSManagedObjectContext *)managedObjectContext
forCarrier:(NSString *)carrierName
forCarrierObject:(NSManagedObject *)objectCarrier
destinationType:(NSInteger)destinationType;
@end
沒錯,波紋管在回答當前正確的模式,但有些不同的是,在這裏。 起初,我沒有單獨的實體類,因爲你出現在你的模型中。我的當前類只是NSManagedObject 第二,關係「carrier」對於Entity DestinationsList是非可選的。
解決方案與錯誤描述:
在故障情況下,有什麼我的代碼發生了: 當我嘗試添加的setValue forKey從DestinationsList到托架放回關係,我忘了,NSManagmentObject返回每次陣列,而不僅僅是對象。 這是我收到有關陣列初始化問題的錯誤的原因。
解決方案不發送Carrier對象的方法,對於我來說bcs太難以從數組中提取沒有鍵值的正確對象。我正在使用謂詞訪問來提取對象的數組和lastObject函數從數組中提取正確的對象。之後,我設置accroding價值和一切工作正常。 一個解決方案看起來不像可可風格,所以更好的方法是在未來重構它,任何建議的歡迎。 下面是相應的代碼:
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:[NSEntityDescription entityForName:@"Carrier"
inManagedObjectContext:managedObjectContext]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name =%@",carrierName];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *currentCarriers = [managedObjectContext executeFetchRequest:request error:&error];
[objectDestination setValue:[currentCarriers lastObject] forKey:@"carrier"];
感謝ü非常。 NSLocalizedDescription =「載體是必需的值」。看起來像當我添加具有反比關係的實體時,我也必須添加它。對於我來說,這是非可選的,以保護我在覈心數據存儲中未使用的數據。 – Alex 2010-11-02 10:53:04
如果我有關係命名承運人,如何我可以連接它從代碼,如果我有實體承運人的名稱屬性? – Alex 2010-11-02 11:25:43
當我將對象載體添加到關係中時,我在保存中看到了最終錯誤:2 - [NSAlert alertWithError:]用nil NSError調用。一般的錯誤信息會顯示出來,但用戶應該更好。操作無法完成。沒有關於此問題的其他信息。但在這個時候,我沒有看到任何麻煩,核心數據保存以及數據庫。 – Alex 2010-11-02 14:54:44