swift-protocols

How to define optional methods in Swift protocol?

梦想与她 提交于 2019-11-26 06:13:08
问题 Is it possible in Swift? If not then is there a workaround to do it? 回答1: 1. Using default implementations (preferred). protocol MyProtocol { func doSomething() } extension MyProtocol { func doSomething() { /* return a default value or just leave empty */ } } struct MyStruct: MyProtocol { /* no compile error */ } Advantages No Objective-C runtime is involved (well, no explicitly at least). This means you can conform structs, enums and non- NSObject classes to it. Also, this means you can take

What does “Protocol … can only be used as a generic constraint because it has Self or associated type requirements” mean?

≡放荡痞女 提交于 2019-11-26 06:00:32
问题 I am trying to create a Dictionary (actually a HashSet) keyed on a custom protocol in Swift, but it is giving me the error in the title: Protocol \'myProtocol\' can only be used as a generic constraint because it has Self or associated type requirements and I can\'t make heads nor tails of it. protocol Observing: Hashable { } var observers = HashSet<Observing>() 回答1: Protocol Observing inherits from protocol Hashable , which in turn inherits from protocol Equatable . Protocol Equatable has

What is the in-practice difference between generic and protocol-typed function parameters?

只愿长相守 提交于 2019-11-26 05:36:45
问题 Given a protocol without any associated types: protocol SomeProtocol { var someProperty: Int { get } } What is the difference between these two functions, in practice (meaning not \"one is generic and the other is not\")? Do they generate different code, do they have different runtime characteristics? Do these differences change when the protocol or functions become non-trivial? (since a compiler could probably inline something like this) func generic<T: SomeProtocol>(some: T) -> Int { return

Unable to use protocol as associatedtype in another protocol in Swift

社会主义新天地 提交于 2019-11-26 05:29:11
I have a protocol, Address , which inherits from another protocol, Validator , and Address fulfills the Validator requirement in the extension. There is another protocol, FromRepresentable , which has an associatedType ( ValueWrapper ) requirement which should be Validator . Now if I try to use Address as associatedType , then it does not compile. It says, Inferred type 'Address' (by matching requirement 'valueForDetail') is invalid: does not conform to 'Validator'. Is this usage illegal? Shouldn't we be able to use Address in place of Validator , as all Addresses are Validator . Below is the

How can I make a weak protocol reference in &#39;pure&#39; Swift (without @objc)

耗尽温柔 提交于 2019-11-26 03:46:59
问题 weak references don\'t seem to work in Swift unless a protocol is declared as @objc , which I don\'t want in a pure Swift app. This code gives a compile error ( weak cannot be applied to non-class type MyClassDelegate ): class MyClass { weak var delegate: MyClassDelegate? } protocol MyClassDelegate { } I need to prefix the protocol with @objc , then it works. Question: What is the \'pure\' Swift way to accomplish a weak delegate ? 回答1: You need to declare the type of the protocol as class .

Protocol can only be used as a generic constraint because it has Self or associatedType requirements

£可爱£侵袭症+ 提交于 2019-11-26 02:54:46
问题 I have a protocol RequestType and it has associatedType Model as below. public protocol RequestType: class { associatedtype Model var path: String { get set } } public extension RequestType { public func executeRequest(completionHandler: Result<Model, NSError> -> Void) { request.response(rootKeyPath: rootKeyPath) { [weak self] (response: Response<Model, NSError>) -> Void in completionHandler(response.result) guard let weakSelf = self else { return } if weakSelf.logging { debugPrint(response)

Swift protocol with “where Self” clause

青春壹個敷衍的年華 提交于 2019-11-26 02:11:58
问题 In addition to this syntax with a protocol extension: protocol P {} extension P where Self : UIView {} ... I discovered by accident that you can use the same where clause on the protocol itself: protocol P where Self : UIView {} Notice that this is not the same as a where clause constraining a generic protocol, and does not itself make P a generic protocol. My experiments seem to show that only a colon can be used here, and the thing after the colon must be a class or a protocol (which may be

Unable to use protocol as associatedtype in another protocol in Swift

南楼画角 提交于 2019-11-26 01:54:43
问题 I have a protocol, Address , which inherits from another protocol, Validator , and Address fulfills the Validator requirement in the extension. There is another protocol, FromRepresentable , which has an associatedType ( ValueWrapper ) requirement which should be Validator . Now if I try to use Address as associatedType , then it does not compile. It says, Inferred type \'Address\' (by matching requirement \'valueForDetail\') is invalid: does not conform to \'Validator\'. Is this usage

Protocol func returning Self

时光毁灭记忆、已成空白 提交于 2019-11-25 23:44:10
问题 I have a protocol P that returns a copy of the object: protocol P { func copy() -> Self } and a class C that implements P: class C : P { func copy() -> Self { return C() } } However, whether I put the return value as Self I get the following error: Cannot convert return expression of type \'C\' to return type \'Self\' I also tried returning C . class C : P { func copy() -> C { return C() } } That resulted in the following error: Method \'copy()\' in non-final class \'C\' must return Self to

Why can&#39;t a get-only property requirement in a protocol be satisfied by a property which conforms?

笑着哭i 提交于 2019-11-25 22:46:30
问题 Why does the following code produce an error? protocol ProtocolA { var someProperty: ProtocolB { get } } protocol ProtocolB {} class ConformsToB: ProtocolB {} class SomeClass: ProtocolA { // Type \'SomeClass\' does not conform to protocol \'ProtocolA\' var someProperty: ConformsToB init(someProperty: ConformsToB) { self.someProperty = someProperty } } The answer in this similar question makes sense. However, in my example, the property is get-only. Why shouldn\'t this work? Is it a