Removing object from array in swift 3

2019-02-07 15:51发布

In my application i used tableview. I added one object in array when select cell and unselect and remove object when re-select cell. i used that code but give me error. Please help me

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)
            }
        }
    }

It gives me 3 error like that

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'

9条回答
Animai°情兽
2楼-- · 2019-02-07 16:20

For Swift 3, you can use index(where:) and include a closure that does the comparison of an object in the array ($0) with whatever you are looking for.

var array = ["alpha", "beta", "gamma"]
if let index = array.index(where: {$0 == "beta"}) {
  array.remove(at: index)
}
查看更多
孤傲高冷的网名
3楼-- · 2019-02-07 16:21

In Swift 3, Use this Extention:

extension Array where Element: Equatable{
    mutating func remove (element: Element) {
        if let i = self.index(of: element) {
            self.remove(at: i)
        }
    }
}

example:

var array = ["alpha", "beta", "gamma"]
array.remove(element: "beta")
查看更多
乱世女痞
4楼-- · 2019-02-07 16:22
var a = ["one", "two", "three", "four", "five"]

// Remove/filter item with value 'three'
a = a.filter { $0 != "three" }
查看更多
我命由我不由天
5楼-- · 2019-02-07 16:29

The Swift (3) equivalent to NSMutableArray's removeObject is

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

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

if the objects are unique. There is no need at all to cast to NSArray and use indexOfObject:

If there are multiple occurrences of the same object use filter. However in cases like data source arrays where an index is associated with a particular object index(of is preferable because it's faster than filter.

查看更多
贪生不怕死
6楼-- · 2019-02-07 16:38

The correct and working one-line solution for deleting a unique object (named "objectToRemove") from an array of these objects (named "array") in Swift 3 is:

if let index = array.enumerated().filter( { $0.element === objectToRemove }).map({ $0.offset }).first {
   array.remove(at: index)
}
查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-02-07 16:39

Swift 4

var students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]

if let index = students.firstIndex(where: { $0.hasPrefix("A") }) {
   students.remove(at: index)
}
查看更多
登录 后发表回答