所以我有一个名为Response
的类class Response<T where T:NSObject>{
var message:String = ""
var result:T?
var status:Int = 0
init(setMessage message:String, setResult result:T?, setStatus status:Int) {
self.message = message;
self.result = result
self.status = status
}
required init(){
super.init()
}
init(JSON:AnyObject, result:T?) {
self.message = JSON["message"] as! String
self.result = result
self.status = JSON["status"] as! Int
}
}
我想做另一个init(),我将self.result的类型设置为NSArray或NSDictionary,我一直在试验一些代码,我似乎无法得到我想做的事情
我想做点像
init(JSON:AnyObject, resultType:T) {
self.message = JSON["message"] as! String
self.result = JSON["result"] as? T
self.status = JSON["status"] as! Int
}
然后从另一个类中调用它,如
let json = //some kind of JSON
Response(JSON: json, resultType:NSArray)
//or
Response(JSON: json, resultType:NSDictionary)