通知オブジェクトとして新しい場所で位置情報の更新を送信しようとしています。私がするとき、私は通知からのデータにアクセスしようとすると、 "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);
LOL。私は%fを始めましたが、私は他の欠陥があり、クラスで遊んでいました。他のいくつかの欠陥を修正した後、私はそれを元に戻すのを忘れました。私は元の問題を解決しなかったと思っていました。見つけてくれてありがとう:) – TERACytE
問題はありません。時には小さなものが欠けてしまうこともあります。 – cocoakomali