保存NS(Mutable)阵列的核心数据失败

时间:2012-07-17 21:39:50

标签: core-data nsarray nsmanagedobject

我遇到Core Data问题。我有两个实体(文章和制造商)(一个包含可转换属性,它是另一个实体的数组)。一切正常,直到我尝试将数组添加到NSManagedObject。这是一些代码:

@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 个答案:

答案 0 :(得分:1)

可转换属性通过键控归档/取消归档进行归档...但NSManagedObject并非设计为以这种方式归档。 (它没有实现NSCoding协议。)

要解决此问题,您可以将托管对象转换为字典表示形式,但最自然的事情似乎是使用2个实体之间的核心数据关系。你有没有在这里建立关系的原因?