본문 바로가기
iOS/iOS

stored property에 'available' 사용하기

by 패쓰킴 2022. 8. 16.
728x90

스택오버플로우 발췌 - https://stackoverflow.com/questions/41904724/using-available-with-stored-properties

 

Here is one potential solution (thanks to blog post). The idea is to use a stored property with a type of Any and then create a computed property that will cast the stored property (and instantiate it if necessary).

private var _selectionFeedbackGenerator: Any? = nil
@available(iOS 10.0, *)
fileprivate var selectionFeedbackGenerator: UISelectionFeedbackGenerator {
    if _selectionFeedbackGenerator == nil {
        _selectionFeedbackGenerator = UISelectionFeedbackGenerator()
    }
    return _selectionFeedbackGenerator as! UISelectionFeedbackGenerator
}

Another option is to use lazy (however, this makes the variable read-write):

@available(iOS 10.0, *)
private(set) lazy var center = UNUserNotificationCenter.current()

 

728x90

'iOS > iOS' 카테고리의 다른 글

햅틱 & 진동  (0) 2022.09.06
화면 캡처 방지  (0) 2022.08.19
카카오 SDK 설정  (0) 2022.08.09
Objectice-C to Swift Migration  (0) 2022.08.03
PCH 파일  (0) 2022.07.29

댓글