致命错误:索引超出范围?但是是吗?

时间:2018-11-15 05:54:01

标签: arrays swift

我有一个很奇怪的数组,我不确定我缺少什么,我确信这很简单。这是我的代码,此代码循环使用带有对象和变量的数组的结构,并尝试查找category = 1的对象。如果为true,则将对象的数组发送到另一个数组中。一切正常,直到我尝试将数组复制到排序后的数组中,并在代码中添加更多注释。我正在尝试在用户单击某个类别时对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

1 个答案:

答案 0 :(得分:2)

在没有过多介绍代码结构以及如何以更“ Swifty”方式编写代码的情况下,我看到的问题是,这不是在Swift中附加到数组的好方法。

            let currentNoSortedCourses = sortedCourses.count
            sortedCourses[currentNoSortedCourses] = courses[count]

这正在访问超出范围并导致崩溃的索引。要在Swift中附加到数组,请使用append方法。

        sortedCourses.append(courses[count])

虽然我在这里,我还是想告诉你,解决您的问题的更好方法(一个班轮)可能是这样的

    sortedCourses = courses.filter { $0.categories.contains(1) }