1

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

这是我的简单 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:],
4

1 回答 1

0

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

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

于 2013-10-08T13:53:01.947 回答