Swift 2.0:推断闭包类型错误

时间:2015-06-09 10:28:08

标签: swift types init lazy-initialization

我收到错误

  

无法在当前上下文中推断闭包类型

在Swift 1.2中运行的代码

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

整个视图控制器,我收到此错误

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);
    }
}

1 个答案:

答案 0 :(得分:4)

我找到了摆脱这个错误的两种方法。

首先,使用其类型显式注释属性。我觉得这很奇怪,因为Swift应该只是从初始化中推断出来。

lazy var embeddedViewController: CustomViewController = CustomViewController()

第二种方法是删除惰性关键字。

var embeddedViewController = CustomViewController()

所以我猜这是一个错误,目前正在困扰Swift 2.0中的懒惰属性?