如果没有找到结果,我的过滤器功能会崩溃

时间:2016-05-27 06:32:52

标签: ios swift filter

以下是代码:

private func filter(searchText: String) -> [MICountry] {
    filteredList.removeAll()

    sections.forEach { (section) -> () in
        section.countries.forEach({ (country) -> () in
            let result = country.name.compare(searchText, options: [.CaseInsensitiveSearch, .DiacriticInsensitiveSearch], range: searchText.startIndex ..< searchText.endIndex)
            if (result == .OrderedSame) {
                filteredList.append(country)
            }

        })
    }

    return filteredList
}

我的国家/地区类别如下:

class MICountry: NSObject {
    let name: String
    var section: Int?

    init(name: String) {
        self.name = name
    }
}

struct MISection {
    var countries: [MICountry] = []

    mutating func addCountry(country: MICountry) {
        countries.append(country)
    }
}

0 个答案:

没有答案