简体   繁体   中英

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 {...}

Try a type cast any object.

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

Or simply force cast as! Int

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM