回答編集履歴
3
修正
test
CHANGED
@@ -1,63 +1,125 @@
|
|
1
|
-
|
1
|
+
とりあえず値は取得できました。
|
2
2
|
|
3
|
-
※
|
3
|
+
※少し変更しました。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
★ 呼び出し側
|
4
8
|
|
5
9
|
```swift
|
6
10
|
|
7
|
-
|
11
|
+
import UIKit
|
8
12
|
|
9
|
-
static func apiRequest(response: (responseArticles: [[String: String?]]) -> ()) {
|
10
13
|
|
11
|
-
|
12
14
|
|
13
|
-
|
15
|
+
class ViewController: UIViewController {
|
14
16
|
|
15
|
-
|
16
17
|
|
17
|
-
Alamofire.request(.GET, "https://qiita.com/api/v2/items")
|
18
18
|
|
19
|
+
override func viewDidLoad() {
|
20
|
+
|
21
|
+
super.viewDidLoad()
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
// 呼び出し側
|
28
|
+
|
19
|
-
.
|
29
|
+
ApiManager.apiRequest { response in
|
20
30
|
|
21
31
|
|
22
32
|
|
23
|
-
guard let object = response.result.value else {
|
24
|
-
|
25
|
-
|
33
|
+
print(response)
|
26
|
-
|
27
|
-
}
|
28
34
|
|
29
35
|
|
30
36
|
|
31
|
-
let json = JSON(object)
|
32
|
-
|
33
|
-
var article: [String: String?] = [:]
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
json.forEach { (_, json) in
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
article = [
|
42
|
-
|
43
|
-
"title": json["title"].string,
|
44
|
-
|
45
|
-
"user": json["user"]["id"].string,
|
46
|
-
|
47
|
-
"profile_image_url": json["user"]["profile_image_url"].string
|
48
|
-
|
49
|
-
]
|
50
|
-
|
51
|
-
articles.append(article)
|
52
|
-
|
53
|
-
|
37
|
+
}
|
54
|
-
|
55
|
-
// 追加↓
|
56
|
-
|
57
|
-
response(responseArticles: articles)
|
58
38
|
|
59
39
|
}
|
60
40
|
|
61
41
|
}
|
62
42
|
|
63
43
|
```
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
★ 通信をして値を戻すクラス
|
48
|
+
|
49
|
+
```swift
|
50
|
+
|
51
|
+
import UIKit
|
52
|
+
|
53
|
+
import Alamofire
|
54
|
+
|
55
|
+
import SwiftyJSON
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
class ApiManager: NSObject {
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
// メソッドの引数を追加(呼び出し元にクロージャーで戻します)
|
66
|
+
|
67
|
+
static func apiRequest(apiResponse: (responseArticles: [[String: String?]]) -> ()) {
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
var articles: [[String: String?]] = []
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
Alamofire.request(.GET, "https://qiita.com/api/v2/items")
|
76
|
+
|
77
|
+
.responseJSON { response in
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
guard let object = response.result.value else {
|
82
|
+
|
83
|
+
return
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
let json = JSON(object)
|
90
|
+
|
91
|
+
var article: [String: String?] = [:]
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
json.forEach { (_, json) in
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
article = [
|
100
|
+
|
101
|
+
"title": json["title"].string,
|
102
|
+
|
103
|
+
"user": json["user"]["id"].string,
|
104
|
+
|
105
|
+
"profile_image_url": json["user"]["profile_image_url"].string
|
106
|
+
|
107
|
+
]
|
108
|
+
|
109
|
+
articles.append(article)
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
// 追加↓
|
114
|
+
|
115
|
+
apiResponse(responseArticles: articles)
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
```
|
2
修正
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
実行していないので動くか分かりませんが戻すという事なら以下のようになるかと思います。
|
1
|
+
実行していないので動くか分かりませんが、戻すという事なら以下のようになるかと思います。
|
2
2
|
|
3
|
-
|
3
|
+
※とりあえずなのでエラーは考慮してません…
|
4
4
|
|
5
5
|
```swift
|
6
6
|
|
1
修正
test
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
実行していないので動くか分かりませんが
|
1
|
+
実行していないので動くか分かりませんが戻すという事なら以下のようになるかと思います。
|
2
2
|
|
3
3
|
|
4
4
|
|
5
5
|
```swift
|
6
6
|
|
7
|
-
|
7
|
+
// メソッドの引数を追加(呼び出し元にクロージャーで戻します)
|
8
8
|
|
9
9
|
static func apiRequest(response: (responseArticles: [[String: String?]]) -> ()) {
|
10
10
|
|
@@ -52,6 +52,8 @@
|
|
52
52
|
|
53
53
|
}
|
54
54
|
|
55
|
+
// 追加↓
|
56
|
+
|
55
57
|
response(responseArticles: articles)
|
56
58
|
|
57
59
|
}
|