Swift에서 함수 매개 변수로 프로토콜을 준수하는 클래스
Objective-C에서는 프로토콜을 따르는 클래스를 메소드 매개 변수로 지정할 수 있습니다. 예를 들어 다음 UIViewController을 준수하는 a 만 허용하는 메서드를 가질 수 있습니다 UITableViewDataSource.
- (void)foo:(UIViewController<UITableViewDataSource> *)vc;
Swift에서이 작업을 수행하는 방법을 찾을 수 없습니다 (아마 아직 불가능할 수도 있습니다). 을 사용하여 여러 프로토콜을 지정할 수 func foo(obj: protocol<P1, P2>)있지만 객체가 특정 클래스에 속하도록 어떻게 요구합니까?
foo일반 함수로 정의 하고 유형 제약 조건을 사용하여 클래스와 프로토콜을 모두 요구할 수 있습니다.
스위프트 4
func foo<T: UIViewController & UITableViewDataSource>(vc: T) {
.....
}
Swift 3 (Swift 4에서도 작동)
func foo<T: UIViewController>(vc:T) where T:UITableViewDataSource {
....
}
스위프트 2
func foo<T: UIViewController where T: UITableViewDataSource>(vc: T) {
// access UIViewController property
let view = vc.view
// call UITableViewDataSource method
let sections = vc.numberOfSectionsInTableView?(tableView)
}
Swift 4에서는 new & sign으로 이것을 달성 할 수 있습니다.
let vc: UIViewController & UITableViewDataSource
Swift 책 문서는 where 절과 함께 유형 제약 조건을 사용하도록 제안합니다.
func someFunction<C1: SomeClass where C1:SomeProtocol>(inParam: C1) {}
이는 "inParam"이 "SomeProtocol"을 준수하는 조건과 함께 "SomeClass"유형임을 보장합니다. 쉼표로 구분 된 여러 where 절을 지정할 수도 있습니다.
func itemsMatch<C1: SomeProtocol, C2: SomeProtocol where C1.ItemType == C2.ItemType, C1.ItemType: SomeOtherProtocol>(foo: C1, bar: C2) -> Bool { return true }
Swift 3를 사용하면 다음을 수행 할 수 있습니다.
func foo(_ dataSource: UITableViewDataSource) {
self.tableView.dataSource = dataSource
}
func foo(_ delegateAndDataSource: UITableViewDelegate & UITableViewDataSource) {
//Whatever
}
이 방법은 어떻습니까? :
protocol MyProtocol {
func getTableViewDataSource() -> UITableViewDataSource
func getViewController() -> UIViewController
}
class MyVC : UIViewController, UITableViewDataSource, MyProtocol {
// ...
func getTableViewDataSource() -> UITableViewDataSource {
return self
}
func getViewController() -> UIViewController {
return self
}
}
func foo(_ vc:MyProtocol) {
vc.getTableViewDataSource() // working with UITableViewDataSource stuff
vc.getViewController() // working with UIViewController stuff
}
2015 년 9 월 참고 : 이것은 Swift 초기의 관찰입니다.
불가능 해 보인다. Apple은 일부 API에서도 이러한 성가신 문제를 가지고 있습니다. 다음은 iOS 8에 새로 도입 된 클래스의 한 예입니다 (베타 5 기준).
UIInputViewController의 textDocumentProxy재산 :
Objective-C에서 다음과 같이 정의됩니다.
@property(nonatomic, readonly) NSObject<UITextDocumentProxy> *textDocumentProxy;
그리고 Swift에서 :
var textDocumentProxy: NSObject! { get }
'Programing' 카테고리의 다른 글
| mcrypt는 더 이상 사용되지 않습니다. 대안은 무엇입니까? (0) | 2020.09.15 |
|---|---|
| HTML에 유니 코드 기호 표시 (0) | 2020.09.15 |
| file.delete ()는 file.exists (), file.canRead (), file.canWrite (), file.canExecute () 모두가 true를 반환하더라도 false를 반환합니다. (0) | 2020.09.14 |
| Chrome 확장 : popup.html을 강제 종료 (0) | 2020.09.14 |
| Go에서 부분적으로 JSON을 맵으로 비 정렬 화 (0) | 2020.09.14 |