2012-07-17 77 views
1

我遇到了Core Data的問題。我有兩個實體(物品和製造商)(其中一個包含可變形屬性,這是另一個實體的陣列)。一切工作正常,直到我嘗試將數組添加到NSManagedObject。下面是一些代碼:保存NS(可變)陣列的核心數據失敗

@implementation HASArticleInitializer 

- (void)initialize:(NSArray *)initArray { 
    //Alloc and init some local variables 
    NSMutableArray *articleEntitiesArray = [NSMutableArray alloc] init]; 
    for (NSInteger pageIndex = 1; pageIndex <= pageCount; pageIndex++) { 

     //Parsing the website 

     //For-loop for every object on every page 
     for (NSInteger object = 0; object < [articleNumbersOnOnePageArray count]; object++) { 
      //Doing other string manipulation and parsing stuff 


      NSManagedObject *newArticleEntry = [NSEntityDescription insertNewObjectForEntityForName:@"Article"inManagedObjectContext:context]; 

      [newArticleEntry setValue:[NSString stringWithFormat:@"%@_%@", manufacturerNumber, articleNumber] forKey:@"number"]; 
      [newArticleEntry setValue:articleTextComplete forKey:@"text"]; 
      [newArticleEntry setValue:articleStatus forKey:@"status"]; 
      [newArticleEntry setValue:manufacturerName forKey:@"manufacturerName"]; 
      [newArticleEntry setValue:manufacturerNumber forKey:@"manufacturerID"]; 

      [articleEntitiesArray addObject:newArticleEntry]; 
     } 
    } 

    NSManagedObject *newManufacturerEntry = [NSEntityDescription insertNewObjectForEntityForName:@"Manufacturer" inManagedObjectContext:context]; 

    //Encoding the NSArray and setting the entity attribute to Binary Data doesn't work neither 
    //NSData *arrayData = [NSKeyedArchiver archivedDataWithRootObject:articleEntitiesArray]; 

    [newManufacturerEntry setValue:manufacturerName forKey:@"name"]; 
    [newManufacturerEntry setValue:manufacturerNumber forKey:@"manufacturerID"]; 
    [newManufacturerEntry setValue:manufacturerCount forKey:@"count"]; 
    [newManufacturerEntry setValue:articleEntitiesArray forKey:@"articles"]; 
    NSLog(@"%@", articleEntitiesArray); 
} 


@end 

我想我以爲它還是忍不住的一切,我真的花這個日子,但我總是得到以下錯誤,當AppDelegate中的- (IBAction)saveAction:(id)sender方法被稱爲:

[15836:a057] -[NSManagedObject encodeWithCoder:]: unrecognized selector sent to instance 0x1001896e0 
[15836:a057] An uncaught exception was raised 
[15836:a057] -[NSManagedObject encodeWithCoder:]: unrecognized selector sent to instance 0x1001896e0 
[15836:a057] (
0 CoreFoundation      0x00007fff8dc94716 __exceptionPreprocess + 198 
1 libobjc.A.dylib      0x00007fff8bfe6470 objc_exception_throw + 43 
2 CoreFoundation      0x00007fff8dd2ad5a -[NSObject(NSObject) doesNotRecognizeSelector:] + 186 
3 CoreFoundation      0x00007fff8dc82c3e ___forwarding___ + 414 
4 CoreFoundation      0x00007fff8dc82a28 _CF_forwarding_prep_0 + 232 
5 Foundation       0x00007fff86931401 _encodeObject + 1163 
6 Foundation       0x00007fff869323cc -[NSKeyedArchiver _encodeArrayOfObjects:forKey:] + 410 
7 Foundation       0x00007fff869320c9 -[NSArray(NSArray) encodeWithCoder:] + 473 
8 Foundation       0x00007fff86931401 _encodeObject + 1163 
9 Foundation       0x00007fff869358f9 +[NSKeyedArchiver archivedDataWithRootObject:] + 182 
10 iLoadPix       0x0000000100003c21 -[HASArticleInitializer initialize:] + 3793 
11 Foundation       0x00007fff86926842 __NSThread__main__ + 1345 
12 libsystem_c.dylib     0x00007fff88553782 _pthread_start + 327 
13 libsystem_c.dylib     0x00007fff885401c1 thread_start + 13 

核心數據模型: Entity 1 Entity 2

正如我所說的,一切工作正常,如果我不救陣,NSLog的是正確的,太。如果它的事項我使用NSThreads(但我不這麼認爲,因爲一切工作得很好):-(

請幫助我!感謝這麼多提前!:-(

回答

1

變形的特性是通過存檔keyed archive/unarchiving ...然而,NSManagedObject並沒有被設計成以這種方式歸檔(它不實現NSCoding協議)。

爲了解決這個問題,你可以把你的託管對象變成字典表示,但是最自然的事情似乎是使用這兩個實體之間的核心數據關係。難道你沒有在這裏建立關係模型嗎?

+0

其實我確實有他們之間的關係,但我認爲我不使用他們(我添加了上述模型的圖片。 我在我的應用程序中有兩個tableviews,一個顯示製造商,另一個顯示所屬文章。所以我將ArticleController的Content Array綁定到了ManufacturerArrayController。 – HAS 2012-07-17 22:03:42

+0

對不起 - 我不仔細閱讀。你爲什麼要保存陣列? – nielsbot 2012-07-17 22:39:26

+0

因爲我想在articlesTableView中顯示它們。有沒有辦法做到這一點,沒有數組?或者沒有*保存*他們? – HAS 2012-07-17 22:45:25