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