Xcode 8 beta 6 AnyObject Swift 3 changes
Asked Answered
N

2

7

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

Nautilus answered 18/8, 2016 at 13:53 Comment(0)
T
7

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.

Terrell answered 18/8, 2016 at 14:18 Comment(13)
unfortunately even with either options they both produce the error Type Any has no subscript members still.Nautilus
As my data is a JSON feed I was not sure how to use either a custom class or struct as you suggest ?Nautilus
How is values declared?Terrell
values = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSMutableArrayNautilus
Do not use NSMutableArray use at least a Swift Array [[String:AnyObject]] or [[String:Any]]. The declaration as var gives you mutability for free.Terrell
Thanks I think I was forced down the NSArray thing by mistake in the first instance.. so I can make values as var values:Any = [] but let maindata = values[(indexPath as NSIndexPath).row] - complains....Nautilus
I have to rewrite - values = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSMutableArray ? but not sure whatNautilus
Please, please don't declare an obvious array simply as Any. That's the most unspecified type at all. Help the compiler then the compiler will help you. The JSON collection types are either dictionary [String:AnyObject] or array [[String:AnyObject]] (in Xcode 8 maybe Any for AnyObject), so cast the JSON serialization to [[String:Any(Object)]]Terrell
Let us continue this discussion in chat.Nautilus
Vadian with this update the compiler says, "Method does not override any methods from its superclass". Then when I change the line to override func awake(withContext context: Any?) the error goes away. Is it ok to simply change it to Any??Scientistic
Yes, it is. All AnyObject types in parameters which represent id have been changed to AnyTerrell
Thanks Vadian. Also I tried to find the same release notes Release Notes but cannot find code changes. Do you have a link?Scientistic
I guess the release notes of the beta versions are available only in a developer account.Terrell
N
0

I needed to make changes to my approach and ditch NSMutableArray (which I am pleased about)

so I declare the empty array as follows

var values = [[String:AnyObject]]()

and pop the data into it like so now

values = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [[String : AnyObject]];

one other minor tweak

let maindata = values[(indexPath).row]

Job done -thanks @vadian for jumping to chat to help me understand his answer which is technically correct

Nautilus answered 18/8, 2016 at 15:11 Comment(1)
Isn't 'values' an array of dictionaries?Fossiliferous

© 2022 - 2024 — McMap. All rights reserved.