scoobyDoo
scoobyDoo

Reputation: 123

Swift, error: ambiguous use of subscript

I am getting this error: ambiguous use of subscript, and I don't know how to resolve it. Could you please help ? The first one is at the line:

next = next[sub]

Here is the code:

public subscript(path: [SubscriptType]) -> JSON {
    get {
        if path.count == 0 {
            return JSON.nullJSON
        }

        var next = self
        for sub in path {
            next = next[sub]
        }
        return next
    }
    set {

        switch path.count {
        case 0: return
        case 1: self[path[0]] = newValue
        default:
            var last = newValue
            var newPath = path
            newPath.removeLast()
            for sub in Array(path.reverse()) {
                var previousLast = self[newPath]
                previousLast[sub] = last
                last = previousLast
                if newPath.count <= 1 {
                    break
                }
                newPath.removeLast()
            }
            self[newPath[0]] = last
        }
    }
}

Thank you very much,

Upvotes: 0

Views: 511

Answers (1)

buildsucceeded
buildsucceeded

Reputation: 4243

You will need to tell the compiler what data type self is, like:

var next = self as? [String]

or

var next = self as? [String:AnyObject]

If you're not sure you will want to do a branching condition like

if let next = self as? [String] 
     { // foo } 
else if let next = self as? [String:AnyObject] 
     { // bar } 

(I am not sure exactly what type self is here, you may need other types than [String] and [String:AnyObject]).

Upvotes: 0

Related Questions