0
Obj-C의 @property (nonatomic) NSArray <SomeProtocol>* items;
을 미러링하는 최선의 방법은 항목이 UIView
하위 클래스 인 경우입니다. 아래의 예에서 프로토콜을 준수하는 UIView 하위 클래스의 신속한 배열
UIKit
구성 요소 (예 :
UILabel
,
UIButton
등)의 배열을 저장하고 싶은 몇 가지 다른 방법은 무엇
Protocol can only be used as a generic constraint because it has Self or associated type requirements
이 모델링?
예 놀이터 :
import UIKit
/// Protocol representing a form field model
protocol FieldRepresentable {}
/// Protocol representing a form UI control
protocol FormControllable {
associatedtype FieldRepresentable
init(model: FieldRepresentable)
var model: FieldRepresentable { get }
}
/// Example label model
class FormLabelElement: FieldRepresentable {}
/// Example label UI control
class FormLabel: UILabel, FormControllable {
required init(model: FormLabelElement) {
self.model = model
super.init(frame: CGRect.zero)
}
let model: FormLabelElement
required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
}
/// Example form
class Form: UIView {
// Error here
var fields: [FormControllable]?
}
아마도이 기사는 도움이 될 수 있습니다. https://milen.me/writings/swift-generic-protocols/ – nebs
프로토콜을 클래스에만 적용한다는 것을 나타 내기 위해 프로토콜을': class'로 선언해야합니다 구조체가 아닌 객체 – Paulw11