質問編集履歴

10

やりたいことの明確化

2016/09/20 11:23

投稿

takhub
takhub

スコア28

test CHANGED
File without changes
test CHANGED
@@ -14,15 +14,17 @@
14
14
 
15
15
  ###実現したい動作フロー
16
16
 
17
+ 下記フローの中で4の箇所を実現するために、まずはサンプルデータで上手くいくかどうかを試しています。
18
+
17
- 1. scanViewControllerクラスのfoundCodeメソッドでバーコード番号読み取り
19
+ 0. scanViewControllerクラスのfoundCodeメソッドでバーコード番号読み取り
18
-
20
+
19
- 2. nutritionDisplayViewControllerクラスのfoodFindIdForBarcodeメソッドへ1を渡し、製品IDを取得
21
+ 0. nutritionDisplayViewControllerクラスのfoodFindIdForBarcodeメソッドへ1を渡し、製品IDを取得
20
-
22
+
21
- 3. nutritionDisplayViewControllerクラスのfoodGetメソッドへ2を渡し、栄養パラメータを取得
23
+ 0. nutritionDisplayViewControllerクラスのfoodGetメソッドへ2を渡し、栄養パラメータを取得
22
-
24
+
23
- 4. BarChartViewクラスに3を投げてグラフを作成
25
+ 0. **BarChartViewクラスに3を投げてグラフを作成**
24
-
26
+
25
- 4. scanViewControllerクラスに戻り、transitionメソッドでnutritionDisplayViewControllerクラスへ画面遷移
27
+ 0. scanViewControllerクラスに戻り、transitionメソッドでnutritionDisplayViewControllerクラスへ画面遷移
26
28
 
27
29
 
28
30
 
@@ -198,27 +200,7 @@
198
200
 
199
201
 
200
202
 
201
- let URL = "http://platform.fatsecret.com/rest/server.api"
202
-
203
- var parameters = [String: String]()
204
-
205
-
206
-
207
- parameters["format"] = "json"
208
-
209
- parameters["food_id"] = fid
203
+ //省略(foodFindIdForBarcodeと同様)
210
-
211
-
212
-
213
- parameters["method"] = "food.get"
214
-
215
-
216
-
217
- parameters["oauth_version"] = "1.0"
218
-
219
- //省略
220
-
221
- parameters["oauth_signature"] = oauthSignatureForMethod("GET", url: NSURL(string: URL)!, parameters: parameters)
222
204
 
223
205
 
224
206
 
@@ -258,7 +240,7 @@
258
240
 
259
241
  }
260
242
 
261
- print("(1)")
243
+
262
244
 
263
245
  /*********************/
264
246
 
@@ -266,7 +248,7 @@
266
248
 
267
249
  /*********************/
268
250
 
269
- print("(2)")
251
+
270
252
 
271
253
  self.months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
272
254
 

9

実現したい動作フローを追記

2016/09/20 11:23

投稿

takhub
takhub

スコア28

test CHANGED
File without changes
test CHANGED
@@ -12,6 +12,20 @@
12
12
 
13
13
 
14
14
 
15
+ ###実現したい動作フロー
16
+
17
+ 1. scanViewControllerクラスのfoundCodeメソッドでバーコード番号読み取り
18
+
19
+ 2. nutritionDisplayViewControllerクラスのfoodFindIdForBarcodeメソッドへ1を渡し、製品IDを取得
20
+
21
+ 3. nutritionDisplayViewControllerクラスのfoodGetメソッドへ2を渡し、栄養パラメータを取得
22
+
23
+ 4. BarChartViewクラスに3を投げてグラフを作成
24
+
25
+ 4. scanViewControllerクラスに戻り、transitionメソッドでnutritionDisplayViewControllerクラスへ画面遷移
26
+
27
+
28
+
15
29
  ###発生している問題・エラーメッセージ
16
30
 
17
31
 
@@ -46,13 +60,41 @@
46
60
 
