是否可以使用标准属性语法将块作为属性?
ARC有什么变化吗?
在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) Cf Apple在Swift上的网页:https://developer.apple.com/swift/
Swift中的块是否像objective-c一样?它们是如何创建和调用的?
如何在Swift中执行异步请求?
在swift中创建与块相关的内存泄漏是否容易?如果是的话,你会如何避免它们?