2013-02-09 38 views
0

我試圖實現我的LocationManager如下:無法設置屬性與定製的setter

開始更新使用者的位置

- (void)startStandardUpdates { 
    if (self.locationManager == nil) { 
     self.locationManager = [[CLLocationManager alloc] init]; 
    } 

    self.locationManager.delegate = self; 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

    // Set a movement threshold for new events 
    self.locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters; 

    [self.locationManager startUpdatingLocation]; 

    CLLocation *currentLocation = self.locationManager.location; 

    if (currentLocation) { 
     self.currentLocation = currentLocation; 
    } 
} 

實現自定義的制定者MY PROPERTY發送通知

- (void)setCurrentLocation:(CLLocation *)currentLocation { 
    self.currentLocation = currentLocation; 
    NSLog(@"%f", currentLocation.coordinate.latitude); 

    //Notify the app of the location change 
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:self.currentLocation forKey:kIFLocationKey]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [[NSNotificationCenter defaultCenter] postNotificationName:kIFLocationChangeNotification object:nil userInfo:userInfo]; 
    }); 
} 

的問題是: 當我運行該應用程序,我在調試模式和叔得到一個「BAD_EXEC(代碼2)」中的錯誤「setCurrentLocation」方法的消息他的應用已掛斷。但我不明白這個問題。我錯過了什麼嗎?就我所見,在「startStandardUpdates」中,如果位置管理器找到用戶的位置,則使用我的自定義設置器「self.currentLocation = currentLocation」更新屬性「currentLocation」。

感謝您的幫助提前! 關於 塞巴斯蒂安

回答

2

你實施setter的方式是問題。 self.currentLocation = ...導致調用者被調用,並且在實現setter時調用它,導致無限循環。如下所示合成你的ivar,並且只在setters(和getters)中使用_variablename。

@synthesize currentLocation = _currentLocation;

//二傳手

- (void)setCurrentLocation:(CLLocation *)currentLocation { 
    _currentLocation = currentLocation; 
    NSLog(@"%f", currentLocation.coordinate.latitude); 

    //Notify the app of the location change 
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:self.currentLocation forKey:kIFLocationKey]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [[NSNotificationCenter defaultCenter] postNotificationName:kIFLocationChangeNotification object:nil userInfo:userInfo]; 
    }); 
}