委托方式不会触发(为空)。使用ARC [英] Delegate method won't fire (is null). Using ARC

查看:150
本文介绍了委托方式不会触发(为空)。使用ARC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让我的委托方式触发。我有以下代码:

I'm having trouble getting my delegate method to fire. I have the following code:

@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有关?分配位置的对象符合 LocationDelegate 。我也设定了自己的代表。但是代理方法不是触发,如果我输出它,它是空的。

Used to work fine for other projects, so wondering if it has to do with ARC? The object that allocates Location does conform to LocationDelegate. I am also setting the delegate to self. But the delegate method just isn't firing, and if I output it, it's null.

谢谢

推荐答案

从您粘贴到这里的代码,我看不到任何持有您的代理(保留的引用)

From just the code you've pasted here, I don't see anything that is holding on to your delegate (a retained reference).

您粘贴的所有内容都具体显示了您的方式,以避免此代码保留代理。

Everything you've pasted specifically shows you going out of your way to not have the delegate retained by this code.

如果你想要这样做,那么你最好确保它被保留在其他地方 - 否则ARC将会正确地得出结论,因为没有人有强大的(保留)参考给代表,它是安全的(和适当的)释放它。

If thats how you want it to be, then you better be sure it's been retained elsewhere - otherwise ARC will correctly conclude that since no one has a strong (retained) reference to the delegate, that it's safe (and proper) to release it.


分配位置的对象符合LocationDelegate。 I
也将委托人设置为自己。

The object that allocates Location does conform to LocationDelegate. I am also setting the delegate to self.

谁有强烈的引用该对象?

And who has a strong reference to that object?

这篇关于委托方式不会触发(为空)。使用ARC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