首先,我非常清楚Xcode 6和Swift语言都处于测试版并且容易出错; 然而,这个特别的东西似乎有点奇怪,因为到目前为止我尝试的其他东西似乎都很好.
如果这不适合StackOverflow,我很乐意删除这个问题.
我开始玩Xcode 6/Swift(准备发布),与我的想法相比,这是一次非常愉快的体验.话虽这么说,移植我喜欢做的"训练"风格的应用程序的一个问题是,我似乎无法生成UIAlertView,因为EXC_BAD_ACCESS
有问题的代码是:
override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) var alert = UIAlertView(title: "Title", message: "Message", delegate: nil, cancelButtonTitle: "OK") // EXC_BAD_ACCESS here alert.show() }
在创建UIAlertView的行上,我得到一个EXC_BAD_ACCESS
因为[UIAlertView retain]
在一个解除分配的实例上被调用.
再一次,我正在将这个问题归结为beta横幅,但是如果我做错了什么或者其他人遇到过类似的问题,我很好奇.
请尝试以下代码
let alert = UIAlertView() alert.title = "Title" alert.message = "My message" alert.addButtonWithTitle("Ok") alert.show()
但是在iOS 8中
UIAlertView
已弃用.因此,使用UIAlertController
了preferredStyle
的UIAlertControllerStyleAlert
.它应该是:
var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil)
检查上面的代码,你是否得到相同的错误?