I have had this problem since updating to Swift 3
. I have tried updating this to Swift 3
by using stride(from:to:by:)
but I can't implement the old Swift 2
code to the Swift 3
code. Below I have included my code:
我更新到Swift 3后遇到了这个问题。我尝试使用stride(from:to:by :)将其更新为Swift 3但我无法将旧的Swift 2代码实现为Swift 3代码。下面我已经包含了我的代码:
for var i = 0; i <= self.senderArray.count - 1; i += 1 {
if self.senderArray[i] == userName {
self.other2Array.append(self.otherArray[i])
}
else {
self.other2Array.append(self.senderArray[i])
}
self.message2Array.append(self.messageArray[i])
self.sender2Array.append(self.senderArray[i])
}
The error I am getting:
我得到的错误:
C-style for statement has been removed in Swift 3
在Swift 3中删除了C-style for语句
I have tried this method below, but it doesn't work:
我在下面尝试过这种方法,但它不起作用:
for i in 0 ..< self.senderArray.count
Before anyone marks this question as a duplicate I have had a look at the following questions but for some reason, I can't figure out how to implement my code into the updated Swift 3
code.
在任何人将此问题标记为重复之前,我已经看过以下问题但由于某种原因,我无法弄清楚如何将我的代码实现到更新的Swift 3代码中。
#warning: C-style for statement is deprecated and will be removed in a future version of Swift
#warning:不推荐使用C-style for语句,将在未来的Swift版本中删除
How to fix C-style for statement?
如何修复C风格的语句?
Fix warning "C-style for Statement is deprecated" in Swift 3
在Swift 3中修复警告“不推荐使用C语言的语句”
https://*.com/questions/37814867/fix-c-style-for-statement-is-deprecated-in-swift
1 个解决方案
#1
2
Keeping the index is easy with enumerated
:
使用枚举保持索引很容易:
for (i, sender) in senderArray.enumerated() {
// Can simply use 'sender' here, no need to index into senderArray.
if sender == userName {
...
// Unchanged lines; 'i' is used as index here.
self.message2Array.append(self.messageArray[i])
self.sender2Array.append(self.senderArray[i])
}
#1
2
Keeping the index is easy with enumerated
:
使用枚举保持索引很容易:
for (i, sender) in senderArray.enumerated() {
// Can simply use 'sender' here, no need to index into senderArray.
if sender == userName {
...
// Unchanged lines; 'i' is used as index here.
self.message2Array.append(self.messageArray[i])
self.sender2Array.append(self.senderArray[i])
}