2013-10-08 64 views
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 

我在我的視圖控制器下面的代碼。在viewDidLoadRKEntityMapping導致異常:

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

回答

0

responseDescriptorWithMapping已過時,它現在有一個額外的method參數。這只是一個警告 - 你應該更新,但你不必。

如果第一次嘗試使用managedObjectStore時發生崩潰,則表明您沒有正確/完全配置Core Data堆棧(或者您的實體名稱/類名稱錯誤)。查詢this doc,瞭解需要什麼設置的簡單概述。如果您想使用對象管理器,請檢查this doc

+0

感謝您的回答。你介意在第二部分給我一點細節。我對此很陌生,並且我正在關注Gist教程,它似乎並不需要額外的代碼。 – Mika

+0

哪個教程(給一個鏈接)。您需要創建託管對象庫和上下文作爲絕對最小值。 – Wain

+0

https://github.com/RestKit/RKGist/blob/master/TUTORIAL.md – Mika