0
var globalCountArray = [AnyObject]()
var assetDictionary = [String:AnyObject]()
globalCountArray.append(assetDictionary as AnyObject)

How to filter dictionary using Anyobject array? I have tried this.

  globalCountArray = globalCountArray.filter {$0     as AnyObject != dict }

But I got error

Binary operator '!=' cannot be applied to operands of type 'AnyObject' and '[String : AnyObject]'"

8
  • globalCountArray.filter {$0 as? [String: AnyObject] != dict }? Commented Nov 15, 2017 at 10:50
  • why AnyObject array? You should specify type of array if it's a array having dictionaries then it should be var globalCountArray: [[String: Any]] = [[:]] Commented Nov 15, 2017 at 10:55
  • What exactly do you mean by filter? Can you be a bit more specific? Commented Nov 15, 2017 at 10:55
  • Check this :- stackoverflow.com/questions/42948755/… Commented Nov 15, 2017 at 10:57
  • if i change globalCountArray: [[String: Any]] = [[:]] like this then i got Binary operator '!=' cannot be applied to operands of type '[String : Any]' and '[String : AnyObject]' Commented Nov 15, 2017 at 11:06

2 Answers 2

1

Try using this

    var globalCountArray = [AnyObject]()
    var assetDictionary = [String:AnyObject]()
    globalCountArray.append(assetDictionary as AnyObject)
    let dict = [String:AnyObject]()




    globalCountArray = globalCountArray.filter({ (obj) -> Bool in

        if obj is[String:AnyObject] {

            return (obj as! [String:AnyObject]) != dict 


        }
        return false
    })

--------- OR You can achieve the same via ----------

globalCountArray = globalCountArray.filter({ (obj) -> Bool in

            if obj is[String:AnyObject] {

                return (obj as! [String:AnyObject]) == dict 


            }
            return true
        })

You need to add this method to outside your class definition.

public func !=(lhs: [String: AnyObject], rhs: [String: AnyObject] ) -> Bool {
    return !NSDictionary(dictionary: lhs).isEqual(to: rhs)
}

 public func ==(lhs: [String: AnyObject], rhs: [String: AnyObject] ) -> Bool {
    return NSDictionary(dictionary: lhs).isEqual(to: rhs)
}
Sign up to request clarification or add additional context in comments.

7 Comments

i got this errror 'AnyObject' is not convertible to '[String : AnyObject]'; did you mean to use 'as!' to force downcast?
i have tried this but i got error Binary operator '!=' cannot be applied to two '[String : AnyObject]' operands
Oh i Actually made for "==" for now, I have updated it for "!="
It will not deleted the same object but delete the other object . example suppose there are two dictionary A,B and i want to delete A but in actual it will delete B not A.. i think != is required.. But it gives error
I have used the "!=" as per your question. You need to choose "==" for this.
|
0

You can write an extension for Dictionary:

extension Dictionary where Key: ExpressibleByStringLiteral, Value: AnyObject {
    func isEqual(_ dictionary: [String: AnyObject]) -> Bool {
        return NSDictionary(dictionary: dictionary).isEqual(to: self)
    }

    func isNotEqual(_ dictionary: [String: AnyObject]) -> Bool {
        return !NSDictionary(dictionary: dictionary).isEqual(to: self)
    }
}

Then you can compare [String: AnyObject] dictionaries with:

FirstDictionary.isEqual(SecondDictionary)
FirstDictionary.isNotEqual(SecondDictionary)

In your case:

var globalCountArray = [AnyObject]()
var assetDictionary = [String:AnyObject]()
globalCountArray.append(assetDictionary as AnyObject)

globalCountArray = globalCountArray.filter { (($0 as! [String: AnyObject]).isNotEqual(assetDictionary)) }

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.