Swift 2.0: print closure type error

I get an error

Unable to infer closure type in current context

In code that worked in Swift 1.2

private lazy var _messagesVC = { return MessagesViewController(nibName:"MessagesViewController",bundle:nil)}() 

Full view controller where i get this error

 import UIKit class FriendsViewController: UIViewController { @IBOutlet weak var containerView: UIView! @IBOutlet weak var segmentContainerView: UIView! private lazy var _connectionVC = { return FriendsConnectionViewController(nibName:"FriendsConnectionViewController",bundle:nil)}() private lazy var _messagesVC = { return MessagesViewController(nibName:"MessagesViewController",bundle:nil)}() override func viewDidLoad() { super.viewDidLoad() self.selectedControllerFrom(index: 0) // Do any additional setup after loading the view. } override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) } func selectedControllerFrom(index index:UInt) { var vc:UIViewController? switch index{ case 0: vc = _connectionVC case 1: vc = _messagesVC default : vc = nil } if vc != nil{ self.showViewController(vc!,containerView: containerView); } } 
+6
source share
1 answer

I found two ways to get rid of this error.

First, I explicitly annotate the property with its type. I find this very strange because Swift should just get it out of initialization.

 lazy var embeddedViewController: CustomViewController = CustomViewController() 

Secondly, just delete the lazy keyword.

 var embeddedViewController = CustomViewController() 

So, I think this is a bug that is currently causing lazy properties in Swift 2.0?

+4
source

Source: https://habr.com/ru/post/988777/


All Articles