1
崩潰我新的Objective-C和非常套牢RestKit。我想我的Web服務器上創建一個與JSON數據非常簡單UITableView
,我一直停留了一段時間。RestKit在RKEntityMapping線
這是我簡單的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:],
感謝您的回答。你介意在第二部分給我一點細節。我對此很陌生,並且我正在關注Gist教程,它似乎並不需要額外的代碼。 – Mika
哪個教程(給一個鏈接)。您需要創建託管對象庫和上下文作爲絕對最小值。 – Wain
https://github.com/RestKit/RKGist/blob/master/TUTORIAL.md – Mika