Search code examples
xcodeswift3swift2

Cannot subscript a value of type 'JSON' with an index of type '(key: String)'


Can someone help me with the following error ? I made a migration from swift 2 to swift 3 and in my code it gives me the following error:

Cannot subscript a value of type 'JSON' with an index of type '(key: String)'

and

Cannot subscript a value of type 'JSON' with an index of type '(index: Int)'

My code:

fileprivate subscript(sub: SubscriptType) -> JSON {
        get {
            if sub is String {
                return self[key:sub as! String]
            } else {
                return self[index:sub as! Int]
            }
        }
        set {
            if sub is String {
                self[key:sub as! String] = newValue
            } else {
                self[index:sub as! Int] = newValue
            }
        }
    }

Solution

  • Try to remove key: and index: from your code, i think will resolve.

    ex: self[sub as! String]