回答編集履歴

1

s

2017/02/19 03:04

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -3,8 +3,6 @@
3
3
 
4
4
 
5
5
  ```swift
6
-
7
-
8
6
 
9
7
  import Foundation
10
8
 
@@ -22,15 +20,37 @@
22
20
 
23
21
  let jsonDict = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) as! NSDictionary
24
22
 
25
- let jsonArray = jsonDict["address_components"] as? NSArray
23
+ if let jsonArray = jsonDict["address_components"] as? NSArray {
26
24
 
27
- let dict = jsonArray?[0] as? NSDictionary
25
+
28
26
 
29
- let country = dict?["short_name"] as? String
27
+ for item in jsonArray {
30
28
 
31
-
29
+
32
30
 
31
+ if let item = item as? NSDictionary {
32
+
33
+ if let types = item["types"] as? [String],
34
+
35
+ types.contains("country") {
36
+
37
+
38
+
39
+ // typesの中にcountryがある
40
+
41
+ let country = item["short_name"] as? String
42
+
43
+
44
+
33
- country
45
+ country
46
+
47
+ }
48
+
49
+ }
50
+
51
+ }
52
+
53
+ }
34
54
 
35
55
 
36
56