알림 개체로 새 위치로 위치 업데이트를 보내려고합니다. 내가 할 때, 알림에서 데이터에 액세스하려고하면 "EXC_BAD_ACCESS"오류가 발생합니다. "po location"을 실행하면 데이터를 볼 수 있지만 왜 데이터를 얻을 수 없는지 불분명합니다. 옵저버를 설정할 때 object 변수를 멤버 변수에 할당하려고 시도했지만 locationUpdate는 호출되지 않습니다.NSNotification 객체에서 데이터를 얻으려면 어떻게해야합니까?
// 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
: 여기 내 코드 (즉, ARC가 활성화되어주의)이다@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