swift-protocols

How to create a generic function in Swift that will reject the given parameter unless it is an Optional?

旧巷老猫 提交于 2020-05-17 06:56:06
问题 This question is a follow up to my earlier question: I expected the system to report non protocol conformance, but it does not! Why? Please read the referred question for you to get a better idea of the constraints at hand. I created a generic function in Swift that will reject its parameter unless such a parameter is Optional . The function I created fully works and does what I desire. Meaning, any calls to onlyCallableByAnOptable(...) , even inside an if let , will yield error due to non

Pass a Swift class as parameter, and then call a class method out of it

故事扮演 提交于 2020-04-12 15:59:15
问题 I want to be able to store a class as a variable, so I can call class methods out of it later, something like this: class SomeGenericItem: NSObject { var cellClass: AnyClass init(cellClass: AnyClass) { self.cellClass = cellClass } func doSomething(p1: String, p2: String, p3: String) { self.cellClass.doSomething(p1, p2: p2, p3: p3) } } class SomeClass: NSObject { class func doSomething(p1: String, p2: String, p3: String) { ... } } I want to be able to say something like: let someGenericItem =

I expected the system to report non protocol conformance, but it does not! Why?

巧了我就是萌 提交于 2020-03-22 09:06:39
问题 I am using Xcode Version 11.3.1 (11C504) I am trying to create a generic function in Swift that will reject its parameter unless such a parameter is Optional. In the following code, I was expecting the system to report errors in all calls to onlyCallableByAnOptable() made inside test() , because none of them provide an optional value as a parameter. However, the system only reports non-protocol conformance if I remove the Optional extension that conforms to Optable ! Which to me, it means

iOS: How to use decodable for a model class with variable of type a protocol

纵饮孤独 提交于 2020-03-06 11:05:33
问题 Question: There is a model class that conforms to Decodable, this model has a variable of type someProtocol. But compiler gives an error, Compiler error Type 'MyModel' does not conform to protocol 'Decodable' Protocol requires initializer 'init(from:)' with type 'Decodable' Cannot automatically synthesize 'Decodable' because 'SomeProtocol' does not conform to 'Decodable' Code Snippet class MyModel: Decodable { var name: String? var employee: SomeProtocol? enum CodingKeys: String, CodingKey {

How can I call a static function on a protocol in a generic way?

房东的猫 提交于 2020-01-22 10:32:53
问题 Is there a point to declaring a static function on a protocol? The client using the protocol has to call the function on a type conforming to the protocol anyway right? That breaks the idea of not having to know the type conforming to the protocol IMO. Is there a way to call the static function on the protocol in a way where I don't have to know the actual type conforming to my protocol? 回答1: Nice question. Here is my humble point of view: Is there a point to declaring a static function on a

How can I call a static function on a protocol in a generic way?

冷暖自知 提交于 2020-01-22 10:30:14
问题 Is there a point to declaring a static function on a protocol? The client using the protocol has to call the function on a type conforming to the protocol anyway right? That breaks the idea of not having to know the type conforming to the protocol IMO. Is there a way to call the static function on the protocol in a way where I don't have to know the actual type conforming to my protocol? 回答1: Nice question. Here is my humble point of view: Is there a point to declaring a static function on a

Swift - class method which must be overridden by subclass

我只是一个虾纸丫 提交于 2020-01-19 04:38:16
问题 Is there a standard way to make a "pure virtual function" in Swift, ie. one that must be overridden by every subclass, and which, if it is not, causes a compile time error? 回答1: You have two options: 1. Use a Protocol Define the superclass as a Protocol instead of a Class Pro : Compile time check for if each "subclass" (not an actual subclass) implements the required method(s) Con : The "superclass" (protocol) cannot implement methods or properties 2. Assert in the super version of the method

How can I correctly use associatedType in my protocol

徘徊边缘 提交于 2020-01-16 02:50:50
问题 I’m trying to come up with an protocol-oriented MVVM for my tableviewcells. I have lots of them. my viewModel protocol PlainTableViewCellModelType { var backgroundColor : UIColor {get} var textColor: UIColor {get} var titleFont : UIFont {get } var accessoryType : UITableViewCellAccessoryType {get} var textLabelNumberOfLines: Int {get} } my view protocol PlainTableViewCellType{ associatedtype viewModel : PlainTableViewCellModelType func setupUI(forViewModel viewModel: viewModel) } my class