gpt4 book ai didi

ios - iOS上的RestKit-嵌套对象的后续API调用?

转载 作者:行者123 更新时间:2023-12-01 16:56:29 25 4
gpt4 key购买 nike

我正在使用RestKit将数据从Foursquare API提取到我的iPhone应用程序中,但是在后续API调用嵌套对象时遇到了麻烦。

特别:

  • 我调用Venues搜索API(https://developer.foursquare.com/docs/venues/search)来检索Venues列表。每个场所都有一个唯一的ID,该ID包含在响应中。我在ViewController中执行以下操作:
    [objectManager loadObjectsAtResourcePath:[NSString stringWithFormat:@"%@?%@", [URL resourcePath], [URL query]] delegate:self]; 
  • 然后我进行RestKit映射等操作,并将Venues存储在数据数组中。到这里一切正常。
  • 此时,我遍历数据数组中的Venue,并必须进行后续API调用以检索有关每个Venue的更多详细信息。对于每个场所,我使用唯一的ID并调用场所详细信息API(https://developer.foursquare.com/docs/venues/venues)。这是让我难过的部分。我正在尝试从第二个API调用返回一个NSArray of Photo对象。到目前为止,我已经尝试了以下方法:
    for (id venue in self.data){
    Venue *myvenue = (Venue *)venue;
    RKURL *URL = [RKURL URLWithBaseURL:[objectManager baseURL] resourcePath:[NSString stringWithFormat:@"/venues/%@", myvenue.venueid] queryParameters:queryParams];
    [objectManager loadObjectsAtResourcePath:[NSString stringWithFormat:@"%@?%@", [URL resourcePath], [URL query]] delegate:self];
    }

    在我的映射中:
    RKObjectMapping *photosMapping = [RKObjectMapping mappingForClass:[Photo class]];
    [photosMapping mapKeyPathsToAttributes:@"url", @"imageURL", nil];
    [venueMapping mapRelationship:@"photos" withMapping:photosMapping];
    [objectManager.mappingProvider setMapping:photosMapping forKeyPath:@"response.photos"]; // not sure if this keypath is for the second API call

    我的Venue class 有:
    @property (strong, nonatomic) NSArray *photos;

  • Venue.photos始终返回空白。有什么建议?

    最佳答案

    从您发布的代码中,您没有将响应链接到场所的任何地方。我不确定foursquare返回的确切JSON,但是您将需要构思一种使用带有嵌套照片对象的场所映射进行映射的方法。如果foursquare在响应中未返回对原始Venue对象的引用,则可以尝试将RKObjectLoader的targetObject属性设置为Venue(代码中的myVenue)

    关于ios - iOS上的RestKit-嵌套对象的后续API调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11346076/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com