3

I'm getting the following error:

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

It's strange this assignment was working before then when I restarted my Xcode, I started to get this error. From what I have read online, this should not give the error.

Here is my code:

    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
        }

       }

       }


       }
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
Ngoma Mbaku Davy
  • 169
  • 3
  • 11

1 Answers1

1

The type of CondoIndivi2 variable is [[String: AnyObject?]] but you are passing an array of type [[String: AnyObject]] where the dictionary values are non-optional.

Since any non-optional value with same type can be safely converted to its optional corresponding, you can do the following:

buildingdDetailVC.CondoIndivi2  = self.CondoIndivi.map { $0 as [String: AnyObject?] }
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
  • I see . it wasn't supposed to be an optional .Thanks and while we are at it quick question i m trying to retrieve the value from that array dictinary like this if let bath = self.CondoIndivi2["bath"] as! [[String:AnyObject]]{ self.label1.text = bath } but i m getting this error Cannot subscript a value of type '[[String : AnyObject]]' with an index of type 'String' I have digged the whole internet but this error doesnt not give me the right problem . what do you think – Ngoma Mbaku Davy Apr 23 '16 at 05:10
  • `CondoIndivi2` is an array of dictionaries, not a dictionary. Thus, you should use integer indices to get the dictionary first than use the string `"bath"` to get the value mapped to it. Example: `if let bath = self.CondoIndivi2[0]["bath"] as? String { ...` – Ozgur Vatansever Apr 23 '16 at 05:20