2012-02-12 1 views
2

알림 개체로 새 위치로 위치 업데이트를 보내려고합니다. 내가 할 때, 알림에서 데이터에 액세스하려고하면 "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); 

답변

4

변경 %의 @에서 NSLog의 형식 지정자 % f를 . 객체로 float 값에 액세스하려고합니다!

+0

을 LOL. 시작하려면 % f을 (를) 가지고 있었지만, 다른 결함이 있었고 수업을 가지고 놀고있었습니다. 다른 결함을 수정 한 후에 다시 변경하는 것을 잊었습니다. 나는 원래의 문제를 해결하지 못했다고 생각했다. 그것을 주셔서 감사합니다 :) – TERACytE

+0

괜찮습니다. 때로는 작은 일들을 놓치는 경우가 있습니다. – cocoakomali

2

NSNotifications에는 userInfo이라는 사전이있어 알림과 함께 전송하려는 정보를 넣을 수 있습니다.

코드를 수정하려고합니다. 거꾸로 가고 ... 나와 맺어졌습니다. 일반적으로 NSNotification 클래스는 사용되지 않았으므로 사용되지 않습니다.

이 상황을 해결하려면 여러 가지 작업을 수행해야합니다. NSNotification 게시물의 object 값은 전달하려는 개체가 아닌 NSNotification을 게시하는 개체입니다.

CLLocation 개체를 사전에 추가하고 userInfo 사전으로 전달합니다. 또한이 사용자 정의 알림 클래스에 대한 이유가 없습니다. 그래서 당신은 이제 우리가 통지와 위치 정보를 게시하는 Notification.hNotification.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