0

I made table view with friends contact info.
And Each cell has button if touched,
I want to insert the info to selected friend array
(by the array, I made another small view to slide up with the friends list).
But If user the button one more,
I want to delete the friend Info in the selected friend array.
I know how to append to array,
but I don't know how to erase the specific item(NSObject) in array by not using index.

my source code is below

class FriendModel : NSObject {
   dynamic var index : ""
   dynamic var thumbnail : ""
   dynamic var name : "" 

}

and In view controller class,

  var selectedList = [FriendModel]()

@IBAction func SelectAct(_ sender: Any) {

    let chooseBtn = sender as! UIButton

    let indexPath = NSIndexPath(row: chooseBtn.tag, section:0)
    let cell = tableView.cellForRow(at: indexPath as IndexPath) as! FriendsListSendCell

    // when selected button is pushed
    if chooseBtn.isSelected == true {
        chooseBtn.isSelected = false
        count = count! - 1

        if self.count! < 1 {
            self.windowShowUp = false
            UIView.animate(withDuration: 0.1, delay: 0.1, options: [], animations:{self.selectedBoard.center.y += 80 }, completion: nil)
            self.checkNumLabel.text = ""
        }else{

        }
     //////////////////////////here////////////////////////////  
     //////////////////how to erase the FriendModel(NSObject) in selectedList.

    }
    //when the unselected button is pushed 
    else {

        //instance for append the friend info
        let friendInfo = FriendModel()


        chooseBtn.isSelected = true
        count = count! + 1

        friendInfo.thumbnail = cell.thumbnail
        friendInfo.name = cell.nameLabel.text!

        //add friend info to selectedList
        self.selectedList.append(friendInfo)
        print("\(self.selectedList)")



        if self.windowShowUp! == false{
            self.windowShowUp = true
            UIView.animate(withDuration: 0.1, delay: 0.1, options: [], animations:{self.selectedBoard.center.y -= 80 }, completion: nil)

        }else{

        }

    }
}
6
  • Can you update your post with a specific question? Commented Jan 26, 2017 at 2:59
  • @BenjaminLowry I edited it! thanks Commented Jan 26, 2017 at 3:09
  • you should save also an unique identifier to the FriendInfo. otherwise you can search for it (solution from @MirekE) and not do something later with the selected list Commented Jan 26, 2017 at 4:14
  • to save a index to FriendInfo you should do carefully, because this can changed fast (with your code) Commented Jan 26, 2017 at 4:15
  • @muescha and the OP: I suppose the FriendInfo.index is some sort of unique ID as opposed to the array index. You don't need to store array index here. But you should probably have some sort of unique identifier to ensure that the same record is not entered to the array more than once. You could also use a Set instead of an Array then. Commented Jan 27, 2017 at 6:57

2 Answers 2

3

You can use index(where:) to get index of your object and then remove item at index position.

class FriendModel {
    var index = ""
    var thumbnail = ""
    var name = ""
}

let fm0 = FriendModel()
fm0.index = "100"
fm0.thumbnail = "tell.png"
fm0.name = "Phillips"

let fm1 = FriendModel()
fm1.index = "200"
fm1.thumbnail = "ask.png"
fm1.name = "Allen"

var array = [FriendModel]()
array.append(fm0)
array.append(fm1)

// The index below is an index of the array. Do not confuse with the FriendModel.index
let index = array.index {
    return $0.thumbnail == "ask.png" && $0.name == "Allen"
}
array.forEach { print($0.name) }

print("Array index:", index ?? "n/a")

array.remove(at: index!)

array.forEach { print($0.name) }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your solution. but can you give an example use the method? like I want to check index of FriendModel.thumbnali = "ask.png"and FriendModel.name = "Allen"
1

You could use MirekE's solution which would work but here's an alternative solution.

First step would be identifying what the unique identifier on your FriendModel object is such as an id property.

Then on your model, since it is an NSObject, override the isEqual function like this:

override public func isEqual(object: AnyObject?) -> Bool {
    let friend = object as? FriendModel
    if self.id == friend?.id { return true }
    return false
}

At this point you can use your desired form of iteration to find your element in the array and use remove.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.