简体   繁体   English

无法转换泛型协议的函数类型实现的返回表达式

[英]Cannot convert return expression of generic protocol's function type implementation

I'd like to use swift generics in a way described below: 我想以下面描述的方式使用swift泛型:

class ResponseContainer {

}

protocol DisplayManageable {

    func getModel<ModelType: ResponseContainer>() -> ModelType?
}

class DisplayBaseManager<ObtainedModelType: ResponseContainer>: NSObject, DisplayManageable {

    var modelObtained: ObtainedModelType? = nil

    func getModel<ObtainedModelType>() -> ObtainedModelType? {

        return modelObtained
    }
}

But I have a problem with this code, more precisely there is a problem in this line: 但是这个代码有问题,更确切地说这行有一个问题:

return modelObtained

And I'm getting the error: 我收到了错误:

Cannot convert return expression of type 'ObtainedModelType?' 无法转换'ObtainedModelType?'类型的返回表达式 to return type 'ObtainedModelType?' 返回类型'ObtainedModelType?'

And now my simple question, why can't I do that? 现在我的简单问题,为什么我不能这样做? What's wrong with that? 这有什么问题?

Generics in protocol's function and in class definitions are the same. 协议函数和类定义中的泛型是相同的。 Everything looks fine in my opinion and logically is ok, so why cannot I do so? 在我看来,一切看起来都很好,从逻辑上来说还可以,所以为什么我不这样做呢?

In

func getModel<ObtainedModelType>() -> ObtainedModelType? { ... }

ObtainedModelType introduces a local generic placeholder, and that hides the ObtainedModelType from the class definition ObtainedModelType引入了一个本地通用占位符,并从类定义中隐藏ObtainedModelType

class DisplayBaseManager<ObtainedModelType: ResponseContainer>

This causes the strange looking error message 这会导致奇怪的错误消息

Cannot convert return expression of type 'ObtainedModelType?' 无法转换'ObtainedModelType?'类型的返回表达式 to return type 'ObtainedModelType?' 返回类型'ObtainedModelType?'

because return modelObtained has the generic type ObtainedModelType? 因为return modelObtained具有泛型类型return modelObtained ObtainedModelType? from the class definition, but the expected return type is ObtainedModelType? 从类定义,但预期的返回类型是ObtainedModelType? from the method definition. 从方法定义。

What you probably want is a protocol with an associated type 您可能需要的是具有关联类型的协议

protocol DisplayManageable {
    associatedtype ModelType: ResponseContainer
    func getModel() -> ModelType?
}

and a class adopting this protocol with ModelType == ObtainedModelType : 和一个采用ModelType == ObtainedModelType协议的类:

class DisplayBaseManager<ObtainedModelType: ResponseContainer>: NSObject, DisplayManageable {

    var modelObtained: ObtainedModelType? = nil
    func getModel() -> ObtainedModelType? {
        return modelObtained
    }
}

Łukasz, I don't now why it doesn't compile but I found the way to compile it. Łukasz,我现在不知道它为什么不编译,但我找到了编译它的方法。 Just change the return statement: 只需更改return语句:

return modelObtained as? ObtainedModelType

But I'm still waiting for someone to explain the reason for error in the original code. 但我仍在等待某人解释原始代码中出错的原因。

暂无
暂无

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

相关问题 泛型函数 - &gt;无法将返回表达式转换为返回类型 - Generic function -> Cannot convert return expression to return type 无法转换返回表达式的类型 - Cannot convert return expression of type 无法将类型“[()]”的返回表达式转换为返回类型 - Cannot convert return expression of type '[()]' to return type 协议类型“名称”的值不能符合具有通用 function 的协议“名称” - Value of Protocol type 'name' cannot conform to protocol 'name' with generic function 无法将“CustomProfileCell”类型的返回表达式转换为“UITableViewCell”类型 - Cannot convert return expression of type 'CustomProfileCell' to return type 'UITableViewCell' 无法转换类型为“P”的返回表达式? 返回类型“UIViewController” - Cannot convert return expression of type 'P?' to return type 'UIViewController' Xcode“无法将类型为&#39;String&#39;的返回表达式转换为类型为&#39;Float&#39;的返回值” - Xcode “Cannot convert return expression of type 'String' to return type 'Float'” 无法将类型“[String: Any]”的返回表达式转换为返回类型“Song?” - Cannot convert return expression of type '[String : Any]' to return type 'Song?' Swift闭包:无法将类型&#39;()&#39;的返回表达式转换为类型&#39;LiveSearchResponse?” - Swift Closures: Cannot convert return expression of type '()' to return type 'LiveSearchResponse?' 无法将“NSManagedObject”类型的返回表达式转换为“字符串”类型? - Cannot convert return expression of type 'NSManagedObject' to return type 'String?'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM