swift-protocols

Protocol variable implementing another protocol

只谈情不闲聊 提交于 2021-02-19 05:53:26
问题 I'm trying to do something like that, but then, ParentC doesn't conform to Parent because its children member isn't Child but ChildC Which is weird because ChildC implements Child ... Is this a limit of Swift? Or is there is a way of doing that? (I do not ask for an alternative solution, I want to know if this is possible) protocol Parent: Codable { var children: [Child] { get } } protocol Child: Codable { var name: String { get } } struct ParentC: Parent { var children: [ChildC] } struct

How to implement a Swift protocol across structs with conflicting property names

谁说我不能喝 提交于 2021-02-19 04:24:43
问题 I'm trying to write a protocol that reconciles two different structs that describe the same concept, a stop of some kind. Both have a Code , Description , and Latitude & Longitude coordinates, but for one type, the Description could be nil , and for the other type, the coordinates might be nil . How can I write a single protocol that reconciles these two structs? Here's my protocol: protocol Stop { var Code : String { get } var Description : String { get } var Latitude : Double { get } var

How to create constraint on protocol

你离开我真会死。 提交于 2021-02-11 10:58:52
问题 I tried to create protocol which can be only implemented by classes which inherit from UIView , what was my surprise when this code compiles without errors (in Swift 3.0): protocol TestsProtocol { func test() } extension TestsProtocol where Self: UIView { } class FooClass: TestsProtocol { func test() { } } We can see that FooClass don't inherit from UIView , using protocol extension I wan't to force that only classes which inherit from UIView can implement it. As far as I remember this would

How to create constraint on protocol

我的梦境 提交于 2021-02-11 10:56:25
问题 I tried to create protocol which can be only implemented by classes which inherit from UIView , what was my surprise when this code compiles without errors (in Swift 3.0): protocol TestsProtocol { func test() } extension TestsProtocol where Self: UIView { } class FooClass: TestsProtocol { func test() { } } We can see that FooClass don't inherit from UIView , using protocol extension I wan't to force that only classes which inherit from UIView can implement it. As far as I remember this would

How to use protocols for stucts to emulate classes inheritance

扶醉桌前 提交于 2021-02-08 06:38:33
问题 I'm implementing a model: It has structs ClientSummary and ClientDetails ClientDetails struct has all properties of ClientSummary struct + some extra properties Both structs have main initializer init(jsonDictionary: [String: Any]) inits of ClientSummary and ClientDetails share big part of the code There is an extension which will work with shared functionality of those structs. The most straightforward solution which came to my mind is just classic inheritance, but it doesn't work for value

Generic class with class-bound constraint cannot be parametrized by class-bound protocol

放肆的年华 提交于 2021-02-05 11:11:58
问题 I'd like to have a generic weak reference to an object and parametrize it by a protocol that is class-bound. Here is the code example that does not work in my case: protocol Protocol: class { // also written as `protocol Protocol: AnyObject {` func function() } final class A: Protocol { func function() {} } final class Weak<Type> where Type: AnyObject { final private weak var property: Type? init(property: Type) { self.property = property } } let a = A() let something = Weak<Protocol>

Generic class with class-bound constraint cannot be parametrized by class-bound protocol

冷暖自知 提交于 2021-02-05 11:06:04
问题 I'd like to have a generic weak reference to an object and parametrize it by a protocol that is class-bound. Here is the code example that does not work in my case: protocol Protocol: class { // also written as `protocol Protocol: AnyObject {` func function() } final class A: Protocol { func function() {} } final class Weak<Type> where Type: AnyObject { final private weak var property: Type? init(property: Type) { self.property = property } } let a = A() let something = Weak<Protocol>

Implementing Objective C delegate in Swift

狂风中的少年 提交于 2021-01-28 04:41:09
问题 I have a Objective C class that has methods that look like this: @class Client; @protocol ClientDelegate <NSObject> @optional -(void) receivedMessageFromClient : (id) message; @end @interface Client : NSObject +(id) setup; @property(nonatomic, strong) id <ClientDelegate> clientDelegate; // more method declarations below I am implementing the ClientDelegate in my Swift class like this: class HomeViewController: UIViewController, ClientDelegate { var client : AnyObject? var delegate: