2016-11-29 53 views
25

在我的應用程序中,我使用了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' 
+0

提供顯示你怎麼樣聲明聯繫人數組,你去哪兒宣佈它 –

+1

把觸點陣列 –

+0

的申報代碼多一點代碼,你可以使用'設置',而不是一個數組。你能提供關於你的聯繫對象的更多信息嗎?如果你自己創造了它,你將需要它符合'Hashable'和'Equatable',以便將其放入一組。 – Paulw11

回答

41

雨燕(3)相當於的removeObjectNSMutableArray

var array = ["alpha", "beta", "gamma"] 

if let index = array.index(of:"beta") { 
    array.remove(at: index) 
} 

沒有必要在所有投給NSArray,並使用indexOfObject:

+11

這是最好的答案,但它不是愚蠢的,沒有刪除(object:「beta」)。 – zeeple

+2

我認爲'.index(':只有當集合包含'Equatable'類型時纔可用。 –

+0

@AdamWaite是的,但是這也適用於基礎類型。 – vadian

1

在斯威夫特試試這個3

array.remove(at: Index) 

而不是

array.removeAtIndex(index) 

更新

"Declaration is only valid at file scope". 

確保對象在範圍內。你可以給範圍「內部」,這是默認的。

index(of:<Object>)工作,類應符合Equatable

3
  1. 擴展聲明需要在類的範圍之外,將其移出

  2. for var index = self.indexOfObject(object); index != NSNotFound; index = self.indexOfObject(object)是在C型環和已被刪除

  3. 更改您的代碼是這樣的,除去所有類似的對象,如果它已經循環:

    let indexes = arrContacts.enumerated().filter { $0.element == contacts[indexPath.row] }.map{ $0.offset } 
    for index in indexes.reversed() { 
        arrContacts.remove(at: index) 
    } 
    
+0

enumerated - > filter - > map和remove(at)is smart解決方法推薦此文章 –

0

這是你如何從一個數組中刪除一個對象:

var array = ["1", "2", "3"] 
array.remove(at: 1) // removes the number 2 

您正在使用「contacts [indexPath.row]」作爲索引,這可能需要轉換爲數字嗎? 「arrContacts」可能是一本字典?

1

正確和工作單行溶液從這些對象的數組中刪除一個唯一的對象(名爲「objectToRemove」)在夫特3(命名爲「陣列」)是:

if let index = array.enumerated().filter({ $0.element === objectToRemove }).map({ $0.offset }).first { 
    array.remove(at: index) 
} 
3

的另一個優點和有用的解決方案是創建這種擴展的:

extension Array where Element: Equatable { 

    @discardableResult mutating func remove(object: Element) -> Bool { 
     if let index = index(of: object) { 
      self.remove(at: index) 
      return true 
     } 
     return false 
    } 

    @discardableResult mutating func remove(where predicate: (Array.Iterator.Element) -> Bool) -> Bool { 
     if let index = self.index(where: { (element) -> Bool in 
      return predicate(element) 
     }) { 
      self.remove(at: index) 
      return true 
     } 
     return false 
    } 

} 

這樣,如果你有自定義對象您的數組:

let obj1 = MyObject(id: 1) 
let obj2 = MyObject(id: 2) 
var array: [MyObject] = [obj1, obj2] 

array.remove(where: { (obj) -> Bool in 
    return obj.id == 1 
}) 
// OR 
array.remove(object: obj2) 
36
var a = ["one", "two", "three", "four", "five"] 

// Remove/filter item with value 'three' 
a = a.filter { $0 != "three" } 
+4

這是正確的Swift解決方案,它利用語言提供的語法好吃的東西。 – Michael

13

對於Swift 3,您可以使用index(where :)幷包含一個閉包,它將數組中的對象($ 0)與您正在尋找的任何對象進行比較。

var array = ["alpha", "beta", "gamma"] 
if let index = array.index(where: {$0 == "beta"}) { 
    array.remove(at: index) 
} 
+0

這個工作如果我想刪除多個對象嗎?(其中:{$ 0 ==「beta」|| $ 0 ==「gamma」}) –