Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing object from array in Swift 3

In my application 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.

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)         }     } }  class MyViewController: UITableViewController {     var arrContacts: [Any] = []     var contacts: [Any] = []      func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {         arrContacts.removeObject(contacts[indexPath.row])     } } 

It gives me 2 error like that:

C-style for statement has been removed in Swift 3 Value of type '[Any]' has no member 'removeObject' 
like image 726
Kamlesh Shingarakhiya Avatar asked Nov 29 '16 06:11

Kamlesh Shingarakhiya


People also ask

How to delete all elements of an array in Swift?

We can remove all elements of an array that satisfy a given predicate in Swift by using the removeAll(where:) method.

How do you delete an object in Swift?

Helpfully, managed object context has a matching delete() method that will delete any object regardless of its type or location in the object graph. Once an object has been deleted from the context, we can then call saveContext() to write that change back to the persistent store so that the change is permanent.

How do you delete an array in Swift?

To remove an element from the Swift Array, use array. remove(at:index) method. Remember that the index of an array starts at 0. If you want to remove ith element use the index as (i-1).

How do I filter an array in Swift?

To filter an array in Swift: Call the Array. filter() method on an array. Pass a filtering function as an argument to the method.


1 Answers

The Swift equivalent to NSMutableArray's removeObject is:

var array = ["alpha", "beta", "gamma"]  if let index = array.firstIndex(of: "beta") {     array.remove(at: index) } 

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

The API index(of: also works but this causes an unnecessary implicit bridge cast to NSArray.

Of course you can write an extension of RangeReplaceableCollection to emulate the function. But due to value semantics you cannot name it removeObject.

extension RangeReplaceableCollection where Element : Equatable {     @discardableResult     mutating func remove(_ element : Element) -> Element?     {         if let index = firstIndex(of: element) {             return remove(at: index)         }         return nil     } } 

Like remove(at: it returns the removed item or nil if the array doesn't contain the item.


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 firstIndex(of is preferable because it's faster than filter.

Update:

In Swift 4.2+ you can remove one or multiple occurrences of beta with removeAll(where:):

array.removeAll{$0 == "beta"} 
like image 80
vadian Avatar answered Sep 18 '22 17:09

vadian