Dev_Dylan

iOS_ViewController buttons를 array에 [Lazy] 본문

iOS/Solve_ERROR

iOS_ViewController buttons를 array에 [Lazy]

Dylan_21 2022. 4. 9. 21:10

 

ViewController에 버튼을 추가해서 배열에 담기

최초

 

StoryBoard 에서도

혹은, 코드로 UIButton을 선언해도

같은 오류가 난다.

 

 

Cannot use instance member 'button' within property initializer; property initializers run before 'self' is available

 

오류 코드에 보면

[인스턴스 맴버인 버튼을 초기화 없이 사용 할 수 없다]

라고 나온다.

 

Lazy 키워드로 해결

buttons 배열에 lazy를 사용해서 ERROR를 해결한다.

 

lazy키워드를 이용해 동시에 호출되는 것을 방지한다.

 

 

즉, 각각의 button1,button2,button3 와

이를 포함하는  [buttons] 배열을 동시에 호출되지 않도록

lazy var buttons 를 이용해서 배열이 사용 될 때에 호출 되도록 하는 것.

 

 

https://developer.apple.com/documentation/foundation/indexpath/1780078-lazy/

 

 

 

 

 

 

 

 

 

 

button 뿐만아니라 다른 타입들에서도 마찬가지로, class 내에서 생성 할 때 유의하면 된다.

VCLifeCycle, 객체LifeCycle 에 대해 이해하자.!

 

Comments