![]() | TECHNICAL MASTER はじめてのiOSアプリ開発 第2版 Xcode 8 Swift 3対応 新品価格 |

swiftで試しにパーサー書いてみたマスタカです
デフォルトがJSONSerializationなので
一旦はOSS使わずにでフォルトでパースしてみた
・サンプル
do {
let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments)
let top = json as? NSDictionary
let second = top?["data"] as? NSDictionary
let third = second?["data"] as? NSArray
print(third!)
} catch {
print(error)
}
・do try catch
swiftはdo try catchでワンセット
・jsonObject
返り値はdata型のjson
https://developer.apple.com/documentation/foundation/jsonserialization/1415493-jsonobject
・allowFragments
https://developer.apple.com/documentation/foundation/jsonserialization.readingoptions/1416042-allowfragments
・as
asでキャスト
!や?つけたバージョンもある
http://dev.classmethod.jp/smartphone/iphone/swift3_code_tutorial1/
・error
エラーの時はerrorの中に値が入ってる
初期化してねーなーとか思うけど動く
普通にprintすると以下のwarningが出る
expression implicitly coerced from Nsarray? to any
Anyを値としてとるとかで、force unwrapした
https://stackoverflow.com/questions/40691184/expression-implicitly-coerced-from-string-to-any
タグ:Swift