Use of IOS const

Source: Internet
Author: User

1. Const Classic Face question

1> const int age1 = 21;

Age1 = 100; //Compile error

2> int const AGE2 = 22;

3> const int *age3 = 23;

int val1 = 101;

*age3 = Val1; //Compile error

Age3 = &val1; //Compile through

4> int const *AGE4 = 24;

5> int * Const AGE5 = 25;

int val2 = 102;

Age5 = &val2; //Compile error

*age5 = Val2; //Compile through

Explain:

1> Const on the front can be moved backward one, meaning unchanged, int const AGE1 =, const modifier age1, meaning Age1 cannot be modified

2> with 1

3> and 1, move backwards, meaning unchanged, int const *AGE3 =, const modifier *AGE3,

First Age3 is a pointer variable, *AGE3 is the variable that the pointer points to, the Const modifier *age3, which means that the *age3 is the variable that the pointer points to is not modifiable

4> with 3

5> Age5 is a pointer variable, the const modifier pointer variable, means that the pointer variable is not modifiable, and the pointer variable is the address, meaning that this pointer variable can not be stored in the address to another address . Or, you can't change the pointer's direction.

2. Projects, often have some fixed values, may be used in multiple places, this time can be considered with a const

such as the following:

NSString * Const MYURL = @ "http://xxx.xxx.xxx.xxx:8090";

In fact, you can also use macros, such as the following:

#define MYURL @ "http://xxx.xxx.xxx.xxx:8090"

But with a const more efficient than macro, such as the following print multiple times, if the macro, essentially at compile time to do the replacement, will occupy 3 parts of memory, if the const will not waste memory, is always 1 parts of memory

NSLog (@ "The address of the remote connection is%@", myurl);

NSLog (@ "The address of the remote connection is%@", myurl);

NSLog (@ "The address of the remote connection is%@", myurl);

3. Where the use of const must be noted

If multiple. m files need to be used for Myurl, can you initialize each. m file as follows?

NSString * Const MYURL = @ "http://xxx.xxx.xxx.xxx:8090";

Certainly not, compile, will report duplicate definition. For example, writing the above line of code in a PCH file is equivalent to writing this line for every. m file.

To solve the problem of duplicate definitions, you can refer to Apple's practices, such as notification of system presets

    Uikit_extern nsstring *const uikeyboardwillshownotification;

Uikit_extern nsstring *const uikeyboarddidshownotification;

Uikit_extern nsstring *const uikeyboardwillhidenotification;

Uikit_extern nsstring *const uikeyboarddidhidenotification; //Uikit_extern, is treated EXTERN

    

(PS: In fact, you can also modify the global variable with static, indicating that the global variable is only visible to the current file, but it is not recommended to do so, each. m file does so, and more waste of memory)

Specific solutions:

1> Custom Class Myconst, inherited from NSObject

2> in the myconst.m file

#import <Foundation/Foundation.h>

NSString * Const MYURL = @ "http://xxx.xxx.xxx.xxx:8090";

3> in the MYConst.h file

#import <Foundation/Foundation.h>

extern NSString * Const Myurl; //extern, external meaning, indicating this global variable, not own, is someone else's, please find this global variable in other files

4> #import "MYConst.h" in the PCH file so that each. m file in the project has an extern nsstring * Const Myurl;

      

Use of IOS const

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.