gpt4 book ai didi

json - 我如何从主视图调用可映射类中的变量

转载 作者:行者123 更新时间:2023-11-28 07:48:41 26 4
gpt4 key购买 nike

我有三个类,第一个是主视图,这是它的代码

    class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
getJSONS().getLowerJSON()

setupUI()

}
func setupUI(){
print(lowerData.companyName)
}
}

getsJSONS 调用一个调用 almofire 的函数来处理此代码中的 json

class getJSONS {
func getLowerJSON(){

let URL = "http://tickerchart.com/interview/company-details.json"
Alamofire.request(URL).responseObject { (response: DataResponse<lowereViewResponse>) in

let lowereViewResponse = response.result.value

}

}
}

lowereViewResponse 在此代码中调用可映射类

class lowereViewResponse: NSObject, Mappable {
var companyName : String?
var symbol : String?
var tradesCount : Int?
var high : Int?
var low : Int?
var volume : Int?
var amount : Int?

override init() {
super.init()
}

convenience required init?(_ map: Map) {
self.init()
}


func mapping(map: Map) {
companyName <- map["company-name"]
symbol <- map["symbol"]
tradesCount <- map["trades-count"]
high <- map["high"]
low <- map["low"]
volume <- map["volume"]

    required init?(map: Map){
companyName = ""
symbol = ""
tradesCount = 0
high = 0
low = 0
volume = 0
amount = 0
price = 0
}


}

所有导入完成

我正在尝试调用类 lowereViewResponse 来访问变量,但是当它退出“print(lowerData.companyName)”时我得到 null

最佳答案

您可以像下面这样使用:

let lowereViewObject: lowereViewResponse = Mapper< lowereViewResponse >().map(JSON: result)

关于json - 我如何从主视图调用可映射类中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50272898/

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