【问题标题】:How to delete specific NSObject in Array with Swift3如何使用 Swift3 删除 Array 中的特定 NSObject
【发布时间】:2017-01-26 02:46:46
【问题描述】:

我用朋友的联系信息制作了表格视图。
如果触摸每个单元格都有按钮,
我想将信息插入到选定的朋友数组
(通过数组,我又做了一个小视图与好友列表一起向上滑动)。
但如果再按一次按钮,
我想删除所选好友数组中的好友信息。
我知道如何追加到数组,
但我不知道如何擦除数组中的特定项目(NSObject) 通过不使用索引。

我的源代码在下面

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

}

在视图控制器类中,

  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{

        }

    }
}

【问题讨论】:

  • 你能用一个具体的问题更新你的帖子吗?
  • @BenjaminLowry 我编辑了它!谢谢
  • 您还应该为 FriendInfo 保存一个唯一标识符。否则您可以搜索它(来自@MirekE 的解决方案),以后不要对所选列表执行任何操作
  • 要将索引保存到 FriendInfo 你应该小心,因为这可以快速改变(使用你的代码)
  • @muescha 和 OP:我想 FriendInfo.index 是某种唯一 ID,而不是数组索引。您不需要在此处存储数组索引。但是您可能应该有某种唯一标识符,以确保不会多次将同一记录输入到数组中。你也可以使用 Set 而不是 Array。

标签: arrays swift xcode nsobject


【解决方案1】:

您可以使用index(where:) 获取对象的索引,然后使用remove 获取索引位置的项目。

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

【讨论】:

  • 感谢您的解决方案。但是你能举个例子吗?就像我想检查 FriendModel.thumbnali = "ask.png" 和 FriendModel.name = "Allen" 的索引
【解决方案2】:

您可以使用 MirekE 的解决方案,但这里有一个替代解决方案。

第一步是确定 FriendModel 对象上的唯一标识符是什么,例如 id 属性。

然后在你的模型上,因为它是一个 NSObject,像这样覆盖 isEqual 函数:

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

此时您可以使用所需的迭代形式在数组中找到您的元素并使用remove。

【讨论】:

    猜你喜欢
    • 2011-07-09
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    • 2016-11-15
    • 2017-12-06
    • 1970-01-01
    相关资源
    最近更新 更多