Sazzad Hissain Khan
Sazzad Hissain Khan

Reputation: 40186

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
}

Upvotes: 1

Views: 2856

Answers (1)

OOPer
OOPer

Reputation: 47886

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.

Upvotes: 1

Related Questions