[英]Swift: unowned cannot be applied to non-class type THETYPE -> () -> THETYPE
Open a new Swift project and add this to the ViewController.swift: 打开一个新的Swift项目,并将其添加到ViewController.swift中:
private lazy var imagePicker = {
[unowned self] in
let retval = UIImagePickerController()
let selfDelegate = self as! protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
retval.delegate = selfDelegate
retval.allowsEditing = true
return retval
}()
(We recast self
with protocol conformance because protocol conformance is defined in class extensions for readability.) (我们使用协议一致性来重铸
self
,因为协议可读性在类扩展中定义,以提高可读性。)
This produces two errors: 这将产生两个错误:
On the first line: 在第一行:
'unowned' cannot be applied to non-class type 'ViewController -> () -> ViewController'
'unown'不能应用于非类类型'ViewController->()-> ViewController'
and on the last line: 在最后一行:
Cannot invoke value of type '() -> _' with argument list ()'
无法使用参数列表()调用类型'()-> _'的值
Both of these errors make no sense to me. 这两个错误对我来说都毫无意义。 What do they mean and how can I fix them.
它们是什么意思,我该如何解决。
You have to specify the type of the variable: 您必须指定变量的类型:
private lazy var imagePicker: UIImagePickerController = {
That will fix both of your errors. 这将修复您的两个错误。
The compiler just got confused when trying to infer types. 尝试推断类型时,编译器只是感到困惑。 The closure is probably too complicated for it and all type-inferring problems end with strange error messages.
闭包可能太复杂了,所有类型推断问题都以奇怪的错误消息结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.