0
我有一個重複約8-9次的函數,我試圖減少冗餘。是否可以創建一個函數,該函數採用spefic類型並返回一個數組使用發送的json對象的initlizated類型。只傳遞特定類型作爲函數的參數
電流功能
static func initArray(json: JSON)-> [Event]{
var array = [Event]()
json.forEach(){
array.append(Event.init(json: $0.1))
}
return array
}
所需的功能
static func initArray<T> (type: T, json: JSON)-> [T]{
var array = [T]()
//I get stuck here im not too sure how to initlize the type
//Thats why im wondering if it's possible to pass speific types
//to the function
return array
}