OzzY OzzY - 4 months ago 7x
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 {...}


Try a type cast any object.

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

Or simply force cast as! Int