回答編集履歴
2
error != nil は正常系なので判定修正
test
CHANGED
@@ -52,7 +52,7 @@
|
|
52
52
|
|
53
53
|
```swift
|
54
54
|
|
55
|
-
if error
|
55
|
+
if error == nil {
|
56
56
|
|
57
57
|
return
|
58
58
|
|
1
エラーとなっていた原因が間違っていたので修正
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
let directions = MKDirections(request: myRequest)
|
8
8
|
|
9
|
-
directions.calculate {
|
9
|
+
directions.calculate(completionHandler: {(response, error) -> Void in
|
10
10
|
|
11
11
|
_ = response!.routes.count
|
12
12
|
|
@@ -28,15 +28,15 @@
|
|
28
28
|
|
29
29
|
|
30
30
|
|
31
|
-
エラーになっていた原因は、 `
|
31
|
+
エラーになっていた原因は、 `as! MKDirectionsHandler` にあると思います。
|
32
32
|
|
33
|
-
`MKDirections
|
33
|
+
`(response:MKDirectionsResponse!, error:NSError!) -> Void in` の書き方だと handler の型に反ってないため実行時エラーが起きているようです。
|
34
|
+
|
35
|
+
おそらく Xcode のエラーででてくる補完を利用したと思うのですが、あれはあまりアテにしてはいけません。
|
34
36
|
|
35
37
|
|
36
38
|
|
37
|
-
また、 `MKDirectionsHandler`
|
39
|
+
また、 `MKDirectionsHandler` は `directions.calculate { response, error in` のようにもアクセスできますので、一般的にはこう書きます。
|
38
|
-
|
39
|
-
|
40
40
|
|
41
41
|
この書き方は Trailing Closure というのですが、最初のうちは深く考えずにこういう風に書ける記法があると思えば問題ないと思います。 [[Swift] Closureについてメモ \- Qiita](https://qiita.com/edo_m18/items/1d93af7de75c6d415f19#trailing-closures)
|
42
42
|
|