Get Up & Code, MacKin Talk

iOS @available 속성 설정 본문

IOS

iOS @available 속성 설정

맥킨 2024. 4. 23. 22:38

일부 Delegate 등 시스템 제공 API를 몇 가지만 둘러보더라도 @available 과 같은 속성이 정의된 메소드들을 많이 접할 수 있습니다.

 

Xcode를 통해 개발을 하는 경우, @available 속성을 통해 특정 클래스, 메서드, 프로퍼티 또는 기타 코드 구성 요소의 사용 가능한 플랫폼 버전을 명시적으로 표시할 수 있으며, API의 버전 관리를 보다 세밀하게 제어하고, 버전 호환성을 지켜줄 수 있습니다.

 

@available

함수 위에서 @available을 작성하면, Xcode IDE의 도움을 받아 선택적으로 해당 속성을 지정할 수 있습니다.

 

@available(<#platform#>, introduced: <#version#>, deprecated: <#version#>, message: <#String#>)

 

플랫폼, 도입 버전, 폐지 버전, 등 여러 가지를 나타낼 수 있습니다.

// MARK: API 폐지(deprecated) 및 제거(obsoleted)
@available(iOS, deprecated: 10.0, obsoleted: 12.0, message: "Use AnotherClass")
class DeprecatedClass {
    // iOS 10.0에서 폐지되고 12.0에서 완전히 제거됨
}

// MARK: API 폐지 알림
@available(iOS, deprecated: 13.0, message: "Use NewClass instead")
class OldClass {
    // iOS 13.0부터 폐지 예정, NewClass를 사용하라는 메시지 제공
}

// MARK: 도입 버전 지정
@available(iOS 11, *)
class MyClass {
    // iOS 11 이상에서 사용 가능
}


// MARK: 특정 조건에서만 사용 가능하도록 제한
@available(iOS 14, macOS 11, *)
func useNewFeature() {
    // iOS 14와 macOS 11 이상에서만 사용 가능
}

// MARK: 플랫폼 별로 다른 버전 지정
@available(iOS 10, macOS 10.12, *)
class CrossPlatformClass {
    // iOS 10 이상, macOS 10.12 이상에서 사용 가능
}

 

해당 문법의 도입은 developer.apple.com, Swift.org, Xcode Documentation을 찾아보시면 더 자세하게 살펴볼 수 있을 듯 합니다.

 

swift.org 링크 첨부합니다. 보다 자세한 내용은 하단 링크에서 알아보세요.

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/attributes/#available

 

Documentation

 

docs.swift.org

 

 

+ Xcode document에도 Available이 있네요. overview에 적혀 있듯 비슷한 역할을 하는 것 같습니다.

 

ref)

https://swift.org/documentation/ 

 

Swift.org

Swift is a general-purpose programming language built using a modern approach to safety, performance, and software design patterns.

www.swift.org

https://developer.apple.com/library/archive/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051-CH1-SW2