質問編集履歴
1
詳細な質問文にしました
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Swiftで
|
1
|
+
【Swift】APIデータで変換した値をラベルに表示したい
|
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
|
1
|
+
APIを使ってひらがなに変換するアプリを作成しています。
|
2
2
|
|
3
|
-
|
3
|
+
textFieldに入力した漢字が、hiraganaConvertButtonを押すことでひらがなに変換された値がtestLabelに表示されるようにしたいのですが、表示されません。
|
4
4
|
|
5
5
|
|
6
6
|
|
@@ -12,19 +12,53 @@
|
|
12
12
|
|
13
13
|
```Swift
|
14
14
|
|
15
|
+
|
16
|
+
|
15
17
|
import UIKit
|
16
18
|
|
17
19
|
|
18
20
|
|
19
|
-
class
|
21
|
+
class ViewController: UIViewController, UITextFieldDelegate {
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
@IBOutlet weak var testLabel: UILabel!
|
26
|
+
|
27
|
+
@IBOutlet weak var textField: UITextField!
|
20
28
|
|
21
29
|
|
22
30
|
|
31
|
+
// レスポンスデータの構造
|
32
|
+
|
33
|
+
struct PostData: Codable {
|
34
|
+
|
35
|
+
var app_id : String
|
36
|
+
|
23
|
-
|
37
|
+
var request_id : String
|
38
|
+
|
39
|
+
var sentence : String
|
40
|
+
|
41
|
+
var output_type : String
|
42
|
+
|
43
|
+
}
|
24
44
|
|
25
45
|
|
26
46
|
|
47
|
+
struct Rubi: Codable {
|
48
|
+
|
49
|
+
var request_id : String
|
50
|
+
|
51
|
+
var output_type : String
|
52
|
+
|
53
|
+
var converted : String
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
|
58
|
+
|
27
|
-
var
|
59
|
+
var sentence: String = ""
|
60
|
+
|
61
|
+
|
28
62
|
|
29
63
|
|
30
64
|
|
@@ -32,30 +66,134 @@
|
|
32
66
|
|
33
67
|
super.viewDidLoad()
|
34
68
|
|
69
|
+
|
35
70
|
|
71
|
+
|
36
72
|
|
37
|
-
|
73
|
+
|
38
74
|
|
39
|
-
|
75
|
+
textField.delegate = self
|
40
76
|
|
41
|
-
|
77
|
+
|
42
78
|
|
43
79
|
}
|
44
80
|
|
45
81
|
|
46
82
|
|
47
|
-
@IBAction func
|
83
|
+
@IBAction func hiraganaConvertButton(_ sender: Any) {
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
print(sentence)
|
88
|
+
|
89
|
+
sentenceChange()
|
90
|
+
|
91
|
+
testLabel.text = sentence
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
// ModalViewに値を渡す
|
98
|
+
|
99
|
+
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
48
100
|
|
49
101
|
|
50
102
|
|
103
|
+
let modalVC = segue.destination as! ModalViewController
|
104
|
+
|
105
|
+
modalVC.result = sentence
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
// ひらがなに変換
|
112
|
+
|
113
|
+
func sentenceChange() {
|
114
|
+
|
115
|
+
if textField.text != nil {
|
116
|
+
|
117
|
+
var request = URLRequest(url: URL(string: "https://labs.goo.ne.jp/api/hiragana")!)
|
118
|
+
|
119
|
+
request.httpMethod = "POST"
|
120
|
+
|
121
|
+
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
let postData = PostData(app_id: "APIキー",request_id: "record003", sentence: textField.text!, output_type: "hiragana")
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
guard let uploadData = try? JSONEncoder().encode(postData) else {
|
130
|
+
|
131
|
+
print("失敗しました")
|
132
|
+
|
133
|
+
return
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
let task = URLSession.shared.uploadTask(with: request, from: uploadData) { data, response, error in
|
138
|
+
|
139
|
+
if let error = error {
|
140
|
+
|
51
|
-
|
141
|
+
print("error:(error)")
|
142
|
+
|
143
|
+
return
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
guard let data = data, let jsonData = try? JSONDecoder().decode(Rubi.self, from: data) else {
|
150
|
+
|
151
|
+
print("json変換に失敗しました")
|
152
|
+
|
153
|
+
return
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
print(jsonData.converted)
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
task.resume()
|
162
|
+
|
163
|
+
}else{
|
164
|
+
|
165
|
+
print("文章を入力してね")
|
166
|
+
|
167
|
+
}
|
52
168
|
|
53
169
|
|
54
170
|
|
55
171
|
}
|
56
172
|
|
173
|
+
|
174
|
+
|
175
|
+
// キーボードを閉じる
|
176
|
+
|
177
|
+
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
|
178
|
+
|
179
|
+
textField.resignFirstResponder()
|
180
|
+
|
181
|
+
return true
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
|
186
|
+
|
57
187
|
}
|
58
188
|
|
59
189
|
|
60
190
|
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
|
198
|
+
|
61
199
|
```
|