2012-02-12 153 views
2

我試圖發送位置更新與新位置作爲通知對象。當我這樣做時,當我嘗試訪問通知中的數據時,會收到「EXC_BAD_ACCESS」錯誤。如果我執行「po location」,我會看到數據,但是我不清楚爲什麼我無法獲取它。設置觀察者時,我也嘗試將對象參數分配給成員變量,但是不會調用locationUpdate。如何從NSNotification對象獲取數據?

這裏是我的代碼(注意,ARC已啓用):

// LocationController.h

@protocol LocationDelegateProtocol 
@required 
    - (void)locationUpdate:(CLLocation *)location; 
@end 

@interface LocationController : NSObject <CLLocationManagerDelegate> { 
    CLLocationManager *locationManager; 
    id delegate; 
} 

@property(nonatomic, retain) CLLocationManager *locationManager; 
@property(nonatomic, strong) id delegate; 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation; 

+ (LocationController *)sharedInstance; // this class is a singleton 

@end 

// LocationController.m

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    [Notification locationChanged:newLocation]; 
} 

// Notification.h

@interface Notification : NSObject 
    + (void)locationChanged:(CLLocation *)newLocation; 
@end 

extern NSString *const kLocationChanged; 

// Notification.m

NSString *const kLocationChanged = @"NewLocation"; 

[[NSNotificationCenter defaultCenter] postNotificationName:kLocationChanged object:newLocation]; 

// ViewController.h

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, LocationDelegateProtocol> { 
    ... 
} 
... 
- (void)locationUpdate:(CLLocation *)location; 

@end 

// ViewController.m

- (void)setupNotifications { 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(locationUpdate:) name:kLocationChanged object:nil]; 
    // I've tried setting object to a member var "CLLocation *objectFromNotification", but then locationUpdate() is never called. 
} 

- (void)locationUpdate:(NSNotification *)notification {  
    CLLocation *location = (CLLocation *) [notification object]; 
    // program receives signal "EXC_BAD_ACCESS" when executing NSLog below. I can see data inside location when I execute "po location". 
    NSLog(@"latitude = %@, longitude = %@",location.coordinate.latitude, location.coordinate.longitude); 

回答

4

改變你的NSLog格式說明從%@到%F 。您正試圖將float值作爲對象訪問!

+0

LOL。我有%f開始,但是我還有其他缺陷,並且正在上課。修復了一些其他缺陷後,我忘了將它改回來。我曾假設我沒有解決我原來的問題。感謝您的發現:) – TERACytE 2012-02-12 18:16:42

+0

沒問題。有時候,我們錯過了小事。 – cocoakomali 2012-02-13 20:44:52

2

NSNotifications有一個與他們的字典叫userInfo,你可以把你要與通知發送信息。

我要解決你的代碼有點倒退......所以裸陪我。您確實沒有使用NSNotification類,因爲它通常(或打算)被使用。我們不得不做很多事情。 NSNotification文章的object值是發佈NSNotification的對象,而不是您想要傳遞的對象。

CLLocation對象添加到字典中,並將其作爲userInfo字典傳入。這個自定義通知類也沒有任何理由。所以,你可以擺脫​​和Notification.m

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    NSString *const kLocationChanged = @"NewLocation"; 
    NSDictionary *locationDict = [NSDictionary dictionaryWithObject:newLocation forKey:@"Location"]; 
    [[NSNotificationCenter defaultCenter] postNotificationName:kLocationChanged object:nil userInfo:locationDict]; 
} 

所以,現在我們正在發佈該通知的位置信息。接下來,當你收到通知時處理它。

- (void)locationUpdate:(NSNotification *)notification {  
    CLLocation *location = [[notification userInfo] valueForKey:@"Location"]; 

    NSLog(@"latitude = %f, longitude = %f",location.coordinate.latitude, location.coordinate.longitude); 
} 

此外,您的視圖控制器頭更改爲以下:

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, LocationDelegateProtocol> { 
    ... 
} 
... 
- (void)locationUpdate:(NSNotification *)notif; 

@end 
+0

我對https://developer.apple上的文檔的解釋。com/library/mac /#documentation/Cocoa/Reference/Foundation/Classes/nsnotification_Class/Reference/Reference.html是可接受的:「該對象是通知的發佈者希望發送給該通知的觀察者的任何對象「 – TERACytE 2012-02-12 18:20:18

+0

事實上,你可以做任何你想做的事,那就是做程序員的好處。當您需要更多信息,多條信息或需要知道哪些對象發送了通知時,您當前實施的問題就會出現。來自同一文檔的下一行:「該對象是通知的發佈者希望發送給該通知的觀察者的任何對象(通常是發佈通知的對象)。該字典存儲其他相關對象(如果有的話) 「。 – ColdLogic 2012-02-12 18:44:54