- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
当我定义一个UIAlertController
convenience initializer时:
extension UIAlertController {
convenience init(message: String?) {
self.init(title: nil, message: message, preferredStyle: .Alert)
self.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
}
}
并在我的 UIViewController
子类中的按钮操作中使用它:
func buttonAction(button: UIButton) {
let alert = UIAlertController(dictionary: nil, error: nil, handler: nil)
presentViewController(alert, animated: true, completion: nil)
}
然后单击模拟器上的那个按钮,我收到一条警告:
Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (UIAlertController)
但是,如果我使用全局函数而不是便利初始值设定项,我不会收到警告:
func UIAlertControllerWithDictionary(message: String?) -> UIAlertController {
let alert = UIAlertController(title: nil, message: message, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
return alert
}
我已将此作为 iOS SDK 错误报告给 Apple。
在修复之前,是否可以忽略警告并使用便利初始化程序?
最佳答案
我注意到便利初始化器也存在同样的问题。它实际上同时存在两个错误。
UIAlertController
实例。+(id) alertControllerWithTitle:message:preferredStyle:
。UIAlertController
实例。 (错误 #1)UIAlertController
的 deinit
(dealloc) 访问导致日志消息的 view
属性。 (错误#2)self
悄悄地从 Swift 的 UIAlertController
实例更改为 UIKit 的实例。所以第一个错误是 Swift 创建了一个临时的 UIAlertController
,它在没有被使用的情况下就被销毁了。
第二个错误是 UIViewController
在取消初始化期间访问了 view
属性,这是不应该的。
关于您的问题:
这两个错误应该都不是问题,所以我们现在可以简单地忽略警告。我也这么做了,目前还没有任何问题 - 只是日志中的那个警告。
关于ios - 扩展 UIAlertController 便利初始化警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33805197/
我的图书馆的几个部分都带有“便利”功能。例如,容器类可能具有解析字符串信息的功能。这些功能不一定一直都需要(或想要),因此我想将它们放在单独的文件中,以便可以根据用户的需要将它们包括或省略。 应该如何
我已经尝试在 Stack Overflow 和其他网站上搜索使用 Sets 与 Lists 的优缺点和便利,但我真的找不到明确的答案何时使用这个或那个。 从 Hibernate 的文档中,他们声明非重
我是一名优秀的程序员,十分优秀!