Flexible use of Swift 3 enum

Flexible use of Swift 3 enum

1. Preface

A half a year, from the preferred SF before switched to the current flash of silver, and have no time to write the first self-criticism, busy open source project, in the process of making all kinds of wheels, deeply appreciate the Swift enum convenience, directly on the code.

2. enum in Objective-C

Typedef, NS_ENUM (NSUInteger, UserType) {UserTypeStudent, UserTypeTeacher, UserTypeStaff, UserTypeAdministrator, UserTypeOther};

This is the enumeration defines a standard Objective-C style, to declare a type for the NSUInteger enumeration of UserType, what is the use of it, in most cases a lift is to explain the role of how to explain?

{"successful": true "userType": 0 / / possible for other values.

The background returns a string of JSON, userType may be 1~9, stepped on the pit apes who understand, if the userType field is used directly for return to the backstage business logic processing, may appear QianYiFaErDongQuanShen embarrassing situation, not only relates to the code specification, also give their own digging.
recommended practice is that the userType will return into other parts of the code after the enumeration, you can also call the background, if the change in the relationship of userType, we only need to change the corresponding mapping and enumeration

3. enum in Swift

Use the enumeration in the Objective-C, we will find that sometimes we don’t want to use the enumeration based type, such as NSUInteger and so on, we want to be typedef NS_ENUM (NSUInteger, UserType) NSUInteger into NSString, but the system does not support the definition of non basic enumeration types, is an invaild underlying error type, it leads to the enumeration is not so flexible, we look at the enumeration in the Swift

Enum, CIBlurStyle:, String{, case, extraLight = extraLight, case, light = light, case, dark = dark"

This is the enumeration of usage of Swift in the most basic types, we can specify the enumeration, by
can get to the original value of enumeration, we also can pass parameters in the enumeration, such as:

Enum CIBlurHUDType {Case Guide (Bool) case info (String) case error (Error) case other func (hudType: CIBlurHUDType)} handleEnum {switch {Void -> hudType case.Guide (let isAutoHide) case.Info (let: / / tip / /.Error): message of case (let error): / / of `error` treatment default: break}} / / how to use the handleEnum (.Info ("this is a text prompt ~") = NSError.init (domain:) let err "some unknown mistakes", code: 110, userInfo: nil (handleEnum).Error (ERR))

4. how to use enum flexibly in Swift

4.1 using enum to streamline code

The use of Swift in enum is very convenient and practical:

Public, enum, CIImageOperation, {case, cornerRadius (CGFloat), case, scale (CGSize), case, zip (CGFloat), case, other,}

We want to create a picture request library. After downloading, we may process the pictures above. Without using enum, we might declare many methods, such as:

Extensin UIImageView {func setImage (with url: URL cornerRadius: CGFloat -> func setImage; Void) {} (with url: URL scaleTo: CGSize -> func setImage; Void) {} (with url: URL zip: (Bool, CGFLoat) -> Void) {}}

Come on, let’s change the wording and see more streamlined

Extension UIImageView {func setImage (with url: URL, imageOperation: CIImageOperation -> Void) {/ / downloaded images for image processing switch imageOperation {case.CornerRadius (let cornerRadius): case (let.Scale / size tangential angle): / /.Zip (let zipValue case zoom size): / / picture clarity compression ratio of default: break}}} / / imagView.setImage (with url: www.codeinventor.club, imageOperation:.CornerRadius (3))

4.2 use enum to do simple package operation on network request result and so on

Enum, CIUrlResponse, {case, Result (Any), case, error (NSError)}

Usually after a network request is completed, will return a series of response, characteristics of enum can be used to pass parameters, we can use the enum package to the network request, to make it more intuitive and easy to understand, such as the following func:

Func getData (with url: URL) -> CIUrlResponse {};

Note: the enum parameters, use very convenient, but is not recommended to pass too many parameters, or too complex for example: Closure
enum {case negative HUDStyle loading (Bool, CGFloat, CGFloat, String (case) other ((Bool) -> ()))}… Do not comment, do not understand the meaning of this pile of parameters,
So. Swift in the enum, suitable for passing a small amount of parameters, or simple Closure, and function compared to enum bear more or explain the role?

5. follow up

In the near future to get a bigger open source App projects, including map, ForceTouch, and some made their own wheels (picture request Blur effect of the wheels, prompt box) also welcome you to our website CodeInventor (under construction), brick…

Recommend official documents for study:

The Swift Programming Language (Swift 3.1)