0

I have the follow issue in my code:

class A {
    smth1: [[(Int,Int)]] = [[(1,2),(2,3)],
                            [(3,4),(4,5)]]

    var inst = B(smth2: smth1 [1])
}

class B {
    init (smth2: [(Int,Int)]){
    ...}
}

XCode generate error message: Could not find an overload for 'subscript' that accepts the suplied arguments

1 Answer 1

3

Here is how you should do it:

class A {
    let smth1: [[(Int,Int)]] = [[(1,2),(2,3)],
                                [(3,4),(4,5)]]

    @lazy var inst: B = {
        return B(smth2:self.smth1[1])
    }()
}

You cannot use instance properties before initialization/instantiation. I.e. there is no self hence no way to access smth1 property, even if it is constant.

Alternatively you can declare smth1 as class variable and access without @lazy initialization:

class A {
    class var smth1: [[(Int,Int)]] {
        return [[(1,2),(2,3)],[(3,4),(4,5)]]
    }

    var inst: B = B(smth2:smth1[1])
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.