如何在 Swift 2.0 中创建一个 dispatch_block_t 的数组?
How to create an array of dispatch_block_t in Swift 2.0?
我在 Swift 1.2 中有一个代码来创建一个 dispatch_block_t 的数组,它工作正常。但是相同的代码在 Swift 2.0 中会抛出错误。
var menuView: btSimplePopUP!
let actions: [dispatch_block_t] = [{self.pickImages()},
{self.takePicture()},
{self.pickVideos()},
{self.shootVideo()},
{self.recordAudio()},
{self.closeView()}]
menuView = btSimplePopUP(itemImage: imgs as [AnyObject],
andTitles: titles as [AnyObject],
andActionArray: actions as NSArray as [AnyObject],
addToViewController: self)
以上代码在 Swift 1.2 中运行良好。但在 Swift 2.0 中,它会抛出以下错误
[dispatch_block_t] is not convertible to NSArray
如何使用 dispatch_block_t 创建 NSArray?
更新:
我已经用下面的代码替换了上面的代码,
let actions: [Any] = [{self.pickImages()},
{self.takePicture()},
{self.pickVideos()},
{self.shootVideo()},
{self.recordAudio()},
{self.closeView()}]
menuView = btSimplePopUP(itemImage: imgs as [AnyObject],
andTitles: titles as [AnyObject],
andActionArray: actions as! [AnyObject],
addToViewController: self)
现在,之前的错误消失了。但是我在 运行 时间内收到以下错误,
fatal error: array element cannot be bridged to Objective-C
我们将不胜感激。
dispatch_block_t
不是继承自 AnyObject,它不是对象。
但是您可以修改代码并将 AnyObject
更改为 Any
,如下所示:
andActionArray : actions as [Any]
它应该适合你。
更新:
你的函数将参数作为 NSArray
,你可以简单地将你的数组转换为这种类型,这段代码适用于我的 swift 游乐场:
func pickImages() {}
func takePicture() {}
func shootVideo() {}
let actions: [dispatch_block_t] = [{pickImages()},
{takePicture()},
{shootVideo()}]
var actionArray: NSArray = actions as NSArray // pass it to the btSimplePopUP init
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = false
import Foundation
let a: dispatch_block_t = {
print("a")
}
let b: dispatch_block_t = {
print("b")
}
let arr = [a,b]
print(arr.dynamicType)
arr.forEach { (b) -> () in
b()
}
/* prints
Array<@convention(block) () -> ()>
a
b
*/
class Block {
var block: dispatch_block_t
init(block: dispatch_block_t){
self.block = block
}
}
let block1 = Block(block: a)
let block2 = Block(block: b)
let arr2: NSArray = [block1,block2]
print(arr2)
arr2.forEach { (p) -> () in
(p as? Block)?.block()
}
/* prints
(
Block,
Block
)
a
b
*/
我在 Swift 1.2 中有一个代码来创建一个 dispatch_block_t 的数组,它工作正常。但是相同的代码在 Swift 2.0 中会抛出错误。
var menuView: btSimplePopUP!
let actions: [dispatch_block_t] = [{self.pickImages()},
{self.takePicture()},
{self.pickVideos()},
{self.shootVideo()},
{self.recordAudio()},
{self.closeView()}]
menuView = btSimplePopUP(itemImage: imgs as [AnyObject],
andTitles: titles as [AnyObject],
andActionArray: actions as NSArray as [AnyObject],
addToViewController: self)
以上代码在 Swift 1.2 中运行良好。但在 Swift 2.0 中,它会抛出以下错误
[dispatch_block_t] is not convertible to NSArray
如何使用 dispatch_block_t 创建 NSArray?
更新:
我已经用下面的代码替换了上面的代码,
let actions: [Any] = [{self.pickImages()},
{self.takePicture()},
{self.pickVideos()},
{self.shootVideo()},
{self.recordAudio()},
{self.closeView()}]
menuView = btSimplePopUP(itemImage: imgs as [AnyObject],
andTitles: titles as [AnyObject],
andActionArray: actions as! [AnyObject],
addToViewController: self)
现在,之前的错误消失了。但是我在 运行 时间内收到以下错误,
fatal error: array element cannot be bridged to Objective-C
我们将不胜感激。
dispatch_block_t
不是继承自 AnyObject,它不是对象。
但是您可以修改代码并将 AnyObject
更改为 Any
,如下所示:
andActionArray : actions as [Any]
它应该适合你。
更新:
你的函数将参数作为 NSArray
,你可以简单地将你的数组转换为这种类型,这段代码适用于我的 swift 游乐场:
func pickImages() {}
func takePicture() {}
func shootVideo() {}
let actions: [dispatch_block_t] = [{pickImages()},
{takePicture()},
{shootVideo()}]
var actionArray: NSArray = actions as NSArray // pass it to the btSimplePopUP init
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = false
import Foundation
let a: dispatch_block_t = {
print("a")
}
let b: dispatch_block_t = {
print("b")
}
let arr = [a,b]
print(arr.dynamicType)
arr.forEach { (b) -> () in
b()
}
/* prints
Array<@convention(block) () -> ()>
a
b
*/
class Block {
var block: dispatch_block_t
init(block: dispatch_block_t){
self.block = block
}
}
let block1 = Block(block: a)
let block2 = Block(block: b)
let arr2: NSArray = [block1,block2]
print(arr2)
arr2.forEach { (p) -> () in
(p as? Block)?.block()
}
/* prints
(
Block,
Block
)
a
b
*/