Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 7 Cast from XCUIElement to unrelated type 'String' always fails while fetching JSON

Tags:

swift

I am trying to fetch the values from JSON array and i am getting an error "Cast from 'XCUIElement!' to unrelated String always fails."

I am using Xcode 7 with iOS 9.1.

My code is as below:

        let url = NSURL(string: urlAsString)!
        let urlSession = NSURLSession.sharedSession()


        let jsonQuery = urlSession.dataTaskWithURL(url, completionHandler: { data, response, error -> Void in
            if (error != nil) {
                print(error!.localizedDescription)
            }

            do {
                let jsonResult = (try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSMutableArray
                //     print (jsonResult)


                for usernames in jsonResult {
                    let influencer_username = usernames["influencer_username"] as! String


                    print("influencer_username: \(influencer_username)")
                }
like image 895
Ankit Khanna Avatar asked Oct 26 '15 15:10

Ankit Khanna


1 Answers

With Xcode 7.1.1 Not fixed but this workaround helped me:

let influencer_username = usernames["influencer_username"] as AnyObject as! String

like image 106
Eis Avatar answered Nov 15 '22 09:11

Eis