살짝 다른 문법(?)
- ERROR 관련(?) 은 swift 2.0부터 syntax 가 바뀜
* old
var err: NSError? var myJSON = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error:&err) as? NSDictionary if let parseJSON =myJSON { // Now we can access value of First Name by its key var firstNameValue = parseJSON[“firstName”] as? String println(“firstNameValue: \(firstNameValue)”) } |
* new(swift 2.0)
do { if let parseJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary { // Now we can access value of First Name by its key let firstNameValue:String = parseJSON["firstName"] as! String print("firstNameValue: \(firstNameValue)") } } catch { print(error) } |
- 결과 값에 optional 값이 같이 출력됨.(print)
명령어 | 결과값 |
print(“op : \(option)") | op : optional(VALUE) |
print(“op : \(option)!”) | op :VALUE |