-4

I read a snippet from this POST but have not quite understood.

https://www.hackingwithswift.com/example-code/system/how-to-parse-json-using-nsjsonserialization

I am confused on some syntax in the following snippets.

In the following, I am not knowing why try goes here, it is strange syntax to me. Any information about try and as! for this expression?

let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as! [String: AnyObject]

In the following, I am not knowing what is as? [String] doing?

if let names = json["names"] as? [String] {

I know this question might be fundamental, I just need a keyword for me to search the related anwser, thanks. Here is my whole code block.

    // define a string
    let str = "{\"names\": [\"Bob\", \"Tim\", \"Tina\"]}"

    // convert the string into NSUTF8StringEncoding
    let data = str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!

    // put the following statements in try catch block
    do {

        // not knowing why try goes here, strange syntax to me.
        // any higher conception about try and as! for this expression
        let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as! [String: AnyObject]

        // json should be an object(dictionary, hash) use the key "names" to store string array into names
        // not knowing  what is `as? [String]` doing? any keyword for this syntax?
        if let names = json["names"] as? [String] {
            print(names)
        }
    } catch let error as NSError {
        print("Failed to load: \(error.localizedDescription)")
    }
Allanah Fowler
  • 1,251
  • 16
  • 17
user3675188
  • 7,271
  • 11
  • 40
  • 76
  • The way in which you wrote this question is very confusing. Why are you using // comments instead of just writing? It's not in a code block. I do not see, either, how your code even relates to your question. Are you just asking what the difference is between as? and as! ?. Or are you asking what the term is for the type of syntax represented by something like "as?" or "as!" ? – Max von Hippel Jul 18 '16 at 06:06
  • *"not knowing why `try` goes here, strange syntax to me"* – See "Error Handling" in the Swift Reference. – Martin R Jul 18 '16 at 06:06
  • Possible duplicate of [Swift: difference as String? vs. as? String](http://stackoverflow.com/questions/27570957/swift-difference-as-string-vs-as-string) – Max von Hippel Jul 18 '16 at 06:07
  • *"`as!` is what syntax"* – See "Type Casting" in the Swift Reference. – Martin R Jul 18 '16 at 06:07

1 Answers1

0

// not knowing why try goes here, strange syntax to me. what if it fails? as! is what syntax, any keyword to search for it?

The try here is to basically to try to perform the function on that line. If it fails it will go to the catch. Which in this case will just print("Failed to load: \(error.localizedDescription)")

// json should be an object(dictionary, hash) use the key "names" to store string array into names // not knowing what is as? [String] doing? any keyword for this syntax?

The line that you are confuse about is performing a if else on the json object. It check for a key that is name and also want to ensure that its value is a String therefore the as?. In this case if the key name does not exist it will not met the condition. If the value of name is not a String it will not met the condition.

Like what @Martin R mention in the comments, you should read up more on Type Casting in Swift. This should help you. Explanation with some example if you have trouble with Apple Documentation.

As for try catch it is actually use in other languages as well not just Swift. You can read up more here.

Zac Kwan
  • 5,587
  • 4
  • 20
  • 27