在Swift中向NSMutableArray添加对象时出错

时间:2014-06-19 10:31:39

标签: ios nsmutablearray swift exc-bad-access

我正在尝试在我的应用中构建一个功能,用户可以通过该功能向其产品添加自定义规范。应该在NSMutableArray中添加规范,但是当我想向它添加一个对象时,我得到一个EXC_BAD_ACCESS错误。这是我的代码:

var specs = NSMutableArray()

func addSpec () {
            var alert = UIAlertController(title: "Nieuwe Specificatie", message: "Vul hier een naam voor de nieuwe specificatie in:", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addTextFieldWithConfigurationHandler(configurationTextField)
            alert.addAction(UIAlertAction(title: "Opslaan", style:UIAlertActionStyle.Default, handler: {(UIAlertAction) in
                self.specs.addObject(self.newSpecificationTitle.text)
                }))
            alert.addAction(UIAlertAction(title: "Annuleren", style:UIAlertActionStyle.Default, handler: {(UIAlertAction) in
                println("annuleren")
                }))
            self.presentViewController(alert, animated: true, completion:nil)
        }

有人能看出我做错了吗?

1 个答案:

答案 0 :(得分:1)

我不确定您在获取configurationTextField块时所执行的操作。但以下代码适用于Xcode 6 DP2

class ViewController: UIViewController {

    var newSpecificationTitleTextField: UITextField?
    var specs = NSMutableArray()

    @IBAction func addSpec(sender: AnyObject) {
        var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
        alert.addTextFieldWithConfigurationHandler { textField in
            self.newSpecificationTitleTextField = textField
        }
        alert.addAction(UIAlertAction(title: "OK", style:.Default, handler: {(UIAlertAction) in
            self.specs.addObject(self.newSpecificationTitleTextField!.text)
            println("\(self.specs)")
            }))
        alert.addAction(UIAlertAction(title: "Cancel", style:.Cancel, handler: {(UIAlertAction) in
            println("Cancel")
            }))
        self.presentViewController(alert, animated: true, completion:nil)

    }
}