我已经像这样创建了一个dispatch_block_t数组,

let actions: [dispatch_block_t] = [
    {self.pickImages()},
    {self.takePicture()}]


现在,我想将此[dispatch_block_t]转换为NSArray,以便将其传递给函数。我尝试将其强制转换为NSArray。

let actionsArray: NSArray = actions as NSArray


但是我遇到一个错误。


  [dispatch_block_t]无法转换为NSArray


更新:完整代码,

import UIKit
import Foundation
import MobileCoreServices
import MediaPlayer

class AttachFilesViewController: UIViewController, UINavigationBarDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, MultiImagePickerControllerDelegate, MPMediaPickerControllerDelegate, UITableViewDataSource, UITableViewDelegate {

var tableView: UITableView = UITableView()

var imgPkr: MultiImagePickerController!     //For picking image(s)
var cameraPkr: UIImagePickerController = UIImagePickerController()    //For taking a picture

var menuView: btSimplePopUP!

var rightBarButtonItem = UIBarButtonItem()

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor.whiteColor()

    //Creating Right bar button item
    let buttonRight = UIButton(type: UIButtonType.Custom)
    buttonRight.frame = CGRectMake(0, 0, 30, 30)
    buttonRight.setImage(UIImage(named:"add.png"), forState: UIControlState.Normal)
    buttonRight.addTarget(self, action: "openMenu:", forControlEvents: UIControlEvents.TouchUpInside)
    rightBarButtonItem = UIBarButtonItem(customView: buttonRight)

    tableView.delegate = self
    tableView.dataSource = self

    self.view.addSubview(tableView)
}

func openMenu(sender: UIButton){
    if(menuView == nil){
        let imgs = [UIImage(named: "pickImage.png")!,
            UIImage(named: "takePicture.png")!,
            UIImage(named: "pickVideo.png")!,
            UIImage(named: "shootVideo.png")!,
            UIImage(named: "recordAudio.png")!,
            UIImage(named: "quit.png")!] as NSArray

        let titles = ["Images",
            "Capture",
            "Videos",
            "Shoot",
            "Record",
            "Cancel"] as NSArray

                let actions: [dispatch_block_t] = [
                    {self.pickImages()},
                    {self.takePicture()},]

                let actionsArray = NSArray(array: actions)

                menuView = btSimplePopUP(itemImage: imgs as [AnyObject],
                    andTitles: titles as [AnyObject],
                    andActionArray:  actionsArray as [AnyObject],
        addToViewController: self)

                            self.view.addSubview(menuView)
            menuView.show(BTPopUpAnimation.UPAnimateWithFade)
            }else{
        if(!menuView.isDescendantOfView(self.view)){
            self.view.addSubview(menuView)
            menuView.show(BTPopUpAnimation.UPAnimateWithFade)
        }
    }
}

//For picking image(s) from gallery
func pickImages(){
    videoPkr = nil
    globalStruct.groupType = true
    imgPkr = MultiImagePickerController()
    imgPkr.pickerDelegate = self
    self.navigationController!.presentViewController(imgPkr, animated: true) {}
}

//For taking a picture with camera
func takePicture(){
    imgPkr = nil
    videoPkr = nil
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
        self.cameraPkr = UIImagePickerController()
        self.cameraPkr.delegate = self  //UIImagePicker delegate
        self.cameraPkr.allowsEditing = false
        self.cameraPkr.sourceType = .Camera
        self.cameraPkr.cameraCaptureMode = .Photo
        self.cameraPkr.modalPresentationStyle = .FullScreen
        self.navigationController!.presentViewController(self.cameraPkr, animated: true, completion: nil)
    }else{
        SCLAlertView().showError("No Camera!!", subTitle: "Sorry.. Your device does not have a camera!", closeButtonTitle: "Okay")
    }
}


}

任何帮助将不胜感激。

最佳答案

我借助以下答案How to create an array of dispatch_block_t in Swift 2.0?将[dispatch_block_t]转换为NSArray。

09-04 21:47