Swift 2.0 : infer closure type error
Asked Answered
M

1

6

I get error

Unable to infer closure type in the current context

In code which was working in Swift 1.2

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

Whole 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);
    }
}
Metcalfe answered 9/6, 2015 at 10:28 Comment(4)
Can you show the whole context? _mapVC is presumably a property of some class, and your declaration seems to work in Playground if I put it inside a simple class definition (and spoof your MapViewController)...Hoyle
Added other controller but with same error...Metcalfe
Have you tried adding a type annotation? private lazy var _messagesVC: MessagesViewController = { .. }()Tita
Yes, its help please create answer and I will close this issueMetcalfe
C
4

I found two ways to get rid of this error.

First, explicitly annotate the property with its type. I find this very strange because Swift is supposed to just infer this from the initialization.

lazy var embeddedViewController: CustomViewController = CustomViewController()

The second is just to remove the lazy keyword.

var embeddedViewController = CustomViewController()

So I guess this is an error currently plaguing lazy properties in Swift 2.0?

Carnot answered 21/6, 2015 at 18:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.