IOS Swift constants && Macro definitions

Source: Internet
Author: User

Global constants

Global constants defined in the C and Objective-c language source files are automatically introduced by Swift compilation and are global constants of Swift.

preprocessing directives

The Swift compiler does not contain pre-processors. Instead, it takes advantage of compile-time properties, build configurations, and language features to accomplish the same functionality. Therefore, Swift does not introduce pre-processing instructions.

Simple macro

In C and objective-c, you typically use the # define directive to define a simple constant, and in swift you can use global constants instead. For example: Define a constant of # define Fade_animation_duration 0.35, which is better if you use let Fade_animation_duration = 0.35来 in Swift. Because simple macros that define constants are directly mapped to Swift Global, the Swift compiler automatically introduces simple macros defined in C or objective-c source files.

Complex macros

The complex macros used in C and objective-c do not correspond to anything in Swift. Complex macros are macros that are not used to define constants, and function-type macros that contain parentheses. You use complex macros in C and objective-c to avoid the limitations of type checking or to avoid retyping a large number of boilerplate code. However, macros can also cause debug and refactoring to be more difficult. In Swift you can use functions and generics to achieve the same effect, without any compromise of grievance. Therefore, complex macros defined in the C and Objective-c source files are not available in Swift.

Examples of Use:

  globalconst. swift//  liqunswiftdemo////  Created by HF on 15/11/3.//  copyright©2015 year HF-ZHJH. All rights reserved.//import foundationimport Uikitlet webapibaseurl = "http://baidu.com"   //simple macro as global constant let color_th Eme_green:uicolor = GetColor (1)//complex macros with functions This example is too weak to  be supplemented later there is a generic knowledge of the Func-GetColor (Mycolor:int), uicolor{    Return Uicolor (colorliteralred:0.08, green:0.64, blue:0.0, alpha:1)}/////////////////////can be called directly in place of use

IOS Swift constants && Macro definitions

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.