gpt4 book ai didi

swift - 使用增量运算符会产生构建错误 "swift Unary operator '++' cannot be applied to an operand of type ' Int'"

转载 作者:IT王子 更新时间:2023-10-29 05:34:49 26 4
gpt4 key购买 nike

Basic Operators 部分,Swift 编程语言指南指出++ 是有效的运算符:

“More complex examples include the logical AND operator && (as in if enteredDoorCode && passedRetinaScan) and the increment operator ++i, which is a shortcut to increase the value of i by 1.”

摘自:Apple Inc.“The Swift Programming Language”。电子书。 https://itun.es/gb/jEUH0.l

但是,在 Playground 上尝试这样做时;

import UIKit

let i = 0
i++

构建错误显示:

swift 一元运算符“++”不能应用于“Int”类型的操作数

为什么?

最佳答案

是的,不是措辞最好的编译器错误。

问题是您使用let 声明了i。由于整数是值类型,这意味着 i 是不可变的——一旦赋值就无法更改。

如果您将 i 声明为 var i = 0,代码将编译。

关于swift - 使用增量运算符会产生构建错误 "swift Unary operator '++' cannot be applied to an operand of type ' Int'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29280910/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com