回答編集履歴

1

optional

2018/01/22 00:53

投稿

fuzzball
fuzzball

スコア16731

test CHANGED
@@ -3,6 +3,8 @@
3
3
 
4
4
 
5
5
  ```swift
6
+
7
+ //idの存在が保証されている場合
6
8
 
7
9
  let id = json["user"]["id"].intValue
8
10
 
@@ -10,6 +12,22 @@
10
12
 
11
13
  print(type(of: id)) //=> Int
12
14
 
15
+
16
+
17
+ //idの存在が保証されていない場合
18
+
19
+ if let id = json["user"]["id"].int {
20
+
21
+ print(id) //=> 66
22
+
23
+ print(type(of: id)) //=> Int
24
+
25
+ } else {
26
+
27
+ print("id does not exist.")
28
+
29
+ }
30
+
13
31
  ```
14
32
 
15
33