Swift2:按值删除对象的数组扩展 - Foundation

标签 swift parse-platform swift2

我编辑了问题以关注最后一个错误。这是我的代码中的最后一个错误:按值删除对象的数组扩展

好像也和这篇文章有关:

Array extension to remove object by value

但是我卡住了:x

func cell(cell: FriendSearchTableViewCell, didSelectUnfollowUser user: PFUser) {
    if var followingUsers = followingUsers {
        ParseHelper.removeFollowRelationshipFromUser(PFUser.currentUser()!, toUser: user)
        // update local cache
        removeObject(user, fromArray: &followingUsers)
        self.followingUsers = followingUsers
    }

//对于 'removeObject' 会引发错误:

Use of unresolved identifier 'removeObject'

该函数通过 Array+RemoveObject.swift 文件调用框架 Foundation,该文件声明:

import Foundation

// Thanks to Martin R: https://stackoverflow.com/questions/24938948/array-extension-to-remove-object-by-value

extension Array where Element : Equatable {
  // Remove first collection element that is equal to the given `object`:
mutating func removeObject(object : Generator.Element) {
        if let index = self.indexOf(object) {
            self.removeAtIndex(index)
        }
    }
}

我不确定我的工作区是否正确理解他需要引用这个 swift 文件来查找已识别的 removeObject 的详细信息。

最佳答案

PFUser 不符合 Equatable 协议(protocol),因此您的扩展不适用。但是 PFUser 是通过其用户名来识别的。您可以使用filter 解决您的问题,无需扩展:

func cell(cell: FriendSearchTableViewCell, didSelectUnfollowUser user: PFUser) {
    if var followers = followingUsers {
        ParseHelper.removeFollowRelationshipFromUser(PFUser.currentUser()!, toUser: user)
        // update local cache
        followers = followers.filter { $0.username != user.username } 
        self.followingUsers = followers
    }
}

关于Swift2:按值删除对象的数组扩展 - Foundation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33532978/

相关文章:

ios - Swift 中的子类不继承其父类(super class)的初始化程序

ios - 在 swift 5 中设置 UITextField 底部边框的问题

swift - 尝试保存 PFUser 后解析已销毁的 session

parse-platform - 您可以查看 parse.com 云代码的历史日志吗?

ios - NavigationItem titleView 出现在左侧一段时间,然后移至中心

ios - TableView 重新加载未从 Backendless 正确更新

Swift - 加载 LocalDataStore 时解析 PFQueryTableViewController 错误

ios - 我可以使用自定义排序逻辑在 Swift 中对数组进行排序吗

Swift 2.1 - NSDateFormatter 奇怪的错误

ios - Swift 和 Foundation 的关系