繁体   English   中英

无法分配给受限于类的协议中的属性-Swift编译器错误

[英]Cannot assign to property in protocol constrained to class - Swift compiler error

我使用Xcode 7 beta 6,并且我有这样的代码:

public protocol ViewForViewModel {
    typealias ViewModelType
    var viewModel: ViewModelType! { get set }
    func bindToViewModel()
}

func afterViewInstantiated <V : ViewForViewModel where V: UIViewController, V.ViewModelType: AnyObject>(view : V, viewModel: V.ViewModelType) -> V {
    //Cannot assign to property: 'view' is a 'let' constant
    view.viewModel = viewModel // error here

    VMTracker.append(viewModel, view: view)

    return view
}

编译器抱怨分配view.viewModel = viewModel 我了解哪些ViewForViewModel协议本身并不受限于类,但V类型仅限于UIViewController类。 它是错误还是功能?

UPD:它甚至抱怨UITableViewCell变量:

func registerBinding<V: BindableCellView where V: UITableViewCell>(viewType: V.Type) {
    let typeName = nameOfType(V.ViewModelType.self)

    bindings[typeName] = { [unowned self] viewModel, indexPath in
        let view = self.tableView.dequeueReusableCellWithIdentifier(V.CellIdentifier, forIndexPath: indexPath) as! V

        //Cannot assign to 'viewModel' because 'view' is a 'let' constant
        //However view is UITableViewCell that support ViewForViewModel protocol
        view.viewModel = viewModel as! V.ViewModelType

        self.onWillBind?(view, indexPath)
        view.bindToViewModel()
        self.onDidBind?(view, indexPath)

        return view
    }
}

如果编译器无法推断出该参数将始终是引用类型,则可以始终将class添加到协议声明中:

public protocol ViewForViewModel: class {
  typealias ViewModelType
  var viewModel: ViewModelType! { get set }
  func bindToViewModel()
}

这样标记了协议后,即使对象存储在常量中,您也应该能够为属性分配值。

在第一种情况下,应将其视为“未实现的功能”(编译器无法在此上下文中推断类的行为)。 因此,要解决此问题,您必须将value设为var

func afterViewInstantiated <V : ViewForViewModel where V: UIViewController, V.ViewModelType: AnyObject>(var view : V, viewModel: V.ViewModelType) -> V

在第二种情况下,您应该提供有关错误(消息)和类型的更多信息。 V来自哪里?

如果编译器抱怨协议不能用作非泛型类型,请删除协议中的类型typealias

顺便说一句,用例是什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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