2016-04-23 60 views
3

,我發現了以下錯誤:無法分配型「[[字符串:AnyObject]」的值輸入「[[字符串:AnyObject]」

Cannot assign value of type '[[String : AnyObject]]' to type '[[String : AnyObject?]]'

很奇怪這個任務是工作之前,當我重新啓動我的Xcode,我開始得到這個錯誤。從我在網上閱讀的內容來看,這不應該給出錯誤。

這是我的代碼:

import UIKit 
    import Alamofire 
    import SwiftyJSON 

    class Signal Condo TableViewController: 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 // line of the error 
      default: 
      break 
     } 

     } 

     } 


     } 

回答

1

類型CondoIndivi2變量的是[[String: AnyObject?]]但要傳遞[[String: AnyObject]]其中詞典是非可選類型的陣列。

由於同類型的任何非可選值可以安全地轉換到其可選相應的,你可以做到以下幾點:

buildingdDetailVC.CondoIndivi2 = self.CondoIndivi.map { $0 as [String: AnyObject?] } 
+0

我明白了。它不應該是一個可選的。謝謝,雖然我們在它的快速問題,我試圖從這個數組字典中檢索這個值如這個 如果讓bath = self.CondoIndivi2 [「bath」] as! [[String:AnyObject]] { self.label1.text = bath } 但即時獲取此錯誤不能下標類型爲'[[String:AnyObject]]'的值與'String'類型的索引我挖了整個互聯網,但這個錯誤並沒有給我正確的問題。你覺得怎麼樣 –

+0

'CondoIndivi2'是一組字典,而不是字典。因此,您應該使用整數索引來獲取字典,而不是使用字符串'「bath」來獲取映射到它的值。例如:'如果讓bath = self.CondoIndivi2 [0] [「bath」]爲?字符串{...' – ozgur

+0

感謝它的工作 –

相關問題