简体   繁体   English

Swift:无所有权不能应用于非类类型THETYPE->()-> THETYPE

[英]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.

相关问题 swift 协议“弱”不能应用于非类类型 - swift protocol 'weak' cannot be applied to non-class type “弱”不能应用于非类类型“ &lt;&lt;错误类型&gt;&gt;” - 'weak' cannot be applied to non-class type '<< error type>>' Alamofire:“弱”不能应用于非类类型错误 - Alamofire: 'weak' cannot be applied to non-class type error weak不能应用于非类型的uiimageview - weak cannot be applied to non-class type uiimageview 如何使非类类型符合类协议? - How to make a non-class type conform to a class protocol? 从非协议,非类类型&#39;CGPoint&#39;继承 - Inheritance from non-protocol, non-class type 'CGPoint' 从非协议,非类类型“ V”继承 - Inheritance from non-protocol, non-class type 'V' 迅速。 unowned只能应用于类和类绑定协议类型。 弱者工作得很好 - Swift. unowned may only be applied to class and class-bound protocol types. weak works fine 类型参数不能应用于非参数化类&#39;BFTask&#39; - Type arguments cannot be applied to non-paremterized class 'BFTask' 在Swift中,如何制作String的后代? 尝试执行此操作时出现错误“从非协议,非类类型&#39;String&#39;继承” - In Swift, how to make a descendant of String? I get error “Inheritance from non-protocol, non-class type 'String'” when trying to do that
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM