Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何保证inout参数获胜';不改变类型和赢得';t在函数中变为零_Swift_Reference_Optional_Inout - Fatal编程技术网

Swift 如何保证inout参数获胜';不改变类型和赢得';t在函数中变为零

Swift 如何保证inout参数获胜';不改变类型和赢得';t在函数中变为零,swift,reference,optional,inout,Swift,Reference,Optional,Inout,在swift 4中工作。我有一个这样的函数 func setFields<T>(_ fromView : UIView, toObject : inout T!) -> T! setFields(self.view, toObject: &self.productExtended.product) //inside ProductExtended public var product: Product func设置字段(uFromView:UIView,toO

swift 4
中工作。我有一个这样的函数

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

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

//inside ProductExtended
public var product: Product
func设置字段(uFromView:UIView,toObject:inout T!)->T!
设置字段(self.view、toObject:&self.productExtended.product)
//内部产品扩展
公共var产品:产品
当我这样称呼它时,我得到一个错误:

“Inout参数可以设置为一个类型不是'Product'的值;请改用声明为类型'\u1'的值”

此外,如果我试图为ProductExtended.Product内的字段调用它,则会得到不明确的上下文
有没有一种方法可以向编译器保证我不会更改该参数的值类型,也不会在函数中将其设为零?

inout不会接受泛型类型,因为引用希望将值存储回其原始值。这是inout的链接

你的第二行是不是应该是
设置字段
而不是
获取字段
像这样回答问题是没有帮助的。请基本上从不将函数参数声明为隐式展开可选(
)。如果函数中的
toObject
不能变为
nil
,则将其声明为非可选(删除感叹号)。为什么函数有一个
inout
参数和一个相同类型的返回值?这是遗留代码,函数的作用是修改传入类并返回对它的引用。它使用隐式unwrapped optional,因为里面有很多反射,如果我不使用它,我得到的类型是optional而不是t。