在我的應用程序中,我使用了tableview。在選擇單元格時,我在數組中添加了一個對象,並在重新選擇單元格時取消選擇和刪除對象。我使用該代碼,但給我錯誤。請幫我從swift中刪除對象3
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let cell = tableView.cellForRow(at: indexPath as IndexPath) as? ConctactsCell
cell?.imgCheckMark.image = UIImage(named:"check_mark")
arrContacts.append(contacts[indexPath.row])
NSLog("selected code = %@",arrContacts);
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath as IndexPath) as? ConctactsCell
cell?.imgCheckMark.image = UIImage(named:"")
arrContacts.removeObject(contacts[indexPath.row])
}
extension Array {
func indexOfObject(object : AnyObject) -> NSInteger {
return (self as NSArray).indexOfObject(object)
}
mutating func removeObject(object : AnyObject) {
for var index = self.indexOfObject(object); index != NSNotFound; index = self.indexOfObject(object) {
self.removeAtIndex(index)
}
}
}
它給了我3錯誤一樣,
Declaration is only valid at file scope
C-style for statement has been removed in Swift 3
Value of type '[Any]' has no member 'removeObject'
提供顯示你怎麼樣聲明聯繫人數組,你去哪兒宣佈它 –
把觸點陣列 –
的申報代碼多一點代碼,你可以使用'設置',而不是一個數組。你能提供關於你的聯繫對象的更多信息嗎?如果你自己創造了它,你將需要它符合'Hashable'和'Equatable',以便將其放入一組。 –
Paulw11