47
61
  ```swift
48
62
 
63
+ // scanViewController.swift
64
+
65
+
66
+
67
+ // バーコード番号を読み取る関数
68
+
69
+ public func foundCode(scannedBarcode: String) {
70
+
71
+ nutritionDisplayViewController().foodFindIdForBarcode(scannedBarcode)
72
+
73
+ //省略
74
+
49
- import UIKit
75
+ transition()
50
-
76
+
51
- import Alamofire
77
+ }
52
-
78
+
79
+
80
+
53
- import SwiftyJSON
81
+ func transition() {
82
+
54
-
83
+ let next = self.storyboard?.instantiateViewControllerWithIdentifier("modal") as! nutritionDisplayViewController
84
+
85
+ self.navigationController?.pushViewController(next, animated: true)
86
+
55
- import Charts
87
+ //print(test)
88
+
89
+ }
90
+
91
+ ```
92
+
93
+
94
+
95
+ ```swift
96
+
97
+ // nutritionDisplayViewController.swift
56
98
 
57
99
 
58
100
 
@@ -102,13 +144,7 @@
102
144
 
103
145
  parameters["oauth_version"] = "1.0"
104
146
 
105
- parameters["oauth_signature_method"] = "HMAC-SHA1"
147
+ //省略
106
-
107
- parameters["oauth_consumer_key"] = "81a4be8f300b4d8e8f44c51244ca2a38"
108
-
109
- parameters["oauth_nonce"] = (NSUUID().UUIDString as NSString).substringToIndex(8)
110
-
111
- parameters["oauth_timestamp"] = String(Int64(NSDate().timeIntervalSince1970))
112
148
 
113
149
  parameters["oauth_signature"] = oauthSignatureForMethod("GET", url: NSURL(string: URL)!, parameters: parameters)
114
150
 
@@ -180,13 +216,7 @@
180
216
 
181
217
  parameters["oauth_version"] = "1.0"
182
218
 
183
- parameters["oauth_signature_method"] = "HMAC-SHA1"
219
+ //省略
184
-
185
- parameters["oauth_consumer_key"] = "81a4be8f300b4d8e8f44c51244ca2a38"
186
-
187
- parameters["oauth_nonce"] = (NSUUID().UUIDString as NSString).substringToIndex(8)
188
-
189
- parameters["oauth_timestamp"] = String(Int64(NSDate().timeIntervalSince1970))
190
220
 
191
221
  parameters["oauth_signature"] = oauthSignatureForMethod("GET", url: NSURL(string: URL)!, parameters: parameters)
192
222
 

8

試したこと追記

2016/09/20 10:01

投稿

takhub
takhub

スコア28

test CHANGED
File without changes
test CHANGED
@@ -348,7 +348,13 @@
348
348
 
349
349
 
350
350
 
351
- ###試したこと(1)
351
+ ###試したこと
352
+
353
+ 1.nilとなっている箇所の特定
354
+
355
+
356
+
357
+ ---
352
358
 
353
359
  エラー箇所特定のため、print("(1)")とprint("(2)")を書いてみたところ、(1)のみ表示されるため、self.barChartView.delegate = selfが上手くいっていないと思います。
354
360
 
@@ -356,7 +362,15 @@
356
362
 
357
363
 
358
364
 
365
+
366
+
367
+
368
+
359
- ###試したこと(2)
369
+ 2.viewDidLoad内への移動
370
+
371
+
372
+
373
+ ---
360
374
 
361
375
  下記の部分をviewDidLoad内に書いてみました。動作するものの、これではネットワーク通信で取得したデータを反映させようがないのでダメですね。
362
376
 
@@ -392,6 +406,48 @@
392
406
 
393
407
 
394
408
 
409
+ 3.response.dataの確認
410
+
411
+
412
+
413
+ ---
414
+
415
+ ```
416
+
417
+ 2016-09-20 12:46:13.107 FoodAPISample[2316:1015696] Unable to simultaneously satisfy constraints.
418
+
419
+ Probably at least one of the constraints in the following list is one you don't want.
420
+
421
+ Try this:
422
+
423
+ (1) look at each constraint and try to figure out which you don't expect;
424
+
425
+ (2) find the code that added the unwanted constraint or constraints and fix it.
426
+
427
+ (
428
+
429
+ "<NSLayoutConstraint:0x144d93660 V:|-(20)-[UIInputSetContainerView:0x144d91820] (Names: '|':UITextEffectsWindow:0x144d8eab0 )>",
430
+
431
+ "<NSLayoutConstraint:0x144d7fc70 'UIInputWindowController-top' V:|-(0)-[UIInputSetContainerView:0x144d91820] (Names: '|':UITextEffectsWindow:0x144d8eab0 )>"
432
+
433
+ )
434
+
435
+
436
+
437
+ Will attempt to recover by breaking constraint
438
+
439
+ <NSLayoutConstraint:0x144d93660 V:|-(20)-[UIInputSetContainerView:0x144d91820] (Names: '|':UITextEffectsWindow:0x144d8eab0 )>
440
+
441
+
442
+
443
+ Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
444
+
445
+ The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
446
+
447
+ ```
448
+
449
+
450
+
395
451
  ###補足情報(言語/FW/ツール等のバージョンなど)
396
452
 
397
453
  #####バージョン情報

7

試したこと編集

2016/09/20 03:55

投稿

takhub
takhub

スコア28

test CHANGED
File without changes
test CHANGED
@@ -358,7 +358,37 @@
358
358
 
359
359
  ###試したこと(2)
360
360
 
361
- viewDidLoad内に書いてみました。動作するものの、これではネットワーク通信で取得したデータを反映させようがないのでダメですね。
361
+ 下記の部分をviewDidLoad内に書いてみました。動作するものの、これではネットワーク通信で取得したデータを反映させようがないのでダメですね。
362
+
363
+
364
+
365
+ ```swift
366
+
367
+ self.barChartView.delegate = self
368
+
369
+ self.months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
370
+
371
+ let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
372
+
373
+
374
+
375
+ self.setChart(self.months, values: unitsSold)
376
+
377
+ self.barChartView.descriptionText = ""
378
+
379
+ self.barChartView.xAxis.labelPosition = .Bottom
380
+
381
+ self.barChartView.backgroundColor = UIColor(red: 189/255, green: 195/255, blue: 199/255, alpha: 1)
382
+
383
+
384
+
385
+ self.barChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .EaseInBounce)
386
+
387
+
388
+
389
+ let ll = ChartLimitLine(limit: 10.0, label: "Target")
390
+
391
+ ```
362
392
 
363
393
 
364
394
 

6

試したこと追加

2016/09/20 02:51

投稿

takhub
takhub

スコア28

test CHANGED
File without changes
test CHANGED
@@ -348,7 +348,7 @@
348
348
 
349
349
 
350
350
 
351
- ###試したこと
351
+ ###試したこと(1)
352
352
 
353
353
  エラー箇所特定のため、print("(1)")とprint("(2)")を書いてみたところ、(1)のみ表示されるため、self.barChartView.delegate = selfが上手くいっていないと思います。
354
354
 
@@ -356,6 +356,12 @@
356
356
 
357
357
 
358
358
 
359
+ ###試したこと(2)
360
+
361
+ viewDidLoad内に書いてみました。動作するものの、これではネットワーク通信で取得したデータを反映させようがないのでダメですね。
362
+
363
+
364
+
359
365
  ###補足情報(言語/FW/ツール等のバージョンなど)
360
366
 
361
367
  #####バージョン情報

5

スクショ追加

2016/09/20 02:45

投稿

takhub
takhub

スコア28

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- お世話になります。
1
+ お世話になります。長文になりますがお許しください。
2
2
 
3
3
 
4
4
 
@@ -312,7 +312,7 @@
312
312
 
313
313
 
314
314
 
315
- public func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) {
315
+ public func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dat![イメージ説明](2d825031817cd81197be20b7d407c8eb.png)dex: Int, highlight: ChartHighlight) {
316
316
 
317
317
  print("\(entry.value) in \(months[entry.xIndex])")
318
318
 
@@ -328,6 +328,26 @@
328
328
 
329
329
  ```
