OzzY
OzzY

Reputation: 231

cannot convert value of type 'Anyobject!' to expected argument type 'Int'

I am trying to compare an Int with a JSON object but I encounter converting error. user_id is an Int and I also tried Int(sharedID) but it didn't work as well. So how can I compare sharedID with user_id Thank you.

 let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data:NSData?, response: NSURLResponse?, error:NSError?) -> Void in

        dispatch_async(dispatch_get_main_queue()) {

            if error != nil
            {
                return
            }

            do {
                let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary

                self.homeShareResults.removeAll(keepCapacity: false)

                self.shareTableView.reloadData()

                if let parseJSON = json {

                    if let friends  = parseJSON["checkSharedImage"] as? [AnyObject]
                    {
                        for friendObj in friends
                        {

                            let sharedID = (friendObj["id"])

                            if sharedID == user_id {...}

Upvotes: 0

Views: 2600

Answers (1)

Sandro
Sandro

Reputation: 84

Try a type cast any object.

if let sharedID = friendObj["id"] as? Int{
        if sharedID == user_id {...}
}

Or simply force cast as! Int

Upvotes: 1

Related Questions