简体   繁体   English

在类方法中使用委托,objective-c

[英]using delegates in class method, objective-c

I've got a class method that wants to use CLLocationManager and some of its delegate methods. 我有一个想要使用CLLocationManager及其某些委托方法的类方法。

What is the best way to access the delegate methods from the class method, since I don't have a true instance level "self"? 从类方法访问委托方法的最佳方法是什么,因为我没有真正的实例级别“self”? I could instantiate a self and use as the delegate, which would let the delegate methods run, but doesn't show how to get the data out. 我可以实例化一个self并将其用作委托,这将使委托方法运行,但不会显示如何获取数据。 What's the best approach? 什么是最好的方法?

// desired end function, which runs a block when location is found
[SFGeoPoint geoPointForCurrentLocationInBackground:^(SFGeoPoint *geoPoint, NSError *error) {
    if (!error) {
        // do something with the new geoPoint
        NSLog(@"GeoPoint: %@", geoPoint);
    }
}];


// SFGeoPoint class, key points
static CLLocationManager *_locationManager = nil;

// get geo point for current location and call block with it
+ (void) geoPointForCurrentLocationInBackground:( void ( ^ )( SFGeoPoint*, NSError* ) ) locationFound {

    SFGeoPoint *point = [[SFGeoPoint alloc] init];

    _locationManager = [[CLLocationManager alloc] init];

    // ?????????
    _locationManager.delegate = self;  // this gives a warning about incompatible pointer type assigning Delegate from Class; 
    _locationManager.delegate = point;  // could work, but how to get feedback?  

    _locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [_locationManager startUpdatingLocation];

    [_locationManager startUpdatingLocation];
    locationFound(point, nil);
}


/////////// Core Location Delegate
+ (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation {

    [_locationManager stopUpdatingLocation];

    if (_locationBlock) {
        _locationBlock(newLocation);
    }
}

I would rework what you're doing and not use class methods. 我会重做你正在做的事情,而不是使用类方法。 Instead, use a shared instance singleton, which will allow you to write your code almost identically but gives you an instance to work with and therefore store variables and assign delegates. 相反,使用共享实例单例,这将允许您几乎完全相同地编写代码,但为您提供了一个实例,因此可以存储变量并分配代理。

Just in case you are unfamiliar with the syntax: 以防您不熟悉语法:

+ (instancetype) shared
{
    static dispatch_once_t once;
    static id sharedInstance;
    dispatch_once(&once, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

Then just change all of your + (class) methods to be - (instance) methods and access your class with [[MyClass shared] doWhatever]; 然后只需将所有+ (类)方法更改为- (实例)方法,并使用[[MyClass shared] doWhatever];访问您的类[[MyClass shared] doWhatever];

edit with optional wrapper: 使用可选包装器编辑:

If you really wanna call the method without an instance you can write a wrapper that would do something like this: 如果你真的想在没有实例的情况下调用该方法,你可以编写一个包装器来执行类似这样的操作:

+ (void) doWhatever
{
    [[self shared] doWhatever];
}

That said I would generally not recommend doing this because you are not saving much code and adding possible confusion in the future as to what kind of method this actually is from the caller's perspective. 这就是说我通常不会建议这样做,因为你没有节省太多的代码,并且在将来可能会混淆从调用者的角度来看这实际上是什么样的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM