ios - 为什么 swift 中的 var++ 是错误的,而改成 var+1 是正确的

标签 ios swift macos

enter image description here

初学swift,为什么第一个错了? 谢谢~~ˊ_>ˋ

最佳答案

应该是currentAudioIndex++(没有空格)。它等同于:currentAudioIndex = (currentAudioIndex)%2

currentAudioIndex = (currentAudioIndex++)%2 
// plus 1 to currentAudioIndex will be overrided by `currentAudioIndex =`.
// With (currentAudioIndex++)%2. E.g currentAudioIndex = 1
// 1. currentAudioIndex return 1 for the operator %. It's "1%2"
// 2. currentAudioIndex plus 1. currentAudioIndex == 2 now.
// 3. The operator % (1%2) return 1 for currentAudioIndex.
// 4. currentAudioIndex == 1  at the end.

但在你的情况下,我认为你想要这个 ++currentAudioIndex。它与

相同
currentAudioIndex = currentAudioIndex + 1
currentAudioIndex = (currentAudioIndex)%2

关于ios - 为什么 swift 中的 var++ 是错误的,而改成 var+1 是正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33949366/

相关文章:

swift - 如何向扩展添加另一个级别(像扩展一样分类)

swift - 如何在 Swift 中使用 map 转换字典?

ios - 需要解释一下 Swift 代码中的 JSON 解析

objective-c - 无法在 OSX 上从 64 位进程加载 32 位 dylib,反之亦然?

cocoa - 如何使用 Cocoa 将图像重叠(叠加)在文件图标上?

ios - 无法使用 Xcode 4.2 存档临时分发

javascript - AppGyver - PhoneGap 删除加载过渡屏幕

iphone - 在导航栏中添加多个按钮

ios - NSOPeration 取消错误

android - 无法在 MacOS 上运行 React Native 的 Android 项目