自更新到Swift 3
以来,我遇到了这个问题。我尝试使用Swift 3
将此更新为stride(from:to:by:)
,但我无法将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])
}
我得到的错误:
在Swift 3中删除了C-style for语句
我在下面尝试过这种方法,但它不起作用:
for i in 0 ..< self.senderArray.count
在任何人将此问题标记为重复之前,我已经查看了以下问题,但由于某些原因,我无法弄清楚如何将我的代码实现到更新的Swift 3
代码中。
#warning: C-style for statement is deprecated and will be removed in a future version of Swift
How to fix C-style for statement?
Fix warning "C-style for Statement is deprecated" in Swift 3
https://stackoverflow.com/questions/37814867/fix-c-style-for-statement-is-deprecated-in-swift
答案 0 :(得分:2)
使用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])
}