OzzY OzzY - 2 years ago 84
iOS Question

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

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

self.homeShareResults.removeAll(keepCapacity: false)


if let parseJSON = json {

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

let sharedID = (friendObj["id"])

if sharedID == user_id {...}

Answer Source

Try a type cast any object.

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

Or simply force cast as! Int

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download