Summary of Const,static,extern usage in IOS development

Source: Internet
Author: User

Const

const简介: Commonly used string constants, usually pumping macro, but Apple does not recommend us to smoke macro, we recommend using const constants.

    • 编译时刻: Macros are precompiled (processed before compilation), and const is the compile phase.
    • 编译检查: Macros do not check, do not report compilation errors, just replace, const will compile the check, will report a compilation error.
    • 宏的好处: macros can define some functions, methods. Const cannot.
    • 宏的坏处: With a large number of macros, it is easy to compile for a long time and need to be replaced each time.

Macros define constants, constants are placed in constant areas, and only one copy of memory is generated.

Const: Const is a left-associative type modifier that, along with its left type modifier, is a type modifier that indicates that the type is read-only.

简言之:沿着*号划一条线,如果const位于*的左侧,则const就是用来修饰指针所指向的变量,即指针指向为常量;如果const位于*的右侧,const就是修饰指针本身,即指针本身是常量。

static const NSString *PERSONA1 = @ "A1";//cannot provide outside access after being modified with static

The *PERSONA1 is constant and cannot be modified, but the personA1 is mutable and can point to the new memory address, and when PersonA1 is assigned, personA1 points to the new memory, and the value of the read is changed;

static NSString *const personA2 = @ "A2";//string constant personA2 cannot be re-assigned;

Https://www.jianshu.com/p/2fd58ed2cf55

https://jatstar.cn/2016/01/10/const-static-extern/

Https://www.jianshu.com/p/b86278d322f5

Summary of Const,static,extern usage in IOS development

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.