对代表所有权的困惑

时间:2012-08-12 17:04:01

标签: objective-c delegates

  

例如,WhereamiViewController拥有CLLocationManager,   并且CLLocationManager的代表是   WhereamiViewController

当所有WhereamiViewController具有引用类CLLocationManager的对象的实例变量时,我对WhereamiViewController如何拥有CLLocationManager类感到困惑。有人可以帮我清除这个概念吗?

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate> {
    CLLocationManager *locationManager;
}
@end

#import "WhereamiViewController.h"

@implementation WhereamiViewController

-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        locationManager = [[CLLocationManager alloc] init];
        [locationManager setDelegate: self];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        [locationManager startUpdatingLocation];
    }
    return self;
}

@end

1 个答案:

答案 0 :(得分:0)

根据{{​​3}}

CLLocationManager的代表未被保留

  

@property( 分配 ,非原子)id委托

因此,您的委托不会被CLLocationManger保留,因此没有保留循环。