2016-04-24 169 views
-2

我得到这个错误:致命错误:索引超出范围。我无法得到我做错了什么。我试图做的是,通过使用访问数组字典整数索引比传递一个字符串来获取映射到它的值。示例在操场上工作正常,但不能解释为什么? (数组字典不为空) I have run the same code on playground and it workedthe xcode errorthe array is not empty 这里是我的代码致命错误:索引超出范围

var CondoIndivi2 = [[String:AnyObject]]() 


override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 

    scrollView.contentSize.height = 1500 


    print(CondoIndivi2) 
    if let description_Condo = self.CondoIndivi2[0]["description"] as? String { 

     print(description_Condo) 

    } 



} 




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


} 

这是将数据发送到CondoIndivi2视图

 import UIKit 
     import Alamofire 
     import SwiftyJSON 

     class SignleCondoTableViewController: UITableViewController { 
@IBOutlet var tableview: UITableView! 

var singleCondoData = [[String:AnyObject]]() 
var CondoIndivi = [[String:AnyObject]]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem() 

    self.tableView.delegate = self 
    self.tableView.dataSource = self 
    } 

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

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int  { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
    } 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return singleCondoData.count 
} 


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


    if singleCondoData.count != 0 { 


     let dict = singleCondoData[indexPath.row] as NSDictionary 


     //cell.label1.text? = (dict["name"] as? String)! 

     if let nullcheck = (dict["address"] as? String) { 
      cell.label2.text? = nullcheck 
     } 

    } 



    return cell 
    } 



override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    let dict = singleCondoData[indexPath.row] as NSDictionary 


    if let unitNullCheck = (dict["mls_number"] as? String) { 
     let item_id = unitNullCheck 
     getCondoUnits(item_id) 
     print(item_id) 

     } 



     } 

     //get the individual condo id 

func getCondoUnits(condo_id : String){ 


    Alamofire.request(.GET, "http://android.goidx.com/search/?mls_number=" + String(condo_id)).validate().responseJSON { response in 

     if let value = response.result.value { 
      let json = JSON(value) 

      if let resData = json.arrayObject { 

       self.CondoIndivi = resData as! [[String:AnyObject]] 

       print(self.CondoIndivi) 


      } 

      if self.CondoIndivi.count > 0 { 

       self.tableview.reloadData() 
      } 


     } 

     } 

     } 



override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if let identifier = segue.identifier { 


     switch identifier { 

     case "details" : 


      let buildingdDetailVC = segue.destinationViewController as! DetailsViewController 


      buildingdDetailVC.CondoIndivi2 = self.CondoIndivi 
     default: 
      break 
     } 

      } 

     } 







/* 
// Override to support conditional editing of the table view. 
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    // Return false if you do not want the specified item to be editable. 
+0

'self.CondoIndivi2'最初是空的。你有没有把东西放进去? – BallpointBen

+0

我刚刚编辑了这个问题。是的,我发送数据给它使用prepareforsegue –

+0

你认为可能是什么问题? –

回答

0

正如@詹姆斯把他的评论,你”重新在你的代码中创建一个空数组:

var CondoIndivi2 = [[String:AnyObject]]() 

然后你试图访问我ndexing在位置0:

if let description_Condo = self.CondoIndivi2[0]["description"] as? String { 
    print(description_Condo) 
} 

当然,你将有Index of out Range运行时错误,因为你的阵列它是空的,你总是需要索引之前,以确保一个数组,该指数大于零,小于等于数组的大小,并且数组不是空的。

我希望这对你有所帮助。

+0

我刚刚编辑了这个问题。是的,我发送数据给它使用prepareforsegue.The数组不是空的 –

0

您的getCondoUnits(condo_id : String)内部是异步块(Alamofire.request),CondoIndivi2比viewDidLoad执行时晚收到。您只需将condo_id传递给下一个viewController并在其中执行请求。

相关问题