我的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"]
}
}
MyViewController
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)
如果我在任何地方犯错,我会请你给我一个完整的例子。
答案 0 :(得分:0)
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();
}
}
}
}
更新Pod并清理构建文件夹即可解决我的问题。 希望对您有帮助!