gpt4 book ai didi

ios - 如何使用 AlamoFire/ObjectMapper 访问嵌入在 JSON 字典中的链接?

转载 作者:行者123 更新时间:2023-11-30 12:50:11 24 4
gpt4 key购买 nike

这是我正在使用的对象映射器:

ObjectMapper/AlamoFireObjctMapper

这是我正在使用的 JSON 数据,我正在尝试访问“链接”值:

channels =     (
{
social = {
facebook = {
"facebook_id" = 47360808996;
link = "https://www.facebook.com/CBS";
};
twitter = {
link = "https://twitter.com/CBS";
"twitter_id" = 97739866;
}
}
}
)

我创建了自定义对象来表示 JSON 字典的每个级别:

class SocialInfo: Mappable {

var channels: Social?

required init?(map: Map) {
}

func mapping(map: Map) {

channels <- map["channels"]

}
}

class Social: Mappable {

var facebookSocial: Facebook?
var twitterSocial: Twitter?


required init?(map: Map) {
}

func mapping(map: Map) {

facebookSocial <- map["facebook"]
twitterSocial <- map["twitter"]

}

}


class Facebook: Mappable {

var facebookLink: NSURL?

required init?(map: Map) {
}

func mapping(map: Map) {

facebookLink <- (map["link"], URLTransform())

}
}

最初,我将“Facebook”类下的“facebookLink”作为字符串,但它一直返回零。然后我尝试使用 URLTransform 将其更改为 NSURL 类型,但现在它抛出错误:

Type of expression is ambiguous without more context

以下是我尝试检索 JSON 数据的方法:

 func getSocialInfo (completionHandler: @escaping (SocialInfo?, Error?) -> ()){

Alamofire.request("\(baseURL)/\(apiKey)/show/950", method: .get, encoding: JSONEncoding.default).validate ()
.responseObject{ (response: DataResponse<SocialInfo>) in

switch response.result {
case .success:
let socialInfo = response.result.value

print("This is the social info: \(socialInfo?.channels?.facebookSocial?.facebookLink)")


completionHandler(socialInfo!, nil)

case .failure(let error):

print("Sorry there was an error: \(error)")
completionHandler(nil,error)
return
}

}
}

最佳答案

尝试将链接设置为字符串

var stringURL:String?

获取链接

stringURL <- map["link"]

stringURL转换为URL

if let url = URL(string: stringURL) {
// do something
}

另请注意,这不会起作用,因为您特别指出 facebookDictionary is = 到您的 Facebook 对象。

 class Social: Mappable {

var facebookSocial: Facebook?
var twitterSocial: Twitter?

fileprivate var facebookDictionary:[AnyHashable:Any]?
fileprivate var twitterDictionary:[AnyHashable:Any]?
required init?(map: Map) {
}

func mapping(map: Map) {

facebookDictionary <- map["facebook"]
twitterDictionary <- map["twitter"]

if let _facebookDictionary = facebookDictionary,
let _twitterDictionary = wittertDictionary {

let twitterMap = Map(mappingType: .fromJSON, JSON: _twitterDictionary)
let facebookMap = Map(mappingType: .fromJSON, JSON: _facebookDictionary)

/// Here you can initiate both object using map.

facebookSocial = Facebook(map: facebookMap)
twitterSocial = Twitter(map: twitterMap)

/// If you want to use mapping function, set this as:

facebookSocial = Facebook()
twitterSocial.mapping(map: twitterMap)

facebookSocial = Facebook()
twitterSocial.mapping(map: twitterMap)
}

}

}

由于您没有使用社交词典创建新 map ,因此这两个对象将始终nil

社交相同,首先从社交字典创建一个 map

关于ios - 如何使用 AlamoFire/ObjectMapper 访问嵌入在 JSON 字典中的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41109526/

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