[英]Fatal error: Index Out of range? BUT IS IT?
我有一個非常奇怪的數組,我不確定我錯過了什么,我確定一些非常簡單的東西。 這是我的代碼,此代碼循環一個包含對象和變量數組的結構,並嘗試找到類別 = 1 的對象,當為真時,它將對象數組發送到另一個數組中。 一切正常,直到我嘗試將數組復制到已排序的數組中,代碼中有更多注釋。 當用戶單擊某個類別時,我試圖對 tableView 進行排序,4 個可能的類別是 1,30,31,32(不要問為什么)
var courses = [Course]()
var sortedCourses = [Course]()
@objc func catOthersButtonObj(_ sender: UIButton){ //<- needs `@objc`
var count = 0
let courseCount = courses.count
let otherCat = 1
// set count to loop the number of detected arrays
// this category looks for the number 1
print("courseCount = " + String(courseCount)) // to debug if courseCount is accurate, it is
//let noOfCat = courses.categories.count
while (count < courseCount)
{
print("count = " + String(count)) // to debug if count is increasing every loop, it is
let totalNoCat = (courses[count].categories?.count)!
var catCount = 0
// while entering a loop of arrays of objects, it searches for the array "categories" and sets totalNoCat = number of categories, this is neccesary because one object can have many categories
// catCount is used to loop through the number of detect categories
if (totalNoCat == 0)
{
break
}
// If no categories is detected, next array
while (catCount < totalNoCat)
{
print("totalNoCat = " + String(totalNoCat))
print("catCount = " + String(catCount))
// debug, check if totalNoCat is detected
// debug, catCount if number of categories is detected and added
if (courses[count].categories?[catCount] == otherCat)
{
print("category Detected = " + String((courses[count].categories?[catCount])!))
// see if category is 1, when it is 1, this is triggered, so its working fine
let currentNoSortedCourses = sortedCourses.count
print("currentNoSortedCourses = " + String(currentNoSortedCourses))
// check the number of sorted array, this is to add array into the next array
// if there is 0 number of sorted arrays, then sorted[0] should be course[detectarray category = 0 array]
//print(courses[count]) checks if courses[count] has data, it does
sortedCourses[currentNoSortedCourses] = courses[count] //where the error is
//here it is sortedCourses[0] = courses[7] ( 7 is which the array detected the correct category
break
}
catCount = catCount + 1
print("Category Detected = " + String((courses[count].categories?[catCount - 1])!))
//debug, if category not 1, show what it is
}
count = count + 1
}
}
Print results
courseCount = 50
count = 0
totalNoCat = 2
catCount = 0
Category Detected = 31
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 1
totalNoCat = 2
catCount = 0
Category Detected = 31
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 2
totalNoCat = 1
catCount = 0
Category Detected = 30
count = 3
totalNoCat = 2
catCount = 0
Category Detected = 32
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 4
totalNoCat = 2
catCount = 0
Category Detected = 32
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 5
totalNoCat = 1
catCount = 0
Category Detected = 32
count = 6
totalNoCat = 2
catCount = 0
Category Detected = 32
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 7
totalNoCat = 1
catCount = 0
category Detected = 1
currentNoSortedCourses = 0
無需深入研究代碼的結構以及如何以更“Swifty”的方式編寫代碼,我可以看到問題在於這不是在 Swift 中附加到數組的好方法。
let currentNoSortedCourses = sortedCourses.count
sortedCourses[currentNoSortedCourses] = courses[count]
這是訪問越界並導致崩潰的索引。 要在 Swift 中附加到數組,請使用append
方法。
sortedCourses.append(courses[count])
雖然我在這里,但我不妨告訴您,解決您的問題的更好方法(一個班輪)可能是這樣的
sortedCourses = courses.filter { $0.categories.contains(1) }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.