*やりたいこと
この関数から得られた現在位置の座標を別の関数2つの座標から角度や距離を求めるに使用したい。
その上で、現在位置を戻り値で返したい。
戻り値を関数の外で定数で定義したときにエラーが起きます。
/*情報追加部分
*起きるエラー
let nowTimeCoordinate = locationManager(_ manager: CLLocationManager didUpdateLocations locations: [CLLocation])
を以下のようにxcode様に言われるがまま訂正
let nowTimeCoordinate = locationManager(_, manager: CLLocationManager, didUpdateLocations, locations: [CLLocation])
起きるエラー
use of unresolved identifier 'didUpdateLocations'
情報追加終わり*/
直す前のコード
swift
1func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){ 2 let location = locations.first 3 let nowTimelatitude = location?.coordinate.latitude//Optional<Double> 4 let nowTimelongitude = location?.coordinate.longitude//Optional<Double> 5 6 print("現在位置の緯度: \(nowTimelatitude!)\n現在位置の経度: \(nowTimelongitude!)") 7 8// return (nowTimelatitude,nowTimelongitude) as! (nowTimelatitude: Double, nowTimelongitude: Double) 9 } 10 11// return (今の座標) ができればいい 12let nowTimeCoordinate = locationManager(_ manager: CLLocationManager didUpdateLocations locations: [CLLocation]) 13
試行錯誤後コード
swift
1 private func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) -> (Double,Double){ 2 let location = locations.first 3 let nowTimelatitude = location?.coordinate.latitude//Optional<Double> 4 let nowTimelongitude = location?.coordinate.longitude//Optional<Double> 5 6 print("現在位置の緯度: \(nowTimelatitude!)\n現在位置の経度: \(nowTimelongitude!)") 7 8// print(type(of: nowTimelatitude))//Optional<Double>型判定 9// print(type(of: nowTimelongitude))//Optional<Double> 10 11 return (nowTimelatitude,nowTimelongitude) as! (nowTimelatitude: Double, nowTimelongitude: Double) //戻り値で指定すれば他の関数でも使えるのでは? 12 } 13 14let nowTimeCoordinate = locationManager(_, manager: CLLocationManager, didUpdateLocations, locations: [CLLocation]) //うまくいかない。現在位置の戻り値を受け取りたい。
xcodeさんに指摘されたとおり直したので、どうしてprivateをつけなきゃいかんのかreturn () as!()のas!以下もよく分かってないが、お聞きしたいのは引数が簡単な整数などではないこの関数の戻り値を関数の外で定義するときにスコープの関係でエラーが出るのはどうしたらいいのかということなのです。
回答1件
あなたの回答
tips
プレビュー