如何在Swift 2.0中将[dispatch_block_t]转换为NSArray?

时间:2015-11-12 13:44:34

标签: ios swift swift2 grand-central-dispatch

我创建了一个像这样的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")
    }
}

}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我借助以下答案{[3}}

将[dispatch_block_t]转换为NSArray