以下代码是用 Swift 2.0 编写的,用于创建一个数组dispatch_block_t
let a: dispatch_block_t = {
self.pickImages()
}
let b: dispatch_block_t = {
self.takePicture()
}
let c: dispatch_block_t = {
self.pickVideos()
}
let d: dispatch_block_t = {
self.shootVideo()
}
let e: dispatch_block_t = {
self.recordAudio()
}
let f: dispatch_block_t = {
self.closeView()
}
let block1 = Block(block: a)
let block2 = Block(block: b)
let block3 = Block(block: c)
let block4 = Block(block: d)
let block5 = Block(block: e)
let block6 = Block(block: f)
let actionsArray: NSArray = [block1, block2, block3, block4, block5, block6]
该类Block
定义如下,
class Block: NSObject, NSCopying {
var block: dispatch_block_t
init(block: dispatch_block_t){
self.block = block
}
func copyWithZone(zone: NSZone) -> AnyObject {
return self.block as! AnyObject
}
}
我可以使用上面的代码创建一个 dispatch_block_t 数组。但是,我需要将此数组作为参数传递给另一个函数,并且在传递此数组时遇到问题。
我在按钮单击事件上调用此函数,
menuView = btSimplePopUP(itemImage: imgs as [AnyObject],
andTitles: titles as [AnyObject],
andActionArray: actionsArray as [AnyObject],
addToViewController: self)
执行以下代码段时出现错误,
- (instancetype)initWithImage:(UIImage *)image title:(NSString *)title action:(dispatch_block_t)action {
if ((self = [super init])) {
_title = [title copy];
_imageView = [[UIImageView alloc]initWithImage:image];
_action = [action copy];
}
return self;
}
错误是,
无法将类型“()->()”(0x15164018)的值转换为“Swift.AnyObject”(0x101e500c)。
我在类copyWithZone
函数中收到此错误Block
。
btSimplePopUp 的完整源代码可以在这里查看, https://github.com/balram3429/btSimplePopUp/blob/master/btSimplePopUp/btSimplePopUP.m