如何保证 inout 参数不会改变类型并且不会在函数中变为 nil

How to guarantee that inout argument won't change Type and won't become nil in a function

swift 4 工作。我有这样的功能

func setFields<T>(_ fromView : UIView,  toObject : inout T!) -> T! 

setFields(self.view, toObject: &self.productExtended.product)

//inside ProductExtended
public var product: Product

当我这样调用它时,出现错误:

"Inout argument could be set to a value with a type other than 'Product'; use a value declared as type '_?' instead"

此外,如果我尝试为 ProductExtended.Product 中的字段调用它,我会得到不明确的上下文 有没有办法向编译器保证我不会更改该参数的值类型并且我不会在函数内部将其设为 nil?

inout 不会接受 Generic 类型,因为引用想要将值存储回其原始值。这是 inout https://docs.swift.org/swift-book/LanguageGuide/Functions.html

的 link