我如何从主视图调用mappable类中的变量

时间:2018-05-10 12:21:43

标签: json swift objectmapper

我有三个一级是主视图,这里是它的代码

    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按照此代码

调用mappable类
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来访问变量,但是当它退出时我得到null&#34; print(lowerData.companyName)&#34;

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

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