2017-11-04 9 views
0

프로그래밍 초보자입니다. 필자는 locationManager 함수에서 좌표를 가져와 Firebase에 연결할 수있는 방법을 찾으려고합니다. 계속 오류가 발생합니다 : 'CLLocation!'을 입력하십시오. 회원 '위도'가 없습니다.코어 위치 데이터 추출 후 Firebase에 업로드

locationManager 함수 외부에서 myLocation 변수 좌표를 가져 오는 방법을 알아 내려고 노력했습니다.

어떤 조언이 도움이 될 것입니다!

import UIKit 
import Firebase 
import CoreLocation 

class SignupViewController: UIViewController,CLLocationManagerDelegate { 


    let manager = CLLocationManager() 
    let currentUserLocation = CLLocation! 



    override func viewDidLoad() { 
     super.viewDidLoad() 

     let cloud = Storage.storage().reference(forURL: "gs://applesandoranges.appspot.com") 

     ref = Database.database().reference() 
     userStorage = cloud.child("users") 

     manager.delegate = self 
     manager.desiredAccuracy = kCLLocationAccuracyBest 
     manager.requestWhenInUseAuthorization() 
     manager.startUpdatingLocation() 



    } 


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     let location = locations[0] 
     let myLocation = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 

은}

}

답변

0

난 당신이 현재 사용자의 위치에 대한 속성을 선언하려고했던 생각합니다. 대신에이 선언을 시도해보십시오 대리자 기능에 다음

var currentUserLocation: CLLocation? = nil 

를 (위치 관리자가 갱신했다), 당신은 단지 속성을 설정합니다

currentUserLocation = locations[0] 

내가 그 위치 값을 얻을하는 방법에 대한 질문에 응답 생각을 함수에서 벗어나 뷰 컨트롤러 (의 속성)로 이동합니다.

의미가 있습니까?

PS -

엑스 코드가 사용자의 선언에 오류를보고하지 않았다 궁금 해요 : 엑스 코드의

let currentUserLocation = CLLocation! 

내 버전은 클래스 이름 뒤에 멤버 이름이나 이니셜을 가진 주장한다. 그래서 클래스 자체에 CLLocation.self을 입력하거나 인스턴스를 생성하려면 CLLocation(args)을 입력해야합니다. 그것이 이상한 오류 메시지의 출처였습니다. 즉, 해당 유형의 유형 및 인스턴스를 혼란스럽게합니다.

+0

정말 고마워요! CLLocation을 변경하기 위해 CLLocationCoordinate2D로 변경해야했습니다. –

관련 문제