從vadian的評論在這裏非常重要。你不應該有這樣的多個數組。創建一個包含數據的結構:
struct Score {
let isComplete: Bool
let finalScore: Int
}
然後,您可以添加Date或任何其他您當前具有並行陣列的字段。然後你的數據是這樣的:
let scores = [
Score(isComplete: true, finalScore: 12),
Score(isComplete: true, finalScore: 12),
Score(isComplete: true, finalScore: 12),
Score(isComplete: true, finalScore: 12),
Score(isComplete: false, finalScore: 3),
Score(isComplete: true, finalScore: 13),
Score(isComplete: true, finalScore: 13),
Score(isComplete: false, finalScore: 2),
Score(isComplete: false, finalScore: 2),
]
並獲得完整的人是通過過濾
let completeScores = scores.filter { $0.isComplete }
當然,如果你想只是最後得分爲數組簡單,你可以向下映射到:
let finalCompleteScores = completeScores.map { $0.finalScore }
這就是你應該如何考慮你的數據,而不是你必須保持同步的一堆數組。
考慮使用** **中之一的模型,而不是多個陣列的自定義結構的陣列。 – vadian
OOO聽起來超酷,我該怎麼做? – SashaZ
[Swift語言指南:類和結構](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html) – vadian