약한 연결-클래스가 있는지 확인하고 해당 클래스를 사용합니다.
범용 iPhone 앱을 만들려고하는데 최신 버전의 SDK에서만 정의 된 클래스를 사용합니다. 프레임 워크는 이전 시스템에 존재하지만 프레임 워크에 정의 된 클래스는 존재하지 않습니다.
어떤 종류의 약한 링크를 사용하고 싶지만 함수 존재에 대한 런타임 검사에 대한 모든 문서를 찾을 수 있습니다. 클래스가 존재하는지 어떻게 확인합니까?
TLDR
흐름:
- 스위프트 :
if #available(iOS 9, *)
- Obj-C, iOS :
if (@available(iOS 11.0, *))
- Obj-C, OS X :
if (NSClassFromString(@"UIAlertController"))
유산:
- Swift (2.0 이전 버전) :
if objc_getClass("UIAlertController")
- Obj-C, iOS (4.2 이전 버전) :
if (NSClassFromString(@"UIAlertController"))
- Obj-C, iOS (11.0 이전 버전) :
if ([UIAlertController class])
Swift 2 이상
역사적으로 특정 OS 버전이 아닌 기능 (또는 클래스 존재)을 확인하는 것이 권장되었지만 가용성 확인 이 도입 되었기 때문에 Swift 2.0에서는 제대로 작동하지 않습니다 .
대신 다음 방법을 사용하십시오.
if #available(iOS 9, *) {
// You can use UIStackView here with no errors
let stackView = UIStackView(...)
} else {
// Attempting to use UIStackView here will cause a compiler error
let tableView = UITableView(...)
}
참고 : 대신을 사용하려고 objc_getClass()
하면 다음 오류가 발생합니다.
⛔️ 'UIAlertController'는 iOS 8.0 이상에서만 사용할 수 있습니다.
이전 버전의 Swift
if objc_getClass("UIAlertController") != nil {
let alert = UIAlertController(...)
} else {
let alert = UIAlertView(...)
}
주 objc_getClass()
보다 더 신뢰할 수있다 NSClassFromString()
거나objc_lookUpClass()
.
Objective-C, iOS 4.2 이상
if ([SomeClass class]) {
// class exists
SomeClass *instance = [[SomeClass alloc] init];
} else {
// class doesn't exist
}
자세한 내용은 code007의 답변을 참조하십시오 .
OS X 또는 이전 버전의 iOS
Class klass = NSClassFromString(@"SomeClass");
if (klass) {
// class exists
id instance = [[klass alloc] init];
} else {
// class doesn't exist
}
사용 NSClassFromString()
. 를 반환 nil
하면 클래스가 존재하지 않는 것이고, 그렇지 않으면 사용할 수있는 클래스 객체를 반환합니다.
This is the recommended way according to Apple in this document:
[...] Your code would test for the existence of [a] class using
NSClassFromString()
which will return a valid class object if [the] class exists or nil if it doesnʼt. If the class does exist, your code can use it [...]
For new projects that uses a base SDK of iOS 4.2 or later, there is this new recommended approach which is to use the NSObject class method to check the availability of weakly linked classes at run time. i.e.
if ([UIPrintInteractionController class]) {
// Create an instance of the class and use it.
} else {
// Alternate code path to follow when the
// class is not available.
}
This mechanism uses the NS_CLASS_AVAILABLE macro, which is available for most framework in iOS (note there may be some framework that do not yet support the NS_CLASS_AVAILABLE - check the iOS release note for this). Extra setting configuration may also be needed that can be read in the Apple's documentation link provided above, however, the advantage of this method is that you get static type checking.
참고URL : https://stackoverflow.com/questions/3057325/weak-linking-check-if-a-class-exists-and-use-that-class
'developer tip' 카테고리의 다른 글
Sublime Text 2 구성 / 플러그인을 저장 / 복원하여 다른 컴퓨터로 마이그레이션하는 방법은 무엇입니까? (0) | 2020.09.11 |
---|---|
Android Studio에 ZXing 통합 (0) | 2020.09.11 |
다른 그루비에 그루비 스크립트 포함 (0) | 2020.09.11 |
디버그에서 애플리케이션 인사이트 비활성화 (0) | 2020.09.11 |
가져 오기 쉬운 Git 커밋 통계 (0) | 2020.09.11 |