0

in Swift 1.2

https://gist.github.com/anonymous/a167148a8a6d169292ff

in Swift 2.0 (error) , so I change my code as follows.

let url = NSURL(string:"http://dl-8.one2up.com/onetwo/content/2015/6/15/9c3b51249fbbe20ca9d841401e276d97.php")
    let allContactsData = NSData(contentsOfURL:url!)

    do{
        var allContacts : AnyObject! = try NSJSONSerialization.JSONObjectWithData(allContactsData!, options: NSJSONReadingOptions())
    }catch{
    print(error)
    }

    if let json = allContacts as? Array<AnyObject>  {
    print(json)


        for index in 0...json.count-1 {

            let contact : AnyObject? = json[index]

            let collection = contact! as! Dictionary<String, AnyObject>

            let name : AnyObject? = collection["AnimeName"]
            let cont : AnyObject? = collection["Episodes"]

            names.append(name as! String)
            episodes.append(cont as! String)
        }

    }
    print(names)
    print(episodes)

But that doesn't work.

if let json = allContacts as? Array< AnyObject >

error : Use of unresolved identifier 'allContacts'

2
  • You should pull your code in from the gist, so that we don't have to open multiple tabs to understand your question. :) Commented Jun 23, 2015 at 17:29
  • I'm Sorry @Bloodyaugust Commented Jun 23, 2015 at 17:59

2 Answers 2

2

Your allContacts variable is created in the do block, therefore its scope is limited to the do block, and cannot be accessed outside of that block. If you want it to be accessible outside the block, declare it outside of the block, but then continue to assign it inside the block. IE:

let allContactsData = NSData(contentsOfURL:url!)
var allContacts:AnyObject
do{
   allContacts = try NSJSONSerialization.JSONObjectWithData(allContactsData!, options: NSJSONReadingOptions())
}catch{
print(error)
}
Sign up to request clarification or add additional context in comments.

Comments

0
do{
var allContacts = try NSJSONSerialization.JSONObjectWithData(allContactsData!, options: NSJSONReadingOptions()) as! [Dictionary<String, AnyObject>]
for contact in allContacts {
let name : AnyObject? = contact["AnimeName"]
let cont : AnyObject? = contact["Episodes"]

names.append(name as! String)
episodes.append(cont as! String)
}
}catch{
print(error)
}

5 Comments

Run Succeeded ,Output : fatal error: unexpectedly found nil while unwrapping an Optional value (lldb). T^T
Want to log allContacts?
sorry, ios9 require TLSv1.2 SSL
Not sure what you mean by that, can you elaborate?
App Transport Security (ATS) By default in iOS 9 if you try to load an HTTP resource in your app it's actually going to try to load the HTTPS version. If the HTTPS version is using security Apple considers weak, or the server just doesn't support HTTPS at all, the request will fail. The protocol Transport Layer Security (TLS) must be at least version 1.2. Add this to you info.plist: NSAppTransportSecurity Dictionary NSAllowsArbitraryLoads Boolean YES

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.