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]'"

akshay
  • 765
  • 6
  • 20
  • `globalCountArray.filter {$0 as? [String: AnyObject] != dict }`? – Ozgur Vatansever Nov 15 '17 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]] = [[:]] – Suhit Patil Nov 15 '17 at 10:55
  • What exactly do you mean by filter? Can you be a bit more specific? – ebby94 Nov 15 '17 at 10:55
  • Check this :- https://stackoverflow.com/questions/42948755/binary-operator-cannot-be-applied-to-two-string-operands – Rahul Mayani Nov 15 '17 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]' – akshay Nov 15 '17 at 11:06
  • Why use different type and try casting? Cant u have your array type as `[String: AnyObject]` or atleast `Any`? – Tj3n Nov 15 '17 at 11:12
  • Do you want to filter array to contain only dictionaries of type [String:Any] or you want to filter array for a particular dictionary? – Puneet Sharma Nov 15 '17 at 11:15
  • particular dictionary – akshay Nov 15 '17 at 11:16

2 Answers2

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)
}
Sucharu Hasija
  • 1,096
  • 11
  • 23
  • i got this errror 'AnyObject' is not convertible to '[String : AnyObject]'; did you mean to use 'as!' to force downcast? – akshay Nov 15 '17 at 10:54
  • i have tried this but i got error Binary operator '!=' cannot be applied to two '[String : AnyObject]' operands – akshay Nov 15 '17 at 11:13
  • Oh i Actually made for "==" for now, I have updated it for "!=" – Sucharu Hasija Nov 15 '17 at 11:16
  • 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 – akshay Nov 15 '17 at 11:27
  • I have used the "!=" as per your question. You need to choose "==" for this. – Sucharu Hasija Nov 15 '17 at 11:29
  • Binary operator '!=' cannot be applied to two '[String : AnyObject]' operands – akshay Nov 15 '17 at 11:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/159055/discussion-between-sucharu-hasija-and-akshay). – Sucharu Hasija Nov 15 '17 at 11:31
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)) }
Roo
  • 613
  • 1
  • 7
  • 24