我想在自定義類型數組中插入一個元素。但是我擺脫了索引錯誤。以下是我的代碼:在Swift 3.0中自定義類型數組插入崩潰
var newCGList = [TTCareGiverItem]()
if let snapshotItems = snapshot.value as? [String:Any]{
for (_, snapshotData) in snapshotItems {
let dict = snapshotData as? NSDictionary
if let cgList = dict?.value(forKey: "ul") as? NSDictionary {
for cg in cgList{
let cgKey = cg.key as! String
for ctr in 0 ..< self.careGiverList.count{
let key = self.careGiverList[ctr].id as String
if key == cgKey{
let order: Int = Int(cg.value as! String)!
let aCG = self.careGiverList[ctr]
aCG.order = order
newCGList.insert(aCG, at: ctr)
break
}
}
}
}
}
我缺少什麼?
你需要使用的是'append(_ :)'not'insert(_:at)' –
我需要在順序列表中插入append不起作用。 – TechBee
@TechBee爲什麼你使用插入而不是追加? –