Swift uses global constants instead of macros

Source: Internet
Author: User

preprocessing Directives

The Swift compiler does not contain pre-processors. Instead, it takes advantage of compile-time properties to generate configuration 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 a # define directive that defines a basic constant in swift, which you can use instead of global constants. For example: A global definition of # define Fade_animation_duration 0.35, in which Swift can use let Fade_animation_duration = 0.35来 a better representation. 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

Complex macros used in C and objective-c do not have replicas in swift. Complex macros are macros that are not used to define constants, including function-style macros with parentheses. You use complex macros in C and objective-c to avoid the restriction of type checking, or to avoid retyping a large number of boilerplate code. However, macros can also cause bugs and refactoring difficulties. In Swift you can use functions and generics to achieve the same effect without any compromises. Therefore, complex macros defined in the C and Objective-c source files are not available in Swift.

compiling the configuration

Swift code and C, objective-c code are conditionally edited in different ways. Swift code can be conditionally compiled based on the evaluation of the build configuration. The build configuration includes true and false literals, command-line flags, and platform test functions in the following table. You can use-d <#Flag#> to specify command-line flags.

Related Article

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.