简体   繁体   English

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

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

I use Xcode 7 beta 6 and I have code like this: 我使用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
}

Compiler complains on assignment view.viewModel = viewModel . 编译器抱怨分配view.viewModel = viewModel I'm understand what ViewForViewModel protocol is not constrained to class by itself, but V type is constrained to UIViewController class. 我了解哪些ViewForViewModel协议本身并不受限于类,但V类型仅限于UIViewController类。 Is it a bug or a feature? 它是错误还是功能?

UPD: it even complains about UITableViewCell variable: 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
    }
}

If the Compiler can't infer, that the parameter will always be a reference type, you can always add class to the protocol declaration: 如果编译器无法推断出该参数将始终是引用类型,则可以始终将class添加到协议声明中:

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

Once the protocol is marked up like that, you should be able to assign values to the property even if the object is stored in a constant. 这样标记了协议后,即使对象存储在常量中,您也应该能够为属性分配值。

In the first case it should be considered an "unimplemented feature" (that the compiler cannot infer the behavior of a class in this context). 在第一种情况下,应将其视为“未实现的功能”(编译器无法在此上下文中推断类的行为)。 So to fix it you have to make the value a var : 因此,要解决此问题,您必须将value设为var

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

In the second case you should provide more information about the error (message) and the types. 在第二种情况下,您应该提供有关错误(消息)和类型的更多信息。 Where does V come from? V来自哪里?

If the compiler is complaining about the protocol not being able to be used as a non-generic type, delete the typealias in the protocol. 如果编译器抱怨协议不能用作非泛型类型,请删除协议中的类型typealias

BTW, what is the use case for this? 顺便说一句,用例是什么?

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

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