Swift/Xcode - AKPickerView/UIPickerView - 致命错误:索引超出范围

Posted

技术标签:

【中文标题】Swift/Xcode - AKPickerView/UIPickerView - 致命错误:索引超出范围【英文标题】:Swift/Xcode - AKPickerView/UIPickerView - fatal error: Index out of range 【发布时间】:2017-07-01 20:17:19 【问题描述】:

我遇到了问题。我正在使用 AKPickerView,它只是一个 cocoapod,其工作方式与 UIPickerView 完全相同。

我得到的错误是:

fatal error: Index out of range

在这一行: let titleForModel = carModels[item]

这就是我所拥有的并且我正在努力实现的目标:

我有两个pickerView,一个用于汽车品牌/品牌,一个用于汽车型号。

他们都有自己的委托和数据源。

在选择汽车品牌/品牌之前,carmodelPickerView 才会被填充并呈现给用户。

这是我的完整代码:

    @IBOutlet weak var manufacterPickView: AKPickerView!
    @IBOutlet weak var modelPickerView: AKPickerView!

    var carManufacturers: [String] = ["Select make...", "Abarth","Alfa Romeo","Aston Martin", "Audi", "Bentley","BMW","Bugatti", "Cadillac","Chevrolet","Chrysler","Citroen","Dodge", "Ferrari", "Fiat","Ford","Honda","Hyundai","Jaguar","Jeep","KIA","Lamborghini","Land Rover","Lexus","Lotus","Maserati", "Maybach","Mazda","Mercedes-Benz","Mini", "Mitsubishi","Nissan","Peugeot", "Porsche", "Renault", "Rolls Royce", "SEAT", "Skoda", "Smart","Subaru", "Suzuki", "Toyota", "Vauxhall", "Volkswagen", "Volvo"]

    var carModels = [" "]
    var carModelsBackup = [" "]
    var abarthModelsArray = ["500", "500C", "Grande Punto", "Punto Evo"]

    var alfaRomeoModelsArray = ["147", "156", "159","4C", "8C", "Brera", "Giulietta", "GT", "Mito"]


    override func viewDidLoad() 

        super.viewDidLoad()

        self.manufacterPickView.dataSource = self
        self.manufacterPickView.delegate = self

        self.modelPickerView.dataSource = self
        self.modelPickerView.delegate = self

        self.manufacterPickView.font = UIFont(name: "HelveticaNeue-Light", size: 20)!
        self.manufacterPickView.highlightedFont = UIFont(name: "HelveticaNeue", size: 20)!
        self.manufacterPickView.pickerViewStyle = .wheel
        self.manufacterPickView.maskDisabled = false
        self.manufacterPickView.reloadData()


        self.modelPickerView.font = UIFont(name: "HelveticaNeue-Light", size: 20)!
        self.modelPickerView.highlightedFont = UIFont(name: "HelveticaNeue", size: 20)!
        self.modelPickerView.pickerViewStyle = .wheel
        self.modelPickerView.maskDisabled = false
        self.modelPickerView.reloadData()
   


  func numberOfItemsInPickerView(_ pickerView: AKPickerView) -> Int 

        var carBrand : Int = carManufacturers.count


        if pickerView == manufacterPickView 

        return self.carManufacturers.count
        

        if pickerView == modelPickerView 

            return self.carModels.count
        

        return carBrand
    





    func pickerView(_ pickerView: AKPickerView, titleForItem item: Int) -> String 

        if pickerView == manufacterPickView 

            var titleForItem = carManufacturers[item]

                return titleForItem
        

            if pickerView == modelPickerView 
                let titleForModel = carModels[item] //This is the line which appears in red and breaks at
                return titleForModel
            



         return ""
    


    func pickerView(_ pickerView: AKPickerView, didSelectItem item: Int) 

        if pickerView == manufacterPickView 

        if carManufacturers[item] == "Abarth" 
            carModels = abarthModelsArray
            modelPickerView.reloadData()
            


        else if carManufacturers[item] == "Alfa Romeo" 
            carModels = alfaRomeoModelsArray
            modelPickerView.reloadData()
        
        else 

            carModels = carModelsBackup
            modelPickerView.reloadData()
        
    
        modelPickerView.reloadData()
    

当我选择一个模型然后切换manufacterPickView 时应用程序崩溃。

如果这是一个简单的问题,我非常抱歉。我从来没有使用过 UIPickerView,这就是我为什么挣扎的原因。有人可以帮我吗?

谢谢。

【问题讨论】:

【参考方案1】:

看看这是否有帮助,如果它没有再次告诉我问题,我会编辑这个!

        var isManufacterSelected: Bool = false

    func numberOfItemsInPickerView(_ pickerView: AKPickerView) -> Int 

            if pickerView == manufacterPickView 

            return self.carManufacturers.count
            

            if pickerView == modelPickerView && isManufacterSelected && !carModels.isEmpty 

                return self.carModels.count
            
             else 
               return 0
            
        





        func pickerView(_ pickerView: AKPickerView, titleForItem item: Int) -> String 

            if pickerView == manufacterPickView         
                    return carManufacturers[item]
            

                if pickerView == modelPickerView &&isManufacterSelected 
                    return carModels[item]
                
        
        func pickerView(_ pickerView: AKPickerView, didSelectItem item: Int) 

            if pickerView == manufacterPickView 

            if carManufacturers[item] == "Abarth" 
                carModels.removeAll()
                carModels = abarthModelsArray
                isManufacterSelected = true
                modelPickerView.reloadData()
                


            else if carManufacturers[item] == "Alfa Romeo" 
                carModels.removeAll()
                carModels = alfaRomeoModelsArray
                isManufacterSelected = true
                modelPickerView.reloadData()
            

            else if carManufacturers[item] == "Select make..." 
                isManufacterSelected = false
                carModels.removeAll()
                modelPickerView.reloadData()
            
            else 
                carModels.removeAll()
                modelPickerView.reloadData()
            
        
  

【讨论】:

感谢您的代码。但是,它仍然会崩溃 `return carModels[item]` 这是这一行。 基本上,由于我的abarthArray 中有四个元素,如果我在carManufacter 选择器中选择Alfa Romeo 并到达alfaRomeoModelsArray 中的第四个元素以上,这会导致崩溃。 我需要 carModels 基本上清除,并匹配我选择的 selectedArray 的大小。 让我再看一遍,数组大小不是这里的问题,数组大小可以根据你传递的数据进行扩展或压缩 添加了对数组是否为空的进一步检查,编辑了答案【参考方案2】:

修复它。我注意到一件事。 modelPickerView 不会重置。

所以我把它作为优先事项。

这行代码帮助了我:

modelPickerView.selectItem(0, animated: true)

我的代码现在可以运行并且不会崩溃。

【讨论】:

以上是关于Swift/Xcode - AKPickerView/UIPickerView - 致命错误:索引超出范围的主要内容,如果未能解决你的问题,请参考以下文章

Swift 3、Xcode 8 中的 UINavigationController

使用 Firebase 数据 Swift、Xcode 7 填充 UITableViewController

Swift(XCode)- sigabrt 错误 [重复]

Alamofire 在 Swift 3、Xcode 8 中出现错误

Swift 3(Xcode 8 beta 6)localizedStringWithFormat

Swift - Xcode 8.3 自动完成未显示