委托方法不会触发(为空)。使用ARC

问题描述:

我无法让我的委托方法触发。我有以下代码:委托方法不会触发(为空)。使用ARC

@class Location; 
@protocol LocationDelegate 
- (void)location:(Location*)location foundLocation:(CLLocation*)location; 
- (void)location:(Location*)location failedToLocateUserWithError:(NSError*)error; 
@end 

@interface Location : NSObject { 
    __unsafe_unretained id <LocationDelegate> delegate; 
} 

@property (nonatomic, assign) id <LocationDelegate> delegate; 

@end 

... 
@implementation Location 

@synthesize delegate; 

- (void)startUpdatingLocation { 
    NSLog(@"%@", delegate); // prints '(null)' 
    ... 

} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    NSLog(@"%@", delegate); // prints '(null)' 
    [delegate location:self foundLocation:newLocation]; 
} 

@end 

用于正常工作的其他项目,所以想知道,如果它与ARC办?分配Location的对象确实符合LocationDelegate。我也将代表设置为自我。但委托方法不会触发,如果我输出它,它是空的。

感谢

从刚才你在这里粘贴代码,我没有看到任何持有到您的委托(所保持的参考)。

您粘贴的所有东西都明确表明您不想让代码保留在此代码中。

如果这就是你想要的那样,那么你最好确定它被保留在其他地方 - 否则ARC会正确地得出结论,因为没有人对代表有强烈的(保留的)引用,所以它是安全的(并且是适当的)释放它。

分配Location的对象确实符合LocationDelegate。我也在将代表设置为自我。

谁对这个对象有很强的参考?

+0

我试过把它改为(非原子,强),没有unsafe_unretained,但仍然没有。 – runmad

+0

摆脱伊娃。在你引用委托的行上(像这样:'[delegate location ....')使用'self.delegate'代替。 – Steve

+0

还没有/ null – runmad

无论你在哪里分配Location你有分配给你的代表吗?例如: 例如: Location *location = [[Location alloc] init]; location.delegate = self;

+0

他特别说他是:'我也设置代表自己.' – Steve

+0

是的,我正在做所有这些。 – runmad

+0

尝试在@protocol LocationDelegate后添加 Rick