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