Removing object from array in Swift 3

前端 未结 13 2108
余生分开走
余生分开走 2020-12-08 12:49

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.

extens         


        
13条回答
  •  星月不相逢
    2020-12-08 13:12

    This is official answer to find index of specific object, then you can easily remove any object using that index :

    var students = ["Ben", "Ivy", "Jordell", "Maxime"]
    if let i = students.firstIndex(of: "Maxime") {
         // students[i] = "Max"
         students.remove(at: i)
    }
    print(students)
    // Prints ["Ben", "Ivy", "Jordell"]
    

    Here is the link: https://developer.apple.com/documentation/swift/array/2994720-firstindex

提交回复
热议问题