RestKit在RKEntityMapping行崩溃

时间:2013-10-08 13:40:42

标签: ios json restkit restkit-0.20

我是Objective-C的新手并且非常坚持使用RestKit。我试图在我的网络服务器上用JSON创建一个非常简单的UITableView数据,但我已经被困了一段时间。

这是我简单的JSON:

{
    "restaurants": [
        {
            "id": "27",
            "franchise_name": "Franchise 1",
            "branch_name": "Branch 1",
            "branch_phone": "0200 111 0000",
            "cuisine": "Salad",
            "cooking_time": "15",
            "is_open": 1
        },
        {
            "id": "97",
            "franchise_name": "Franchise 2",
            "branch_name": "Branch 2",
            "branch_phone": "0207 222 0000",
            "cuisine": "Healthy",
            "cooking_time": "10",
            "is_open": 1
        }
    ]
}

这是我的核心数据模型:

    Entities: Restaurant
    Attributes: branchName -> String
    franchiseName -> String
    id -> integer 16
    readyTime-> integer 16

我的视图控制器中有以下代码。在viewDidLoad中,RKEntityMapping会导致异常:

RKManagedObjectStore *managedObjectStore = [RKManagedObjectStore defaultStore];

RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Restaurant" inManagedObjectStore:managedObjectStore]; //THIS LINE CAUSES THE CRASH

[entityMapping addAttributeMappingsFromDictionary:@{
                                                    @"id":             @"id",
                                                    @"cooking_time":   @"readyTime",
                                                    @"branch_name":    @"branchName",
                                                    @"franchise_name": @"franchiseName"}];

RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping pathPattern:@"/api/restaurants" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; //I GET AN ISSUE HERE AS WELL INVOLVING responseDescriptorWithMapping BEING DEPRECIATED.

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http//mywebserver.com/api/resturants"]];

RKManagedObjectRequestOperation *managedObjectRequestOperation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];

managedObjectRequestOperation.managedObjectContext = self.managedObjectContext;

[[NSOperationQueue currentQueue] addOperation:managedObjectRequestOperation];

有几条错误消息,我真的没有:

    NSAssert(objectClass, @"Cannot initialize an entity mapping for an entity with a nil managed object class: Got nil class for managed object class name '%@'. Maybe you forgot to add the class files to your target?", [entity managedObjectClassName]);

在控制台中:

*** Assertion failure in -[RKEntityMapping initWithEntity:],

1 个答案:

答案 0 :(得分:0)

不推荐使用

responseDescriptorWithMapping,它现在有一个额外的method参数。这只是一个警告 - 您应该更新但不必更新。

如果第一次尝试使用managedObjectStore时发生崩溃,则表明您没有正确/完全配置核心数据堆栈(或者您的实体名称/类名称错误) 。查看this doc以获取所需设置的简单概述。如果要使用对象管理器,请选中this doc