小编Jay*_*Dub的帖子

将闭包存储为Swift中的变量

在Objective-C中,您可以定义块的输入和输出,存储传递给方法的其中一个块,然后再使用该块:

// in .h

    typedef void (^APLCalibrationProgressHandler)(float percentComplete);
    typedef void (^APLCalibrationCompletionHandler)(NSInteger measuredPower, NSError *error);

    // in .m

    @property (strong) APLCalibrationProgressHandler progressHandler;
    @property (strong) APLCalibrationCompletionHandler completionHandler;

    - (id)initWithRegion:(CLBeaconRegion *)region completionHandler:(APLCalibrationCompletionHandler)handler
    {
        self = [super init];
        if(self)
        {
            ...
            _completionHandler = [handler copy];
            ..
        }

        return self;
}

- (void)performCalibrationWithProgressHandler:(APLCalibrationProgressHandler)handler
{
    ...

            self.progressHandler = [handler copy];

     ...
            dispatch_async(dispatch_get_main_queue(), ^{
                _completionHandler(0, error);
            });
     ...
}
Run Code Online (Sandbox Code Playgroud)

所以我试图在Swift中做等效的事情:

var completionHandler:(Float)->Void={}


init() {
    locationManager = CLLocationManager()
    region = CLBeaconRegion()
    timer = NSTimer()
}

convenience init(region: CLBeaconRegion, …
Run Code Online (Sandbox Code Playgroud)

closures objective-c-blocks swift

132
推荐指数
5
解决办法
9万
查看次数

标签 统计

closures ×1

objective-c-blocks ×1

swift ×1