alamofire对象映射器..为什么打印我的项目名称?

时间:2017-04-18 11:19:35

标签: swift alamofire mapper objectmapper

Screenshot Here

注意:这是我的第一个swift文件

override func viewDidLoad() {
    super.viewDidLoad()

    let encodingURL = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

    Alamofire.request(encodingURL!).responseObject { (response: DataResponse<channel>) in

        let MovieResponse = response.result.value

        print("결과: \(MovieResponse?.channel?.result as AnyObject)")
        print("타이틀: \(MovieResponse?.channel?.title as AnyObject)")
        print("토탈카운트: \(MovieResponse?.channel?.totalCount as AnyObject)")
        print("전체:\(MovieResponse?.channel as AnyObject)")
        print("아이템: \(MovieResponse?.channel?.item as AnyObject)")
        print("배우:\(MovieResponse?.channel?.item.first?.actor as AnyObject)")

    }
}

注意:第二个swift文件

import Foundation
import ObjectMapper

class channel : Mappable {
    var channel : channelComponents?
    required init?(map: Map){ }

    func mapping(map: Map) {
        self.channel <- map["channel"]    
    }
}

class channelComponents : Mappable {
    var totalCount : Int?
    var result : Int?
    var title : String?
    var item : [item]

    required init?(map: Map){
       item = []       
    }

    func mapping(map: Map) {          
        self.totalCount <- map["totalCount"]
        self.result <- map["result"]
        self.title <- map["q"]
        self.item <- map["item"]  
    }
}

class item : Mappable { 
    var actor : [Actor]

    required init?(map: Map) {       
        actor = []        
    }

    func mapping(map: Map) {        
        self.actor <- map["actor"]     
    }
}

class Actor : Mappable {
    var content : String?
    var link : String?

    required init?(map: Map) { }

    func mapping(map: Map) {
        self.content <- map["content"]
        self.link <- (map["link"])       
    }
}

输入

 print("아이템: \(MovieResponse?.channel?.item as AnyObject)")
 print("배우:\(MovieResponse?.channel?.item.first?.actor as AnyObject)")

输出

아이템: (
    "Movie_D.item"
)
배우:(
"Movie_D.Actor",
"Movie_D.Actor",
"Movie_D.Actor",
"Movie_D.Actor",
"Movie_D.Actor",
"Movie_D.Actor",
"Movie_D.Actor"
)

Movie_D是我的项目名称。

我想知道演员的名字,但只有我的项目名称。 (伯爵是对的)

1 个答案:

答案 0 :(得分:0)

您应该将actor属性转换为String(假设它是1),以便在print语句中正确显示actor的名称。

print("배우:\(String(describing: MovieResponse?.channel?.item.first?.actor))")