2013-06-11 51 views
0

我正在構建一個需要實現Foursquare搜索結果的iOS應用程序。 我使用RestKit,但繼續運行到了同樣的錯誤: 「無響應描述符匹配響應裝」RestKit - 使用CoreData映射實體 - 響應錯誤

這裏是我的代碼的相關部分:

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"https://api.foursquare.com/v2"]]; 
objectManager.managedObjectStore = managedObjectStore; 

[RKObjectManager setSharedManager:objectManager]; 

RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Venue" inManagedObjectStore:managedObjectStore]; 
[entityMapping addAttributeMappingsFromDictionary:@{ 
@"name":   @"name", 
@"id":   @"id", 
@"canonicalUrl": @"canonicalUrl"}]; 
entityMapping.identificationAttributes = @[@"id"]; 

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor 
              responseDescriptorWithMapping:entityMapping 
              pathPattern:@"/v2/venues/search" 
              keyPath:nil 
              statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

[objectManager addResponseDescriptor:responseDescriptor]; 

//Create strings for auth 
clientID = [NSString stringWithUTF8String:kCLIENTID]; 
clientSecret = [NSString stringWithUTF8String:kCLIENTSECRET]; 

[self loadVenues]; 

loadVenues方法:

NSString *currentLatLon = [NSString stringWithFormat:@"%1$f,%2$f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude]; 
NSDictionary *queryParams = [NSDictionary dictionaryWithObjectsAndKeys: 
          @"20130601",@"v", 
          currentLatLon,@"ll", 
          clientID,@"client_id", 
          clientSecret,@"client_secret", nil]; 
[[RKObjectManager sharedManager] 
getObjectsAtPath:@"https://api.foursquare.com/v2/venues/search" 
parameters:queryParams 
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
    //Do something 
    } 
failure:^(RKObjectRequestOperation *operation, NSError *error) { 
    //Do something else 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An Error Has Occurred" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alertView show]; 
}]; 

核心數據模型: enter image description here enter image description here

而這裏的,我試圖解析的那種JSON數據:

representation: { 
meta =  { 
    code = 200; 
}; 
response =  { 
    venues =   (
        { 
      canonicalUrl = "https://foursquare.com/v/union-square/40bbc700f964a520b1001fe3"; 
      categories =     (
            { 
        icon =       { 
         prefix = "https://foursquare.com/img/categories_v2/parks_outdoors/plaza_"; 
         suffix = ".png"; 
        }; 
        id = 4bf58dd8d48988d164941735; 
        name = Plaza; 
        pluralName = Plazas; 
        primary = 1; 
        shortName = "Plaza/Square"; 
       } 
      ); 
      contact =     { 
       formattedPhone = "(415) 781-7880"; 
       phone = 4157817880; 
       twitter = unionsquaresf; 
      }; 
      hereNow =     { 
       count = 8; 
       groups =      (
              { 
         count = 8; 
         items =        (
         ); 
         name = "Other people here"; 
         type = others; 
        } 
       ); 
      }; 
      id = 40bbc700f964a520b1001fe3; 
      location =     { 
       address = "Union Square Park"; 
       cc = US; 
       city = "San Francisco"; 
       country = "United States"; 
       crossStreet = "btwn Post, Stockton, Geary & Powell St."; 
       distance = 68; 
       lat = "37.787750172585"; 
       lng = "-122.4076282253645"; 
       postalCode = 94108; 
       state = CA; 
      }; 
      name = "Union Square"; 
      referralId = "v-1370991275"; 
      restricted = 1; 
      specials =     { 
       count = 0; 
       items =      (
       ); 
      }; 
      stats =     { 
       checkinsCount = 73242; 
       tipCount = 172; 
       usersCount = 39007; 
      }; 
      url = "http://visitunionsquaresf.com"; 
      venuePage =     { 
       id = 34303229; 
      }; 
      verified = 1; 
     }, 
+0

當您收到錯誤時會返回哪些JSON? – Wain

+0

@Wain剛添加它! – khaliq

回答

0

就看北斗星的建議後,我已經想通了。

在運行時RestKit無法找到我的responseDescriptor。 這是我改變讓我的responseDescriptor認可:

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor 
              responseDescriptorWithMapping:entityMapping 
              pathPattern:nil 
              keyPath:@"response.venues" 
              statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

改變這一點,並確保我用正確的路徑(!總是仔細檢查你的路)後,我發現我的鑰匙沒有被映射正常。

的問題是,我叫我的實體模型鍵「ID」之一。 「id」是一個系統關鍵字,並導致了大量時髦的編譯器錯誤。

我只好回到我的數據模型並調整我的屬性。

RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Venue" inManagedObjectStore:managedObjectStore]; 
[entityMapping addAttributeMappingsFromDictionary:@{ 
@"name":   @"name", 
@"id":   @"venueID", 
@"canonicalUrl": @"canonicalUrl"}]; 
entityMapping.identificationAttributes = @[@"venueID"]; 
2

當你撥打電話來獲取數據,使用相對路徑,而不是完整的URL:

[[RKObjectManager sharedManager] getObjectsAtPath:@"venues/search" ... 

同樣,你的響應描述路徑模式應符合:

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor 
             responseDescriptorWithMapping:entityMapping 
             pathPattern:@"venues/search" ... 
+0

這正是我試圖解決我眼前的問題。 我現在得到這個錯誤: ***終止應用程序由於未捕獲的異常'NSInvalidArgumentException',原因:' - [RKPropertyInspector classForPropertyNamed:ofEntity:]:無法識別的選擇器發送到實例0x12152690' – khaliq

+0

您需要調試,無法猜測原因。 – Wain