我是 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:],