330
330
 
331
+ ###Storyboard
332
+
333
+ Storyboard全体のイメージです。nutritionDisplayViewControllerを選択しています。
334
+
335
+
336
+
337
+ ![イメージ説明](082b3d8cc9c8ae2873c29d29850caf81.png)
338
+
339
+
340
+
341
+ nutritionDisplayViewController内のBarChartViewを選択しています。
342
+
343
+
344
+
345
+ ![イメージ説明](fa26eba14e27307daa1abde899c6672e.png)
346
+
347
+
348
+
349
+
350
+
331
351
  ###試したこと
332
352
 
333
353
  エラー箇所特定のため、print("(1)")とprint("(2)")を書いてみたところ、(1)のみ表示されるため、self.barChartView.delegate = selfが上手くいっていないと思います。

4

ちょっとした修正

2016/09/20 02:34

投稿

takhub
takhub

スコア28

test CHANGED
File without changes
test CHANGED
@@ -328,40 +328,38 @@
328
328
 
329
329
  ```
330
330
 
331
-
331
+ ###試したこと
332
+
332
-
333
+ エラー箇所特定のため、print("(1)")とprint("(2)")を書いてみたところ、(1)のみ表示されるため、self.barChartView.delegate = selfが上手くいっていないと思います。
334
+
335
+ nilが返されていると思われるself.barChartView.delegateの情報を見ようと、print(self.barChartView.delegate)で出力してみましたが、何も出力されません。
336
+
337
+
338
+
339
+ ###補足情報(言語/FW/ツール等のバージョンなど)
340
+
341
+ #####バージョン情報
342
+
343
+ Alamofire 3.4
344
+
345
+ Charts 2.2.5
346
+
347
+ XCode 7
348
+
349
+ iOS 9.3.5
350
+
351
+
352
+
353
+ #####利用しているライブラリ
354
+
355
+ [ios-charts](https://github.com/danielgindi/Charts)
356
+
357
+ [Alamofire](https://github.com/Alamofire/Alamofire)
358
+
359
+ [SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON.git)
360
+
361
+
362
+
333
- ######利用しているAPI
363
+ #####利用しているAPI
334
364
 
335
365
  [FatSecret Platform REST API](https://platform.fatsecret.com/api/Default.aspx?screen=rapih)
336
-
337
-
338
-
339
- ###試したこと
340
-
341
- エラー箇所特定のため、print("(1)")とprint("(2)")を書いてみたところ、(1)のみ表示されるため、self.barChartView.delegate = selfが上手くいっていないと思います。
342
-
343
- nilが返されていると思われるself.barChartView.delegateの情報を見ようと、print(self.barChartView.delegate)で出力してみましたが、何も出力されません。
344
-
345
-
346
-
347
- ###補足情報(言語/FW/ツール等のバージョンなど)
348
-
349
- ######バージョン情報
350
-
351
- Alamofire 3.4
352
-
353
- Charts 2.2.5
354
-
355
- XCode 7
356
-
357
- iOS 9.3.5
358
-
359
-
360
-
361
- ######利用しているライブラリ
362
-
363
- [ios-charts](https://github.com/danielgindi/Charts)
364
-
365
- [Alamofire](https://github.com/Alamofire/Alamofire)
366
-
367
- [SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON.git)

3

試したこと訂正

2016/09/20 02:27

投稿

takhub
takhub

スコア28

test CHANGED
File without changes
test CHANGED
@@ -338,7 +338,7 @@
338
338
 
339
339
  ###試したこと
340
340
 
341
- エラー箇所特定のため、print("(1)")とprint("(2)")を書いてみたところ、self.barChartView.delegate = selfの手前でクラッシュしているのでおそらくそこが上手くいっていないと思います。
341
+ エラー箇所特定のため、print("(1)")とprint("(2)")を書いてみたところ、(1)のみ表示されるため、self.barChartView.delegate = selfが上手くいっていないと思います。
342
342
 
343
343
  nilが返されていると思われるself.barChartView.delegateの情報を見ようと、print(self.barChartView.delegate)で出力してみましたが、何も出力されません。
344
344
 

2

エラー追記

2016/09/20 02:26

投稿

takhub
takhub

スコア28

test CHANGED
File without changes
test CHANGED
@@ -30,6 +30,18 @@
30
30
 
31
31
 
32
32
 
33
+ またUI上では、スキャン後に画面遷移する仕様で画面遷移はするのですが、下記のメッセージが表示されてしまいます。(投稿者自身が、データがないときにこのメッセージを表示するようにしています)
34
+
35
+
36
+
37
+ ```
38
+
39
+ "You need to provide data for the chart"
40
+
41
+ ```
42
+
43
+
44
+
33
45
  ###該当のソースコード
34
46
 
35
47
  ```swift

