2016-04-23 59 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

感谢它的工作 –

相关问题