1

I have edited the question to focus on the last error. This is the last error in my code: an Array extension to remove object by value

It seems to be related to this post too:

Array extension to remove object by value

But I am stuck :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
    }

// for the 'removeObject' an error is raised:

Use of unresolved identifier 'removeObject'

The function is calling the framework Foundation through a Array+RemoveObject.swiftfile which states:

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

I am not sure my workspace is properly understanding that he needs to refer to this swift file to find the details of the identified removeObject.

Community
  • 1
  • 1
Jeremy
  • 13
  • 6
  • Make sure to include all of the relevant code to *help others help you* -- right now the pieces that are causing you errors are not included in your code snippets. The error is saying that you cannot cast `PFQueryArrayResultBlock` to whatever `updateList` is. I can't find the definition for `updateList` anywhere in the included code so it's tough to help. The other error saying unresolved identifier `removeObject` is the same thing. Where is `removeObject` defined? The compiler can't find it and that's what's causing the error – Russell Nov 04 '15 at 23:00
  • You are totally right, and thank you for your comment. I looked for the 'updateList' and discovered that it was referring to AnyObject and not PFObject. I made the change and it solved the three errors!!! I still have the last one. It refers to the framework "Foundation" where: `import Foundation extension RangeReplaceableCollectionType where Generator.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) } } }` – Jeremy Nov 04 '15 at 23:28
  • A suggested version for Swift2 is: `extension Array { mutating func removeObject(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 } } }` But how to edit the code – Jeremy Nov 05 '15 at 00:24
  • @Jeremy add it to the end of your array. Ex: followingUsers.removeObject(user) – Leo Dabus Nov 05 '15 at 00:47
  • @LeoDabus thank you for your help. I have tried but now I get a new error based on the PFObject value: `// update local cache followingUsers.removeObject(user) self.followingUsers = followingUsers` Gives two errors `Value of type '[PFUser]?' has no member 'removeObject'`and `Assigning a property to itself` – Jeremy Nov 05 '15 at 14:10
  • Thats because PFUser does not conform to Equatable protocol – Leo Dabus Nov 05 '15 at 14:58

1 Answers1

0

PFUser does not conform to Equatable protocol so your extension doesn't apply. But a PFUser is identified through its user name. You can solve your problem with filter, no extension required:

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
    }
}
Code Different
  • 90,614
  • 16
  • 144
  • 163