2016-08-12 34 views
1

我在我的應用程序跨錯誤絆倒:iOS版 - 索引超出範圍

fatal error: Index out of range (lldb)

我想我可能有一個想法,是什麼問題,但是,沒有一個線索如何修改錯誤。

我相信由於我使用節標題,這是導致問題。我已經證明閱讀代碼以及嘗試修復它並在線搜索。下面我發佈了我的代碼示例(不想包含它,因爲它包含幾百行代碼)。

從本質上講,我使用TableViewController與SWReveal組合使用,用戶選擇一個選項並顯示文本。

class BackTableVC: UITableViewController { 


    struct Brands { 
     var sectionName : String! 
     var sectionBrands : [String]! 
    } 


    struct ThirdView { 
     var ThirdViewArray = [String]() 
    } 

    var brandArray = [Brands]() 



    var ThirdArray = [ThirdView]() 

    var brandAnswerArray = [String]() 

    override func viewDidLoad() { 

     brandArray = [ 


      Brands(sectionName: "Bugatti", sectionBrands: ["EB 110","Veyron"])] 

     ThirdArray = [ThirdView(ThirdViewArray: ["EB 110","Veyron"])] 



} 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return brandArray[section].sectionBrands.count 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell! 

     cell.textLabel?.text = brandArray[indexPath.section].sectionBrands[indexPath.row] 

     return cell 

    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

     let DestVC = segue.destinationViewController as! CarDetailsVC 

     let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow! 

     let ThirdAnswerArray : ThirdView 

     ThirdAnswerArray = ThirdArray[indexPath.row] 

     DestVC.brandAnswerArray = ThirdAnswerArray.ThirdViewArray 

     DestVC.FirstString = brandAnswerArray[indexPath.row] 

    } 


    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return brandArray.count 
    } 

    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return brandArray[section].sectionName 

    } 


} 
    import Foundation 

struct ThirdView { 
    var ThirdViewArray = [String]() 
} 

    class CarDetailsVC: UIViewController { 


     var FirstString = String() 

    var brandAnswerArray = [String]() 






    @IBOutlet var Label: UILabel! 

    override func viewDidLoad() { 

     Label.text = FirstString 

    } 

     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
      _ = segue.destinationViewController as! CarDetailsVC    
     } 


    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
} 


} 

My ThirdView結構和CarDetailsVC在單獨的.swift文件中。

這是給我的悲傷該生產線是:

 DestVC.FirstString = brandAnswerArray[indexPath.row] 

附:如果我是這樣做:

DestVC.FirstString = "Hello World" 

的Hello World是隻選擇第一個選項時顯示,那麼代碼/應用打破了我得到同樣的錯誤「索引超出範圍」就行了:

 ThirdAnswerArray = ThirdArray[indexPath.row] 

回答

3

這個簡單的答案是,你的brandAnswerArray沒有足夠的值給你索引indexPath.row的東西。即如果你有一個包含5個值的數組並且你問它數組[8],那麼應用程序將會崩潰,因爲索引8不存在。

具體來說,你告訴你的表,你有一定數目的細胞/行:

brandArray [段] .sectionBrands.count

這意味着每一個整數,從0開始,到任何brandArray [section] .sectionBrands.count是,表格將要求您生成一個單元格。因此,這是您的indexPath.row可以具有的範圍。

但是:在您的prepareForSegue中,您正在訪問brandAnswerArray [indexPath.row],並且brandAnswerArray根本沒有足夠的值來給予您所要求索引處的任何內容(這是一種風險,因爲您使用了不同的部分的數據構建表)。

+0

我明白你在說什麼,但是,我的ThirdArray包含兩個值,並且有兩個單元格。這應該不夠嗎?那麼,我知道應用程序繼續崩潰的答案。我如何將值分配給brandAnswerArray?什麼是最好的方法? – TheNoviceProgrammer