我使用Alamofire來創建GET請求,並使用ObjectMapper庫將響應轉換爲Swift中自己的類。將字符串轉換爲Swift中的類
Alamofire.request(self.REST_METHOD!, self.REQUEST_URL, headers : ["Authentication_Token" : authToken]).responseJSON { response in
if response.response != nil && response.response?.statusCode == 200 {
let json = JSON((response.result.value as? NSDictionary)!)
let classType : AnyObject.Type = NSClassFromString(entityType)! as AnyObject.Type
//let model = Mapper<classType>.map(json.rawString())
}
}
的的EntityType變量可以是多種類型,即用戶,學生,教師和工作人員,等我想動態創建一個類類型,並基於該模型之一。
但是它崩潰上給出錯誤信息行let classType : AnyObject.Type = NSClassFromString(entityType)! as AnyObject.Type
:
fatal error: unexpectedly found nil while unwrapping an Optional value
此外,當我取消對該行let model = Mapper<classType>.map(json.rawString())
,它給了我一個編譯器錯誤:
classType is not a type
什麼是錯的以上代碼
爲什麼你認爲這段代碼有問題?你期望發生什麼?究竟發生了什麼? –
對不起,添加更多詳情 –
什麼是entityType? – Roee84