Swift 展开ViewController时发现无(可选)

Swift 展开ViewController时发现无(可选),swift,forced-unwrapping,Swift,Forced Unwrapping,我正在构建一个非常简单的swift应用程序,它使用两个视图控制器——“ViewControler”和“AnimalChooserviewController”。First有一个简单的标签和一个带有一个项目的工具栏,可以将用户转移到第二个屏幕。在第二种情况下,使用自定义选择器。应用程序的全部目的是使用输出UILabel将用户从第二个视图控制器选择的内容显示到第一个视图控制器。我必须提到我是usong xCode 6.4(ios8) 我的问题是当我试图将presentedViewController

我正在构建一个非常简单的swift应用程序,它使用两个视图控制器——“ViewControler”和“AnimalChooserviewController”。First有一个简单的标签和一个带有一个项目的工具栏,可以将用户转移到第二个屏幕。在第二种情况下,使用自定义选择器。应用程序的全部目的是使用输出UILabel将用户从第二个视图控制器选择的内容显示到第一个视图控制器。我必须提到我是usong xCode 6.4(ios8)

我的问题是当我试图将presentedViewController强制转换为!ViewController应用程序崩溃,出现“致命错误:在展开可选值时意外发现零” (lldb)“例外情况”。如何解决这个问题,有什么建议吗? 以下是我在ViewControler中的代码:

class ViewController: UIViewController {
@IBOutlet weak var outputLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func displayAnimal(choosenAnimal: String, withSound choosenSound: String, fromComponent choosenComponent: String){
    self.outputLabel.text = "You changed \(choosenComponent) (\(choosenAnimal)(and the sound \(choosenSound))"
}

}
这是我在AnimalChooserviewController中的代码

class AnimalChooserViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{

let kComponentCount: Int = 2
let kAnimalComponent: Int = 0
let kSoundComponent: Int = 1

var animalNames: [String] = []
var animalSounds: [String] = []
var animalImages: [UIImageView] = []

@IBAction func dismisAnimalChooser(sender: AnyObject) {
    dismissViewControllerAnimated(true, completion: nil)
}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return kComponentCount
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if component == kAnimalComponent{
        return animalNames.count
    } else {
        return animalSounds.count
    }
}

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {

    if component == kAnimalComponent {
        let choosenImageView: UIImageView = animalImages[row]

        let workarroundImageView: UIImageView = UIImageView(frame: choosenImageView.frame)
        workarroundImageView.backgroundColor = UIColor(patternImage: choosenImageView.image!)

        return workarroundImageView
    } else {
        let soundLabel: UILabel = UILabel(frame: CGRectMake(0, 0, 100, 32))
        soundLabel.text = animalSounds[row]

        return soundLabel
    }
}

func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
    return 55.0
}

func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
    if component == kAnimalComponent{
        return 75.0
    } else {
        return 150.0
    }
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    let initialView : ViewController = presentedViewController as! ViewController

    if component == kAnimalComponent{
        let choosenSound: Int = pickerView.selectedRowInComponent(self.kSoundComponent)
        initialView.displayAnimal(animalNames[row], withSound: animalSounds[choosenSound], fromComponent: "the Animal")
    } else {
        let choosenAnimal: Int = pickerView.selectedRowInComponent(kAnimalComponent)
        initialView.displayAnimal(animalNames[choosenAnimal], withSound: animalSounds[row], fromComponent: "the Sound")
    }


}




override func viewDidLoad() {
    super.viewDidLoad()

    animalNames = ["Mouse","Goose","Cat","Dog","Snake","Bear","Pig"]
    animalSounds = ["Oink","Rawr","Sss","Meow","Honk","Squeak"]
    animalImages = [UIImageView(image: UIImage(named: "mouse.png")),
        UIImageView(image: UIImage(named: "goose.png")),
        UIImageView(image: UIImage(named: "cat.png")),
        UIImageView(image: UIImage(named: "dog.png")),
        UIImageView(image: UIImage(named: "snake.png")),
        UIImageView(image: UIImage(named: "bear.png")),
        UIImageView(image: UIImage(named: "pig.png"))]

    preferredContentSize = CGSizeMake(340,380)

}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let initialView: ViewController = presentedViewController as! ViewController
    initialView.displayAnimal(animalNames[0], withSound: animalSounds[0], fromComponent: "nothing yet...")


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

我找到了导致异常的原因。我使用了“presentedViewController”而不是“presentingViewController”。这是一个愚蠢的错误,但我花了一个小时才发现:)

首先,永远不要执行第一次强制转换,其次,使用调试方法,并在强制转换之前查看作为presentedViewController获得的对象。这可能会在你放弃尝试之前回答你的问题。谢谢你的回答。我确实尝试使用if-let子句将其展开,但始终触发了else条件(nil)。我刚刚检查了调用presentedViewController时得到的结果,结果为零。我在这里问这个问题是为了找出为什么它是零