迅速不给嵌套模型赋值

时间:2018-09-03 09:43:22

标签: ios swift objectmapper

尝试为模型对象分配值,但返回nil

我的模特:

struct Request:Mappable{


    var  id:String="" //
    var  origin:Location? 

}
  mutating func mapping(map: Map) {


        id <- map["id"]//
        origin <- map["origin"] //

}


struct Location :Mappable{

    var address=Address()


    mutating func mapping(map: Map) {

        address <- map["address"]

    }


var request=Request()
request.origin?.address.province?.id = "test" // assign nil

1 个答案:

答案 0 :(得分:2)

假设您具有key1 = {key[0] for key in my_dictionary} Address类型,如下所示,

Province

然后为省struct Address { var province: Province? } struct Province { var id: String? } 分配一个值,您需要按如下所示设置id

request