【发布时间】:2015-11-04 22:06:40
【问题描述】:
我已编辑问题以关注最后一个错误。这是我的代码中的最后一个错误:按值删除对象的数组扩展
好像也和这个帖子有关:
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”,会引发错误:
使用未解析的标识符“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 的详细信息。
【问题讨论】:
-
确保包含所有相关代码以帮助他人帮助您——现在导致您出错的部分不包含在您的代码 sn-ps 中。错误是说您不能将
PFQueryArrayResultBlock转换为updateList是什么。我在包含的代码中的任何地方都找不到updateList的定义,因此很难提供帮助。另一个错误说未解析的标识符removeObject是同一件事。removeObject在哪里定义?编译器找不到它,这就是导致错误的原因 -
您完全正确,感谢您的评论。我查找了“updateList”,发现它指的是 AnyObject 而不是 PFObject。我进行了更改,它解决了三个错误!!!我还有最后一个。它指的是框架“基础”,其中:
import Foundation extension RangeReplaceableCollectionType where Generator.Element : Equatable { // Remove first collection element that is equal to the givenobject: mutating func removeObject(object : Generator.Element) { if let index = self.indexOf(object) { self.removeAtIndex(index) } } } -
Swift2 的建议版本是:
extension Array { mutating func removeObject<T: Equatable>(object: T) -> Bool { var index: Int? for (idx, objectToCompare) in self.enumerate() { if let toCompare = objectToCompare as? T { if toCompare == object { index = idx break } } } if(index != nil) { self.removeAtIndex(index!) return true } else { return false } } }但是如何编辑代码 -
@Jeremy 将其添加到数组的末尾。例如:followingUsers.removeObject(user)
-
@LeoDabus 感谢您的帮助。我已经尝试过,但现在我得到了一个基于 PFObject 值的新错误:
// update local cache followingUsers.removeObject(user) self.followingUsers = followingUsers给出两个错误Value of type '[PFUser]?' has no member 'removeObject'和Assigning a property to itself
标签: swift parse-platform swift2