无法指定类型' [[String:AnyObject]]'的值输入' [[String:AnyObject?]]'

时间:2016-04-23 04:30:06

标签: arrays swift swift-dictionary

我收到以下错误:

  

无法指定类型' [[String:AnyObject]]'的值输入' [[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 个答案:

答案 0 :(得分:1)

CondoIndivi2变量的类型为[[String: AnyObject?]],但您传递的是[[String: AnyObject]]类型的数组,其中字典是非可选的。

由于任何具有相同类型的非可选值都可以安全地转换为可选的相应值,因此您可以执行以下操作:

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