ios - 在 Swift 中将对象添加到 NSMutableArray 时出错

标签 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)
        }

有人能看出我做错了什么吗?

最佳答案

我不确定您在做什么或从哪里获得 configurationTextField block 。但是下面的代码在 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)

    }
}

关于ios - 在 Swift 中将对象添加到 NSMutableArray 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24304442/

相关文章:

ios - Swift:通过查看其他 UISwitch 的状态来检查 UISwitch 的模式

ios - kAudioFileStreamError_DiscontinuityCantRecover in AudioFileStreamParseBytes

ios - 动态设置放置在 UIScrollView 中的 UITableView 的框架大小

objective-c - 如何将 NSMutableArray 绑定(bind)到 NSArrayController 的选择?

objective-c - 优化数组搜索,通过比较 Objective C 中的 2 个字符串进行搜索

swift - 如何在 SwipeCellKit 中实现左右滑动

ios - 在 iOS 的 UIDatePicker 中隐藏日期

ios - 是否可以使用 eventkit 更改 icalendar 中的默认警报时间?

ios - 如何检查 NSMutableArray 中的状态

ios - Swift:必需的初始值设定项给我一条错误消息