使用字典轉(zhuǎn)模型時(shí)報(bào)錯(cuò)
由于XYSpecial模型中有一個(gè)屬性是另一個(gè)模型的數(shù)組忿等,我在自定義構(gòu)造函數(shù)中使用setValuesForKeys(dict)進(jìn)行字典轉(zhuǎn)模型栖忠,導(dǎo)致報(bào)錯(cuò)
NSArray element failed to match the Swift Array Element type
解決方法:由于未使用第三方字典轉(zhuǎn)模型框架,我將字典中所有的value單獨(dú)賦值給模型的屬性就解決了, 或者使用KVC一個(gè)一個(gè)轉(zhuǎn)換也可以setValue(<#T##value: Any?##Any?#>, forKey: <#T##String#>)
class XYSpecial: NSObject {
// MARK:- 屬性
var scId : Int?
var title : String? // 專題活動(dòng)標(biāo)題
var subTitle : String? // 專題活動(dòng)子標(biāo)題
var desc : String? // 專題活動(dòng)描述
var smallPicUrl : String?
var largePicUrl : String?
var mainPicUrl : String? // 專題詳情頁的頭部圖片
var createTime : String? // 創(chuàng)建時(shí)間
var pubTime : String? // 出版時(shí)間
var weight : Int?
var hitCount : Int?
var hasDel : Int?
var threadId : Int?
var chosen : Int?
var itemList : [XYArticleItem] = [XYArticleItem]() // 專題活動(dòng)的物品模型數(shù)組
// MARK:- 自定義構(gòu)造函數(shù)
init(dict: [String: Any]) {
super.init()
if let list = dict["itemList"] as? [[String : Any]] {
for obj in list {
let articleItem = (XYArticleItem(dict: obj))
itemList.append(articleItem)
}
}
self.scId = dict["scId"] as! Int?
self.title = dict["title"] as! String?
self.subTitle = dict["subTitle"] as! String?
self.desc = dict["desc"] as! String?
self.smallPicUrl = dict["smallPicUrl"] as! String?
self.largePicUrl = dict["largePicUrl"] as! String?
self.mainPicUrl = dict["mainPicUrl"] as! String?
self.createTime = dict["createTime"] as! String?
self.pubTime = dict["pubTime"] as! String?
self.weight = dict["weight"] as! Int?
self.hitCount = dict["hitCount"] as! Int?
self.hasDel = dict["hasDel"] as! Int?
self.threadId = dict["threadId"] as! Int?
self.chosen = dict["chosen"] as! Int?
}
}