How to make an enum conform to a protocol in Swift?

This is my attempt: protocol ExampleProtocol { var simpleDescription: String { get } mutating func adjust() } enum ExampleEnum : ExampleProtocol { case Base, Adjusted var simpleDescription: String { return self.getDescription() } func getDescription() -> String { switch self { case .Base: return “A simple description of enum” case .Adjusted: return “Adjusted description of enum” … Read more

In Swift, how can I declare a variable of a specific type that conforms to one or more protocols?

In Swift 4 it is now possible to declare a variable that is a subclass of a type and implements one or more protocols at the same time. var myVariable: MyClass & MyProtocol & MySecondProtocol To do an optional variable: var myVariable: (MyClass & MyProtocol & MySecondProtocol)? or as the parameter of a method: func … Read more

Swift – class method which must be overridden by subclass

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 Example: class SuperClass … Read more

Non-‘@objc’ method does not satisfy optional requirement of ‘@objc’ protocol

While I think I can answer your question, it’s not an answer you will like. TL;DR: @objc functions may not currently be in protocol extensions. You could create a base class instead, though that’s not an ideal solution. Protocol Extensions and Objective-C First, this question/answer (Can Swift Method Defined on Extensions on Protocols Accessed in … Read more

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

Protocol Observing inherits from protocol Hashable, which in turn inherits from protocol Equatable. Protocol Equatable has the following requirement: func ==(lhs: Self, rhs: Self) -> Bool And a protocol that contains Self somewhere inside it cannot be used anywhere except in a type constraint. Here is a similar question.

Protocol doesn’t conform to itself?

Why don’t protocols conform to themselves? Allowing protocols to conform to themselves in the general case is unsound. The problem lies with static protocol requirements. These include: static methods and properties Initialisers Associated types (although these currently prevent the use of a protocol as an actual type) We can access these requirements on a generic … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)