1

2016/09/20 02:16

投稿

takhub
takhub

スコア28

test CHANGED
File without changes
test CHANGED
@@ -8,310 +8,314 @@
8
8
 
9
9
  大まかな機能としては、バーコードでスキャンした製品の栄養バランスをグラフ化するというものです。
10
10
 
11
+ FatSecretという全世界の製品を網羅する栄養データベースにアクセスし、そのデータをグラフに反映させようとしています。
12
+
13
+
14
+
15
+ ###発生している問題・エラーメッセージ
16
+
17
+
18
+
11
- FatSecretという全世界製品を網羅する栄養データベースアクセスし、そのデータをグラフに反映させたいのですが、アクセス中にView(barChartView)へグラフを書き込もうとするとエラーが発生してしまいます。
19
+ FatSecretのデータベースアクセス中にView(barChartView)へグラフを書き込もうとすると下記エラーが発生してしまいます。
20
+
21
+
22
+
23
+ ```
24
+
25
+ fatal error: unexpectedly found nil while unwrapping an Optional value
26
+
27
+ ```
12
28
 
13
29
  エラーが発生している箇所は特定できているのですが、原因がわからず困っております。何卒よろしくお願いします。
14
30
 
15
31
 
16
32
 
17
- ###発生している問題・エラメッセ
33
+ ###該当のソスコ
34
+
35
+ ```swift
36
+
37
+ import UIKit
38
+
39
+ import Alamofire
40
+
41
+ import SwiftyJSON
42
+
43
+ import Charts
44
+
45
+
46
+
47
+ public class nutritionDisplayViewController: UIViewController, ChartViewDelegate {
48
+
49
+
50
+
51
+ @IBOutlet weak var barChartView: BarChartView!
52
+
53
+
54
+
55
+ var months: [String]! //サンプルデータ
56
+
57
+ var nutritions: [[String: String?]] = [] //FatSecretからとってくる栄養パラメータ
58
+
59
+
60
+
61
+ override public func viewDidLoad() {
62
+
63
+ super.viewDidLoad()
64
+
65
+ // Do any additional setup after loading the view, typically from a nib.
66
+
67
+ }
68
+
69
+
70
+
71
+ // 製品のバーコードに紐づけられた独自のIDを取得
72
+
73
+ public func foodFindIdForBarcode(scannedBarcode:String) {
74
+
75
+
76
+
77
+ let URL = "http://platform.fatsecret.com/rest/server.api"
78
+
79
+ var parameters = [String: String]()
80
+
81
+
82
+
83
+ parameters["format"] = "json"
84
+
85
+ parameters["method"] = "food.find_id_for_barcode"
86
+
87
+ parameters["barcode"] = scannedBarcode
88
+
89
+
90
+
91
+ parameters["oauth_version"] = "1.0"
92
+
93
+ parameters["oauth_signature_method"] = "HMAC-SHA1"
94
+
95
+ parameters["oauth_consumer_key"] = "81a4be8f300b4d8e8f44c51244ca2a38"
96
+
97
+ parameters["oauth_nonce"] = (NSUUID().UUIDString as NSString).substringToIndex(8)
98
+
99
+ parameters["oauth_timestamp"] = String(Int64(NSDate().timeIntervalSince1970))
100
+
101
+ parameters["oauth_signature"] = oauthSignatureForMethod("GET", url: NSURL(string: URL)!, parameters: parameters)
102
+
103
+
104
+
105
+ // FatSecretにリクエスト
106
+
107
+ Alamofire.request(.GET, URL, parameters: parameters)
108
+
109
+ .responseJSON { response in
110
+
111
+ guard let object = response.result.value else {
112
+
113
+ return
114
+
115
+ }
116
+
117
+ let json = JSON(object)
118
+
119
+ json.forEach { (_, json) in
120
+
121
+ let nutrition: [String: String?] = [
122
+
123
+ "barcode": json["barcode"].string
124
+
125
+ ]
126
+
127
+ self.nutritions.append(nutrition)
128
+
129
+ }
130
+
131
+ self.foodGet(json["food_id"]["value"].string!)
132
+
133
+
134
+
135
+ if let JSON = response.result.value {
136
+
137
+ print("JSON: \(JSON)")
138
+
139
+ }
140
+
141
+ }
142
+
143
+ }
144
+
145
+
146
+
147
+ // foodFindIdForBarcode()で取得したIDをもとに栄養パラメータを取得(この関数内でのネットワーク通信中にエラーが発生しています)
148
+
149
+ public func foodGet(fid:String) {
150
+
151
+
152
+
153
+ let URL = "http://platform.fatsecret.com/rest/server.api"
154
+
155
+ var parameters = [String: String]()
156
+
157
+
158
+
159
+ parameters["format"] = "json"
160
+
161
+ parameters["food_id"] = fid
162
+
163
+
164
+
165
+ parameters["method"] = "food.get"
166
+
167
+
168
+
169
+ parameters["oauth_version"] = "1.0"
170
+
171
+ parameters["oauth_signature_method"] = "HMAC-SHA1"
172
+
173
+ parameters["oauth_consumer_key"] = "81a4be8f300b4d8e8f44c51244ca2a38"
174
+
175
+ parameters["oauth_nonce"] = (NSUUID().UUIDString as NSString).substringToIndex(8)
176
+
177
+ parameters["oauth_timestamp"] = String(Int64(NSDate().timeIntervalSince1970))
178
+
179
+ parameters["oauth_signature"] = oauthSignatureForMethod("GET", url: NSURL(string: URL)!, parameters: parameters)
180
+
181
+
182
+
183
+ Alamofire.request(.GET, URL, parameters: parameters)
184
+
185
+ .responseJSON { response in
186
+
187
+ guard let object = response.result.value else {
188
+
189
+ return
190
+
191
+ }
192
+
193
+ let json = JSON(object)
194
+
195
+ json.forEach { (_, json) in
196
+
197
+ let nutrition: [String: String?] =
198
+
199
+ ["food_id": json["food_id"]["value"].string,
200
+
201
+ "brand_name": json["food"]["brand_name"].string,
202
+
203
+ "vitamin_c": json["food"]["servings"]["serving"]["vitamin_c"].string
204
+
205
+ ]
206
+
207
+ self.nutritions.append(nutrition)
208
+
209
+ }
210
+
211
+
212
+
213
+ if let JSON = response.result.value {
214
+
215
+ print("JSON: \(JSON)")
216
+
217
+ }
218
+
219
+ print("(1)")
220
+
221
+ /*********************/
222
+
223
+ self.barChartView.delegate = self //ここで "fatal error: unexpectedly found nil while unwrapping an Optional value"
224
+
225
+ /*********************/
226
+
227
+ print("(2)")
228
+
229
+ self.months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
230
+
231
+ let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
232
+
233
+
234
+
235
+ self.setChart(self.months, values: unitsSold)
236
+
237
+ self.barChartView.descriptionText = ""
238
+
239
+ self.barChartView.xAxis.labelPosition = .Bottom
240
+
241
+ self.barChartView.backgroundColor = UIColor(red: 189/255, green: 195/255, blue: 199/255, alpha: 1)
242
+
243
+
244
+
245
+ self.barChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .EaseInBounce)
246
+
247
+
248
+
249
+ let ll = ChartLimitLine(limit: 10.0, label: "Target")
250
+
251
+ }
252
+
253
+ viewDidAppear(true)
254
+
255
+ }
256
+
257
+
258
+
259
+ override public func viewDidAppear(animated: Bool) {
260
+
261
+ }
262
+
263
+
264
+
265
+ /*----グラフ作成(ios-charts参考)----*/
266
+
267
+ public func setChart(dataPoints: [String], values: [Double]) {
268
+
269
+ barChartView.noDataText = "You need to provide data for the chart"
270
+
271
+
272
+
273
+ var dataEntries: [BarChartDataEntry] = []
274
+
275
+
276
+
277
+ for i in 0..<dataPoints.count {
278
+
279
+ let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
280
+
281
+ dataEntries.append(dataEntry)
282
+
283
+ }
284
+
285
+
286
+
287
+ let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Units Sold")
288
+
289
+ let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
290
+
291
+
292
+
293
+ chartDataSet.colors = ChartColorTemplates.colorful()
294
+
295
+
296
+
297
+ barChartView.data = chartData
298
+
299
+ }
300
+
301
+
302
+
303
+ public func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) {
304
+
305
+ print("\(entry.value) in \(months[entry.xIndex])")
306
+
307
+ }
308
+
309
+
310
+
311
+ /*---- OAuth関連 ----*/
312
+
313
+ //省略
18
314
 
19
315
 
20
316
 
21
317
  ```
