在AlamoFire框架中访问映射对象将返回nil值

时间:2018-03-24 01:21:02

标签: ios swift networking alamofire graphql

我正在使用基于Alamofire框架构建的Swift Wrapper,通过它的graphQL API从Yelp返回业务数据。该调用返回一个JSON对象,然后将其映射到“CDYelpBusiness”类型的对象中,并且我能够访问其所有属性,保存数小时,类型为[CDYelpHour]?。它总是返回零,我确信这是由于对我的误解,但我不确定是什么。这是我的电话:

    yelpAPIClient.cancelAllPendingAPIRequests()
    yelpAPIClient.searchBusinesses(byTerm: "Martinis",
                                 location: nil,
                                 latitude: MapCenter.shared.latitude,
                                longitude: MapCenter.shared.longitude,
                                   radius: 16000,
                               categories: nil,
                                   locale: nil,
                                    limit: 20,
                                   offset: 0,
                                   sortBy: nil,
                               priceTiers: nil,
                                  openNow: false,
                                   openAt: nil,
                               attributes: nil) { (response) in

                                performUIUpdatesOnMain {

                                    if let response = response,
                                        let businesses = response.businesses,
                                        businesses.count > 0 {

                                        for business in businesses {
                                        Location.businesses.append(business)
                                            print(business.hours?[0].isOpenNow)

                                            let imageString = business.imageUrl?.absoluteString

                                            YelpClient.sharedInstance().loadImage(imageString, completionHandler: { (image) in
                                                Location.locationImages.append(image)
                                            })
                                        }

                                    }

这是响应映射到的CDYelpBusiness对象:

public class CDYelpBusiness: Mappable {

public var id: String?
public var name: String?
public var imageUrl: URL?
public var isClosed: Bool?
public var url: URL?
public var price: String?
public var phone: String?
public var displayPhone: String?
public var photos: [String]?
public var hours: [CDYelpHour]?
public var rating: Double?
public var reviewCount: Int?
public var categories: [CDYelpCategory]?
public var distance: Double?
public var coordinates: CDYelpCoordinates?
public var location: CDYelpLocation?
public var transactions: [String]?

public required init?(map: Map) {
}

public func mapping(map: Map) {
    id              <- map["id"]
    name            <- map["name"]
    imageUrl        <- (map["image_url"], URLTransform())
    isClosed        <- map["is_closed"]
    url             <- (map["url"], URLTransform())
    price           <- map["price"]
    phone           <- map["phone"]
    displayPhone    <- map["display_phone"]
    photos          <- map["photos"]
    hours           <- map["hours"]
    rating          <- map["rating"]
    reviewCount     <- map["review_count"]
    categories      <- map["categories"]
    distance        <- map["distance"]
    coordinates     <- map["coordinates"]
    location        <- map["location"]
    transactions    <- map["transactions"]
}
}

这是CDYelpHour对象:

public class CDYelpHour: Mappable {

public var hoursType: String?
public var open: [CDYelpOpen]?
public var isOpenNow: Bool?

public required init?(map: Map) {
}

public func mapping(map: Map) {
    hoursType   <- map["hours_type"]
    open        <- map["open"]
    isOpenNow   <- map["is_open_now"]
}
}

我可以在for循环中更改我的print语句以提取其他属性,例如使用business.id或business.name,但尝试获取hours属性不起作用。

我正在尝试显示该商家是否“现在开放”。虽然原始网络呼叫将此作为参数,但它仅用于过滤当前打开的企业的响应 - 设置为true时。设置为false时,作为默认值,它应该不返回该参数的过滤器。虽然这似乎也不起作用。

这是框架:

https://github.com/chrisdhaan/CDYelpFusionKit

1 个答案:

答案 0 :(得分:0)

好吧,让我自己更好地阅读文档。

CDYelpFusionKit框架设计用于Yelp的RESTful API,而不是graphQL API,可以让企业在原始的单一网络呼叫中开放营业时间。

虽然CDYelpBusiness类确实包含一个小时的变量,但必须在单独的网络调用中检索这些变量。

美好时光。