2014-12-29 3 views
0

저는 앱으로 위치를 프로그래밍하는 법을 배우려고합니다. 코드 전체가 locationManager와 델리게이트와 정말로 혼동을 일으켰습니다. locationManager라는 함수를 선언 할 때 어떤 일이 일어나고 있는지 전혀 알 수 없습니다. 이 함수를 정의하고 있습니다, locationManager, 맞습니까? 2 개의 매개 변수가 있습니다. 정확히이 locationManager를 호출하는 것은 무엇입니까? Complete iOS Developer Course을 통과 할 때 그는 locationManager 스 니펫을 가져 와서 복사하고 붙여 넣을 때 수행중인 작업의 원리를 설명하지 않은 채 붙여 넣습니다. "locationManager (...)"를 호출하는 코드 라인이 있습니까? 그렇다면, 이것이 어디에서 발생합니까? 내 두뇌는 슈퍼 클래스에서 상속 된 함수 인 CLLocationManagerDelegate가 작동하도록하려면 재정의 할 필요가 없다고 생각하고 있습니다. 그리고 당신은 대의원들이 정확히 어떻게 일하는 지에 대해 약간의 직감을 줄 수 있습니까?Swift에서 위임 메서드는 어떻게 작동합니까?

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 



@IBOutlet var myMap : MKMapView! 

var manager:CLLocationManager! 


override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 


    // Core Location 
    manager = CLLocationManager() 
    manager.delegate = self 
    manager.desiredAccuracy = kCLLocationAccuracyBest 
    manager.requestWhenInUseAuthorization() 
    manager.startUpdatingLocation() 




} 


func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) { 

    var userLocation:CLLocation = locations[0] as CLLocation 

    var latitude:CLLocationDegrees = userLocation.coordinate.latitude 

    var longitude:CLLocationDegrees = userLocation.coordinate.longitude 

    var latDelta:CLLocationDegrees = 0.01 

    var lonDelta:CLLocationDegrees = 0.01 

    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta) 

    var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude) 

    var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span) 

    myMap.setRegion(region, animated: true) 

} 

func locationManager(manager:CLLocationManager, didFailWithError error:NSError) 
{ 
    println(error) 
} 

도움 주셔서 감사합니다.

답변

1

CLLocationManagerDelegate 프로토콜에 정의 된이 대리자 메서드는변수에서 인스턴스화하고 참조하는 CLLocationManager 개체에 의해 호출됩니다. 따라서 CLLocationManager 객체를 인스턴스화했으며, 위치 업데이트가있을 때이를 알려달라고 요청했으며, 구현 한 위임 메소드를 호출하여이를 수행합니다.

당신 말 :

내 뇌가 수퍼 클래스, CLLocationManagerDelegate에서 상속있어 기능을, 만약 당신이 순서가 작동하도록하기 위해 그것을 무시할 수 없을 것으로 생각하고 계속?

CLLocationManagerDelegate은 클래스가 아닙니다. 그것은 "프로토콜"입니다. 이 함수는 delegate 개체 (이 경우보기 컨트롤러)를 구현할 수 있는지/구현해야 하는지를 정의합니다. 따라서 무시할 부분이 없습니다.

+0

좋습니다. 위임 메서드는 선택 사항입니다. 맞습니까? 그렇다면 Swift는 이러한 메서드를 코드에 구현 한 것을 어떻게 알 수 있습니까? – rb612

+2

이 경우 NSObject가 제공하는'respondsToSelector' 메소드에 의존합니다 (그리고이 모든 Cocoa Touch 클래스가 내려지는 것). 순수 스위프트 인 경우 프로토콜의 선택적 방법에 대한 적합성을 확인하는 또 다른 메커니즘이 있습니다 ([The Swift Language : Protocols] (https://developer.apple.com/library/prerelease/ios/documentation/Swift /Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID267)), 여기에는 그다지 관련이 없습니다. – Rob

+2

@ rb612 [위임] (https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html) 또는 [프로토콜] (https : // developer Core Competencies 문서의 [library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Protocol.html] 토론 또는 [Programming in Objective-C : Protocols] (https://developer.apple. co.kr/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html). Objective-C에 초점을 맞추었지만 원리는 같습니다. – Rob

관련 문제