How do I get the count of a Swift enum?

As of Swift 4.2 (Xcode 10) you can declare conformance to the CaseIterable protocol, this works for all enumerations without associated values: enum Stuff: CaseIterable { case first case second case third case forth } The number of cases is now simply obtained with print(Stuff.allCases.count) // 4 For more information, see SE-0194 Derived Collection of … Read more

Why would an Enum implement an Interface?

Here’s one example (a similar/better one is found in Effective Java 2nd Edition): public interface Operator { int apply (int a, int b); } public enum SimpleOperators implements Operator { PLUS { int apply(int a, int b) { return a + b; } }, MINUS { int apply(int a, int b) { return a – … Read more

How to set enum to null

You can either use the “?” operator for a nullable type. public Color? myColor = null; Or use the standard practice for enums that cannot be null by having the FIRST value in the enum (aka 0) be the default value. For example in a case of color None. public Color myColor = Color.None;

How do I make an enum Decodable in Swift?

It’s pretty easy, just use String or Int raw values which are implicitly assigned. enum PostType: Int, Codable { case image, blob } image is encoded to 0 and blob to 1 Or enum PostType: String, Codable { case image, blob } image is encoded to “image” and blob to “blob” This is a simple … Read more

How to use enums as flags in C++?

The “correct” way is to define bit operators for the enum, as: enum AnimalFlags { HasClaws = 1, CanFly = 2, EatsFish = 4, Endangered = 8 }; inline AnimalFlags operator|(AnimalFlags a, AnimalFlags b) { return static_cast<AnimalFlags>(static_cast<int>(a) | static_cast<int>(b)); } Etc. rest of the bit operators. Modify as needed if the enum range exceeds int … Read more

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