swift2 ObjectMapper未解析标识符'Map器'

q0qdq0h2  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(191)

我的taskModel类文件

import Foundation
import ObjectMapper

// Mappeable source code
public protocol Mappable {
    init?(_ map: Map)
    mutating func mapping(map: Map)
}

// Conform to the Mappable protocol
class taskModel: Mappable {
    var taskid: Int = 0
    var parentId: Int = 0
    var taskname : String = ""
    var createdBy : String = ""
    var startDate : String = ""
    var taskStatus  : String = ""
    var Color : String = ""

    init(){

    }
    // MARK: Mappable
    required init?(_ map: Map) {
        // subClasses must call the constructor of the base class
        // super.init(map)
    }

    // Mappable
   func mapping(map: Map) {
        taskid    <- map["username"]
        parentId         <- map["age"]
        taskname      <- map["weight"]
        createdBy       <- map["arr"]
        startDate  <- map["dict"]
        taskStatus  <- map["best_friend"]
        Color     <- map["friends"]

    }
}

我的视图控制器

let taskJson = "{\"\"success\": true,\"\"messages\": \"successfully\",\"\"data\": [\"\"{\"\"\"\"taskId\": \"264\",\"\"\"\"parentId\": null,\"\"\"\"taskName\": \"order\",\"\"\"\"createdBy\": \"xyz\",\"\"\"\"assignedto\": \"xyz\",\"\"\"\"startDate\": \"23/11/2016\",\"\"\"\"taskStatus\": \"Open\",\"\"\"\"Color\": \"#6bea66\"\"\"}\"]}";

let repo = Mapper<taskModel>().map(taskJson)

我尝试导入ObjectMapper,然后它显示,

我会要求你请给予我一个完整的例子,如果我做的错误的地方。

fykwrbwg

fykwrbwg1#

import Foundation
import Alamofire
import ObjectMapper

class LiveNewsInterector: PresentorToInterectorProtocol{

    var presenter: InterectorToPresenterProtocol?;

    func fetchLiveNews() {
        Alamofire.request(Constants.URL).responseJSON { response in

            if(response.response?.statusCode == 200){
                if let json = response.result.value as! AnyObject? {
                    let arrayResponse = json["articles"] as! NSArray
                    let arrayObject = Mapper<LiveNewsModel>().mapArray(JSONArray: arrayResponse as! [[String : Any]]);
                    self.presenter?.liveNewsFetched(news: (arrayObject?[0])!);
                }
            }
            else {
                self.presenter?.liveNewsFetchedFailed();
            }
        }
    }
}

I get this from here
更新Pods和清理构建文件夹解决了我的问题。希望这对你有帮助!!

相关问题