swift - 为什么我可以在使用 try 时使用 inout 参数改变 let 常量?
问题描述
这将无法编译并出现以下错误:
不可变值“self.constantValue”不得传入输出
class Test {
let constantValue: String = ""
init() {
Test.makeABC(&constantValue)
}
static func makeABC(_ string: inout String) {
string = "ABC"
}
}
然而,这将编译并实际改变 let 常量。
class Test {
let constantValue: String = ""
init() {
try? Test.makeABC(&constantValue)
}
static func makeABC(_ string: inout String) throws {
string = "ABC"
}
}
有谁知道为什么会这样以及它是否是预期的行为?
解决方案
推荐阅读
- mysql - 删除此条件为真的行的存储过程:
- python - Python:如何使用正则表达式对字典值进行排序
- rundeck - 如何将父作业的选项值作为子作业的“作业名称”传递?
- python - 如何使用抛出异常方法终止线程 - Python
- oracle - Oracle XMLDB 检索无效 XML
- ruby-on-rails - Rails:查找所有具有特定属性的孩子的所有父母
- c++ - 双精度变量无法正常工作
- node.js - 如何在任何 PUG 模板中使用 res.render ,而不仅仅是渲染的那个?
- python - 使用 Pandas [with key column] 将 CSV 与不同的列组合
- python - 打印输出正确,但分析显示错误