swift - 如何保证 inout 参数不会改变类型并且不会在函数中变为 nil
问题描述
工作在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 参数可以设置为类型不是“产品”的值;使用声明为类型“_?”的值 反而”
此外,如果我尝试为 ProductExtended.Product 中的字段调用它,我会得到模棱两可的上下文有没有办法向编译器保证我不会更改该参数的值类型并且我不会在函数内将其设为 nil ?
解决方案
inout won't accept Generic type , because the reference wants to store value backs to its original value. here is the link for inout https://docs.swift.org/swift-book/LanguageGuide/Functions.html
推荐阅读
- database - 我有一个关于功能依赖和违反第二范式的问题
- java - 如何在运行时在@POST RESTEasy 客户端方法中设置实体字段的@PartFilename?
- vue.js - 如何检查socket.io中是否存在套接字
- laravel - 我想一次上传 3 张图片时遇到编译错误(函数名称必须是字符串)
- python - 产品聊天机器人 v2
- python - 使用二分搜索返回在排序的值列表中可以多次出现的值的第一次出现的位置
- excel - 多重匹配多重返回excel公式
- javascript - tensorflow js:未捕获的错误:检查时出错:预期 conv2d_input 有 4 个维度,但得到了形状为 [28,28,1] 的数组
- material-ui - 在 Select [Material UI] 中更改所选项目的颜色
- c# - 如何将数据从字符串生成器值传递到 Excel 工作表