У меня очень странный массив, и я не уверен, чего мне не хватает, что-то очень простое, я уверен.Вот мой код. Этот код зацикливает структуру с массивами объектов и переменных и пытается найти объекты с категорией = 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