简体   繁体   中英

Swift: unowned cannot be applied to non-class type THETYPE -> () -> THETYPE

Open a new Swift project and add this to the 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.)

This produces two errors:

On the first line:

'unowned' cannot be applied to non-class type '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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM