Ambiguous use of 'subscript' - Access array node inside class

Sorry for another dumb question, but I can't find an answer.

I want to know why to access a second node of an array outside of the main.

For exemple, my main:

let myArray = [["Andrea", 39], ["Vera", 70], ["Renata", 42], ["Joao Pedro", 10], ["Felipe", 4], ["Joao", 71]]
//this for work in the main but not in the class
for i in 0...(myArray.count - 1) {
    print("Name: \(myArray[i][0]) - Age: \(myArray[i][1])")
}


My class:

class AacTestArray: NSObject {
    func aacPrintArray(array: Array<AnyObject>) {
        let numRow: Int = array.count
     
        //this for doesnt work in the class
        for i in 0...(numRow - 1) {
            print("Name: \(array[i][0]) - Age: \(array[i][1])")
        }  
    }
}

when i use the for inside the class, i get this error on line 7:

Ambiguous use of 'subscript'


Thanks

print("Name: \(array[i][0) - Age: \(array[i][1])")


Aren't you simply missing the closing bracket around the 0


print("Name: \(array[i][0]) - Age: \(array[i][1])")

Sorry my code is with the bracket, I just wrote wrong here, still doesn't work.

The problem is that you declare a parameter "array" of type Array<AnyObject>, but you then double-subscript it. What you did is exactly the same as:


for i in 0...(numRow - 1) {
     let element: AnyObject = array [i]
     print("Name: \(element[0]) - Age: \(element[1])")
}


In that form, it's clear that the compiler cannot know how to apply the subscripts [0] or [1] to an AnyObject. Try declaring your method like this:


func aacPrintArray(array: Array<Array<AnyObject>>) { … }


However the real problem is a design issue. If your main array elements are always a pair of values (one String, one Int), then you should define a custom struct to hold them, rather than packaging them into 2-element sub-arrays. That way, your data is strongly typed, and the compiler can enforce correct usage in case you accidentally make a mistake.

Thanks Quincey.

I'm trying to understand how things work on Swift and it seems like a very burocratic language 😁

If you have an exemple showing how to define a custom struct, please let me know.

Thanks again

Here is an example with a struct:


struct Person {
    let name: String
    let age: Int
}
let myArray = [Person(name: "Andrea", age: 39),
               Person(name: "Vera", age: 70),
               Person(name: "Renata", age: 42),
               Person(name: "Joao Pedro", age: 10),
               Person(name: "Felipe", age: 4),
               Person(name: "Joao", age: 71)]
class AacTestArray: NSObject {
    func aacPrintArray(array: Array<Person>) {
        for person in array {
            print("Name: \(person.name) - Age: \(person.age)")
        }
    }
}


If you think it is too bureaucratic with structs, then here is an example with a simple tuple:


let myArray = [("Andrea", 39),
               ("Vera", 70),
               ("Renata", 42),
               ("Joao Pedro", 10),
               ("Felipe", 4),
               ("Joao", 71)]
class AacTestArray: NSObject {
    func aacPrintArray(array: Array<(String, Int)>) {
        for person in array {
            print("Name: \(person.0) - Age: \(person.1)")
        }
    }
}


or if you want to have named members in the tuple:


let myArray : [(name: String, age: Int)] =
    [("Andrea", 39),
     ("Vera", 70),
     ("Renata", 42),
     ("Joao Pedro", 10),
     ("Felipe", 4),
     ("Joao", 71)]
class AacTestArray: NSObject {
    func aacPrintArray(array: Array<(name: String, age: Int)>) {
        for person in array {
            print("Name: \(person.name) - Age: \(person.age)")
        }
    }
}


If you use named members in the tuple, you can choose if you want to initialize the tuple as ("Andrea", 39) or (name: "Andrea", age: 39) and it is still possible to access the members with .0 and .1 just like with unnamed members.

Wow, thanks Ahltorp 🙂

Ambiguous use of 'subscript' - Access array node inside class
 
 
Q