gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:52:44 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序中构建一个功能,用户可以通过该功能向他们的产品添加自定义规范。规范应该添加到 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com