How Swift uses the features of the macro in OC

Source: Internet
Author: User

There is no concept of macros in Swift, so how can we use the macro function in Swift to improve efficiency?

One, using the keyword let to declare a constant to store the corresponding value, the following code declares a constant MyColor to store a specified color

Let MyColor = Uicolor (red:0.2, green:0.7, blue:0.1, alpha:1.0)

Judging the current system version

Let IOS9 = (Uidevice.currentdevice (). Systemversion as NSString). Doublevalue

Using MyColor, note that when used, add self before the constant name.

Vc1.view.backgroundColor = MyColor

But how do you get a random color?

Second, since Swift can use point syntax to invoke functions, it is very convenient to use the method of declaring functions to define "macros"

Each call to the following method returns a random color

Func randomcolor ()->uicolor? {        return Uicolor (Red:cgfloat ((Double (arc4random ())%)/255.0), Green:cgfloat ((Double (arc4random ())% 256)/255 .0), Blue:cgfloat ((Double (arc4random ())%)/255.0), alpha:1.0)    }

Use the "macro"

Vc2.view.backgroundColor = Self.randomcolor ()

Note If you declare a constant with let to store a random color, only the color changes each time you start the program, and when the program starts, the constant stores a fixed color.

How Swift uses the features of the macro in OC

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.