2

新しい場所を通知オブジェクトとして、場所の更新を送信しようとしています。通知からデータにアクセスしようとすると、「EXC_BAD_ACCESS」エラーが発生します。「po location」を実行するとデータが表示されるのですが、取得できない理由がよくわかりません。オブザーバーを設定するときに、オブジェクト パラメーターをメンバー変数に代入しようとしましたが、locationUpdate が呼び出されません。

これが私のコードです(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

2 に答える 2

4

NSLog のフォーマット指定子を %@ から %f に変更します。float 値にオブジェクトとしてアクセスしようとしています!

于 2012-02-12T17:27:39.700 に答える
2

NSNotificationsuserInfo通知と一緒に送信したい情報をどこに置くことができるかという辞書を用意してください。

私はあなたのコードを少し逆向きに修正するつもりですので、我慢してください. NSNotification クラスは通常 (または意図して) 使用されているため、実際には使用していません。

この状況を解決するために、私たちはたくさんのことをしなければなりません。投稿のobject値は、一緒に渡したいオブジェクトではなく、NSNotification投稿しているオブジェクトです。NSNotification

CLLocationオブジェクトをディクショナリに追加し、それをuserInfoディクショナリとして渡します。あなたが持っているこのカスタム通知クラスのものにも理由はありません。Notification.hしたがって、 andを取り除くことができます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);
}

また、View Controller ヘッダーを次のように変更します。

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

@end
于 2012-02-12T18:01:33.290 に答える