Tips for using enumeration as constants-php Tutorial

Source: Internet
Author: User
Enumeration is a small technique used by constants for Swift. when GG translated the article, he followed the link in the article and learned two more articles. now he has some experience in translating so many articles. He also summarized a complete set of theories, I will talk about it later.

This article is very simple. I will not translate it one sentence at a time.

As we all know, the enumeration type in Swift is much more powerful than OC. Today we will introduce a small technique for using enumeration as constants.

Add enumeration for your Image Assets

Previously, you used UIImage (named: "FooBar") to create images. remember a series of "foobar", "FooBar", "foo_bar", "foo-bar" image names, be careful not to spell it wrong. after all, there is no automatic completion.

Of course, you can also define a constant let FooBarImageName = "FooBar" to avoid spelling errors, but it is cool to define a global constant, even if they are all organized in a global static struct, they are not so elegant.

Next, let's take a look at how to solve the problem with Swift enum (enum can includeOriginal type string)

enum ImageAsset : String {    GreenApple = "green-apple"  Banana = "banana"  Pear = "pear"  Strawberry = "1-strawberry"  Strawberries = "strawberry-basket"}
Add constructor

Now that we need an image, add a computing attribute.

extension ImageAsset {    var image : UIImage {    return UIImage(named: self.rawValue)!  }}

Now, you can call ImageAsset. GreenApple. image to obtain a UIImage. it is not spelled incorrectly and provides automatic completion.

Since the image is created, we have transformed the UIImage together and added a constructor.

extension UIImage {    convenience init(asset: ImageAsset) {    self.init(named: asset.rawValue)!  }}

Now you can get the image through UIImage (asset:. GreenApple ).

Add enumeration for your UIColors

The essence of UIColors can actually represent the hexadecimal number (ffcc00ff) of RGBA, so we use the UInt32 enumeration type to represent the color:

extension UIColor {    enum ColorName : UInt32 {    case Translucent = 0xffffffcc    case ArticleBody = 0x339666ff    case Cyan = 0xff66ccff    case ArticleTitle = 0x33fe66ff  }}

Now we can use the following enumeration to build a UIColor instance, add an initialization, and perform point operations:

extension UIColor {    convenience init(named name: ColorName) {    let rgbaValue = name.rawValue    let red   = CGFloat((rgbaValue >> 24) & 0xff) / 255.0    let green = CGFloat((rgbaValue >> 16) & 0xff) / 255.0    let blue  = CGFloat((rgbaValue >>  8) & 0xff) / 255.0    let alpha = CGFloat((rgbaValue      ) & 0xff) / 255.0    self.init(red: red, green: green, blue: blue, alpha: alpha)  }}UIColor(named: .ArticleBody)  
Enum constants are used everywhere

Please read the enum algorithm with me!

Localizable. strings

Create an enum and add convenience init in String extension. then you can use String (key:. Greetings) to replace NSLocalizedString ("Greetings", comment.

UIStoryboard

Sometimes you need to create UIStoryboard instances and UIViewControllers. to avoid using global constants (storyboardIDs/identifiers), you can use enum. after the transformation, the call is very elegant:

let wizzardVC = UIStoryboard.Wizzard.initialViewController()  let msgCompVC = UIStoryboard.Message.Composer.viewController()  let tutorialVC = UIStoryboard.Tutorial.viewController(.QuickStart)  

Observe that the preceding call, Wizzard, Message, and Tutorial correspond to an enum respectively. You can add multiple enum in the UIStoryboard and assign a case entry for each.

Summary

At last, the author wrote a library "SwiftGen", which will automatically generate this enum for you. you only need to import it in the project.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.