Why do I get an exception when I try to access a list of my own type in Swift's class?

Asked 2 years ago, Updated 2 years ago, 35 views

The question is whether the code below is correct or not.
In XCode 9.0, this is the code for Swift 4.

When I tried to execute it with the code below
error:Execution was interrupted, reason:EXC_BAD_ACCESS (code=2, address=0x7ffff5c02cff8).
1. Please let me know the reason
2. Is the code below wrong in the first place?
Have your own instance in the class or declare your own instance
 Is it possible to define an array to store your own instances?

class Test {
    vartest:Double {
        get{
            return self.test+100
        }
        set(a){
            self.test=a
        }
    }

    vartestList —Array<Test>=Array()

    init(a:Double) {
        self.test=a
        addTest(a:a)
    }

    funcaddTest(a:Double) {
        let t : Test = Test (a:a)
        testList.append(t)
    }
}

vartest1: Test=Test(a:1) ※←Exception display location
print(test1.testList[0].test)

swift

2022-09-30 19:02

1 Answers

Answer 1:

addTest()function{

Initiator {

addTest()function{

}

}

}

Answer 2: YES. Wrong for the above reasons.

so that it stops after repeating it a certain number of times.
 init(a:Double, count:Int=0){
    self.test=a
    addTest(a:a, count+1)
}

funcaddTest(a:Double, count:Int){
    if count<5{
        let t : Test = Test (a:a, count:count)
        testList.append(t)
    }
}

I think it's OK to count the number of loops in this way (unless it has to last indefinitely).

NOTE: var testList: Array<Test>=Array()var testList:[Test]=[]


2022-09-30 19:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.