2012-02-12 4 views
2

通知オブジェクトとして新しい場所で位置情報の更新を送信しようとしています。私がするとき、私は通知からのデータにアクセスしようとすると、 "EXC_BAD_ACCESS"エラーが表示されます。私が "po location"を実行するとデータが見えますが、私がなぜそれを得ることができないのかはわかりません。オブザーバーを設定するときに、オブジェクト変数をメンバー変数に代入しようとしましたが、locationUpdateは呼び出されませんでした。NSNotificationオブジェクトからデータを取得するにはどうすればよいですか?

はここで(つまりARCが有効になっている注意してください)私のコードです:

Notification.h

// 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]; 
} 

//

@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 。あなたはオブジェクトとして浮動小数点値にアクセスしようとしています!

+0

LOL。私は%fを始めましたが、私は他の欠陥があり、クラスで遊んでいました。他のいくつかの欠陥を修正した後、私はそれを元に戻すのを忘れました。私は元の問題を解決しなかったと思っていました。見つけてくれてありがとう:) – TERACytE

+0

問題はありません。時には小さなものが欠けてしまうこともあります。 – cocoakomali

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

+0

確かに、プログラマになるという利点があります。あなたの現在の実装の問題は、より多くの情報、複数の情報、または通知を送信したオブジェクトを知る必要がある場合に発生します。同じドキュメントの次の行: "オブジェクトは、通知のポスターがその通知のオブザーバーに送信したいオブジェクトです(通常、通知を送信したオブジェクトです)。 " – ColdLogic

関連する問題