Sazzad Hissain Khan Sazzad Hissain Khan - 2 months ago 20
Swift Question

Type 'NSDictionary!' has no subscript members Swift - 3.0 conversion issue

I am getting this compiler error below after auto conversion from Swift 2 to Swift 3,


Type 'NSDictionary!' has no subscript members


I have seen similar question in this post, but still the solution is not supposed to work for NSDictionary.

Please let me know how to fix it.

Code:

fileprivate var allData:NSDictionary!;
.
.
.
open func getData(_ key:String) -> AnyObject?
{
return allData[key]; // error in this line
}

Answer

In Swift 3, the Value type of NSDictionary has been changed to Any. So the result type of subscript allData[key] is Any?, which cannot be automatically converted to AnyObject?.

Try this:

open func getData(_ key: String) -> AnyObject?
{
    return allData[key] as AnyObject?
}

But, if you use your allData as shown, why don't you declare it as [String: AnyObject] ?

And the error message... Better send a bug report.

Comments