如何将这个ObjectiveC块存储在swift变量中?

iOS*_*com 3 cocoa cocoa-touch objective-c objective-c-blocks swift

这是objectiveC块

@property (copy) void (^anObjcBlock)();

anObjcBlock = ^{
    NSLog(@"Yea man this thing works!!");
};
NSMutableArray *theArrayThatHasTheBlockInItAtIndexZero = [NSmutableArray array];
[theArrayThatHasTheBlockInItAtIndexZero addObject:anObjBlock];
Run Code Online (Sandbox Code Playgroud)

这是我迅速做的:

var theBlock: ( ()->Void) ?

theBlock = theArrayThatHasTheBlockInItAtIndexZero[0] as? ()->Void
//Now call the block
theBlock!()
Run Code Online (Sandbox Code Playgroud)

但是与此同时我得到运行时错误。基本上,由于失败,该theBlock = theArrayThatHasTheBlockInItAtIndexZero[0] as? ()->Void语句将为theBlocknil as?。当我将语句更改theBlock = theArrayThatHasTheBlockInItAtIndexZero[0] as! ()->Void为时,出现运行时错误在此处输入图片说明

我不确定该怎么办。这是一个空项目,实际上没有任何代码。

Mat*_*ini 5

在这种情况下,问题似乎来自NSMutableArray

[NSMutableArray objectAtIndex:]在Objective-C中返回id,Swift 将其转换为AnyObject

如果尝试将AnyObject强制转换()-> Void,则会出现错误。

解决方法如下:

//Create your own typealias (we need this for unsafeBitcast)
typealias MyType = @convention(block) () -> Void

//Get the Obj-C block as AnyObject
let objcBlock : AnyObject = array.firstObject! // or [0]

// Bitcast the AnyObject Objective-C block to a "Swifty" Objective-C block (@convention(block)) 
//and then assign the result to a variable of () -> Void type

let block : () -> Void = unsafeBitCast(objcBlock, MyType.self)

//Call the block

block()
Run Code Online (Sandbox Code Playgroud)

该代码对我有用。


有趣的事实

如果您将Objective-C代码编辑为如下形式...

//Typedef your block type
typedef void (^MyType)();

//Declare your property as NSArray of type MyType
@property (strong) NSArray<MyType>* array;
Run Code Online (Sandbox Code Playgroud)

Swift现在将数组类型报告为[MyType]!

由于某些原因,NSMutableArray上的泛型似乎没有被Swift接受。

尽管如此,如果执行以下操作,就会收到运行时错误:

 let block : MyType? = array[0]
Run Code Online (Sandbox Code Playgroud)