Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 8 beta 6 AnyObject Swift 3 changes

Xcode beta 6 has changed some of the Swift Language

Since ‘id’ now imports as ‘Any’ rather than ‘AnyObject’, you may see errors where you were previously performing dynamic lookup on ‘AnyObject’.

I have tried the fix to either cast to AnyObject explicitly before doing the dynamic lookup, or force cast to a specific object type

But am not sure I am doing it correctly - can someone help please here is original working code from Beta 5

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SpecialCell
        let maindata = values[(indexPath as NSIndexPath).row]
        cell.name.text = maindata["name"] as? String
        cell.id.text = maindata["id"] as? String
        //team_id.text = maindata["team_id"] as? String

        return cell
    }

https://www.dropbox.com/s/ln0vx3b9rbywv83/Screen%20Shot%202016-08-18%20at%2014.32.23.png?dl=0

like image 921
adamprocter Avatar asked Aug 18 '16 13:08

adamprocter


1 Answers

According to the beta 6 release notes you have to (bridge) cast to AnyObject

cell.name.text = (maindata["name"] as AnyObject) as? String

or force cast

cell.name.text = maindata["name"] as! String

That's one more reason to prefer custom classes / structs with distinct property types over common dictionaries.

like image 105
vadian Avatar answered Oct 20 '22 21:10

vadian