22
318
 
23
- fatal error: unexpectedly found nil while unwrapping an Optional value
24
-
25
- ```
26
-
27
-
28
-
29
- ###該当のソースコード
30
-
31
- ```swift
32
-
33
- import UIKit
34
-
35
- import Alamofire
36
-
37
- import SwiftyJSON
38
-
39
- import Charts
40
-
41
-
42
-
43
- public class nutritionDisplayViewController: UIViewController, ChartViewDelegate {
44
-
45
-
46
-
47
- @IBOutlet weak var barChartView: BarChartView!
48
-
49
-
50
-
51
- var months: [String]! //サンプルデータ
52
-
53
- var nutritions: [[String: String?]] = [] //FatSecretからとってくる栄養パラメータ
54
-
55
-
56
-
57
- override public func viewDidLoad() {
58
-
59
- super.viewDidLoad()
60
-
61
- // Do any additional setup after loading the view, typically from a nib.
62
-
63
- }
64
-
65
-
66
-
67
- // 製品のバーコードに紐づけられた独自のIDを取得
68
-
69
- public func foodFindIdForBarcode(scannedBarcode:String) {
70
-
71
-
72
-
73
- let URL = "http://platform.fatsecret.com/rest/server.api"
74
-
75
- var parameters = [String: String]()
76
-
77
-
78
-
79
- parameters["format"] = "json"
80
-
81
- parameters["method"] = "food.find_id_for_barcode"
82
-
83
- parameters["barcode"] = scannedBarcode
84
-
85
-
86
-
87
- parameters["oauth_version"] = "1.0"
88
-
89
- parameters["oauth_signature_method"] = "HMAC-SHA1"
90
-
91
- parameters["oauth_consumer_key"] = "81a4be8f300b4d8e8f44c51244ca2a38"
92
-
93
- parameters["oauth_nonce"] = (NSUUID().UUIDString as NSString).substringToIndex(8)
94
-
95
- parameters["oauth_timestamp"] = String(Int64(NSDate().timeIntervalSince1970))
96
-
97
- parameters["oauth_signature"] = oauthSignatureForMethod("GET", url: NSURL(string: URL)!, parameters: parameters)
98
-
99
-
100
-
101
- // FatSecretにリクエスト
102
-
103
- Alamofire.request(.GET, URL, parameters: parameters)
104
-
105
- .responseJSON { response in
106
-
107
- guard let object = response.result.value else {
108
-
109
- return
110
-
111
- }
112
-
113
- let json = JSON(object)
114
-
115
- json.forEach { (_, json) in
116
-
117
- let nutrition: [String: String?] = [
118
-
119
- "barcode": json["barcode"].string
120
-
121
- ]
122
-
123
- self.nutritions.append(nutrition)
124
-
125
- }
126
-
127
- self.foodGet(json["food_id"]["value"].string!)
128
-
129
-
130
-
131
- if let JSON = response.result.value {
132
-
133
- print("JSON: \(JSON)")
134
-
135
- }
136
-
137
- }
138
-
139
- }
140
-
141
-
142
-
143
- // foodFindIdForBarcode()で取得したIDをもとに栄養パラメータを取得(この関数内でのネットワーク通信中にエラーが発生しています)
144
-
145
- public func foodGet(fid:String) {
146
-
147
-
148
-
149
- let URL = "http://platform.fatsecret.com/rest/server.api"
150
-
151
- var parameters = [String: String]()
152
-
153
-
154
-
155
- parameters["format"] = "json"
156
-
157
- parameters["food_id"] = fid
158
-
159
-
160
-
161
- parameters["method"] = "food.get"
162
-
163
-
164
-
165
- parameters["oauth_version"] = "1.0"
166
-
167
- parameters["oauth_signature_method"] = "HMAC-SHA1"
168
-
169
- parameters["oauth_consumer_key"] = "81a4be8f300b4d8e8f44c51244ca2a38"
170
-
171
- parameters["oauth_nonce"] = (NSUUID().UUIDString as NSString).substringToIndex(8)
172
-
173
- parameters["oauth_timestamp"] = String(Int64(NSDate().timeIntervalSince1970))
174
-
175
- parameters["oauth_signature"] = oauthSignatureForMethod("GET", url: NSURL(string: URL)!, parameters: parameters)
176
-
177
-
178
-
179
- Alamofire.request(.GET, URL, parameters: parameters)
180
-
181
- .responseJSON { response in
182
-
183
- guard let object = response.result.value else {
184
-
185
- return
186
-
187
- }
188
-
189
- let json = JSON(object)
190
-
191
- json.forEach { (_, json) in
192
-
193
- let nutrition: [String: String?] =
194
-
195
- ["food_id": json["food_id"]["value"].string,
196
-
197
- "brand_name": json["food"]["brand_name"].string,
198
-
199
- "vitamin_c": json["food"]["servings"]["serving"]["vitamin_c"].string
200
-
201
- ]
202
-
203
- self.nutritions.append(nutrition)
204
-
205
- }
206
-
207
-
208
-
209
- if let JSON = response.result.value {
210
-
211
- print("JSON: \(JSON)")
212
-
213
- }
214
-
215
- print("(1)")
216
-
217
- /*********************/
218
-
219
- self.barChartView.delegate = self //ここで "fatal error: unexpectedly found nil while unwrapping an Optional value"
220
-
221
- /*********************/
222
-
223
- print("(2)")
224
-
225
- self.months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
226
-
227
- let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
228
-
229
-
230
-
231
- self.setChart(self.months, values: unitsSold)
232
-
233
- self.barChartView.descriptionText = ""
234
-
235
- self.barChartView.xAxis.labelPosition = .Bottom
236
-
237
- self.barChartView.backgroundColor = UIColor(red: 189/255, green: 195/255, blue: 199/255, alpha: 1)
238
-
239
-
240
-
241
- self.barChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .EaseInBounce)
242
-
243
-
244
-
245
- let ll = ChartLimitLine(limit: 10.0, label: "Target")
246
-
247
- }
248
-
249
- viewDidAppear(true)
250
-
251
- }
252
-
253
-
254
-
255
- override public func viewDidAppear(animated: Bool) {
256
-
257
- }
258
-
259
-
260
-
261
- /*----グラフ作成(ios-charts参考)----*/
262
-
263
- public func setChart(dataPoints: [String], values: [Double]) {
264
-
265
- barChartView.noDataText = "You need to provide data for the chart"
266
-
267
-
268
-
269
- var dataEntries: [BarChartDataEntry] = []
270
-
271
-
272
-
273
- for i in 0..<dataPoints.count {
274
-
275
- let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
276
-
277
- dataEntries.append(dataEntry)
278
-
279
- }
280
-
281
-
282
-
283
- let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Units Sold")
284
-
285
- let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
286
-
287
-
288
-
289
- chartDataSet.colors = ChartColorTemplates.colorful()
290
-
291
-
292
-
293
- barChartView.data = chartData
294
-
295
- }
296
-
297
-
298
-
299
- public func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) {
300
-
301
- print("\(entry.value) in \(months[entry.xIndex])")
302
-
303
- }
304
-
305
-
306
-
307
- /*---- OAuth関連 ----*/
308
-
309
- //省略
310
-
311
-
312
-
313
- ```
314
-
315
319
 
316
320
 
317
321
  ######利用しているAPI