Talk about why IOS development don't use macros to define constants _ios

Source: Internet
Author: User
Tags modifier naming convention

First, preprocessing commands he is not a constant!!!!

Let's look at a piece of code

#define @ "Avatar"
 if (false) {
  #define AVATAR @ "
 NSLog"}
 (Avatar);

How much does this code output, we define "avatar" for 60, and then redefine "Avatar" in a code that never executes, the code in theif statement never executes, but at compile time , the compiler compiles the code, and this time the compiler replaces the avatar name with @ "80", so the final output of this code is 80.

Of course this time the compiler will have a warning, but do not know how many alumni ignore this warning. Or you'll tell me you're very sensitive to warnings, will not let him go, but remember you are not a person in writing code, may be in someone else's page he redefined your define, dug a big hole for you, still can't find ...

Use const to define a constant

Variables defined by the const modifier are immutable, for example, you need to define an animation time constant, you can do this:

static const Nstimeinterval kanimateduration = 0.3;

When you try to modify the value of " kanimateduration", the compiler will make an error. More importantly, the constants defined in this way are of type information, and this is define.

Maybe you've found out, if you define like this:

static const NSString * Kusername = @ "Strongx";

You can modify the value of the userName , (the said constant ~ ~ ~ ~ ~)

The first thing we need to determine is that the following two kinds of writing are the same:

static NSString Const * Kusername = @ "Strongx";
static const NSString * Kusername = @ "Strongx";


This means that the const is the same effect before or after the type. Then the different effects are the following:

Static NSString * Const KUSERNAME = @ "Strongx";

The const modifier is the part of his right hand, which means:

static NSString Const * Kusername = static NSString Const (* kusername)

static NSString * Const KUSERNAME = static NS String * CONST (KUSERNAME)

When the const modifier is (userName), the immutable is the userName; * "In the C language," said
Pointer to a character, that is, this time userName point to the memory block address is immutable, and the contents of memory storage is variable, let's try to:

 NSLog (@ "Memory address:%x",& kusername);
 Kusername = @ "SUPERXLX";
 NSLog (@ "Memory address:%x",& kusername);

The above NSLog prints the memory block address that the *username points to, and his output is:


Output

We have found that when we change the memory of memory, his address does not change, which means that it conforms to the "const" modifier.

And when our modifiers are like this:

Static NSString * Const KUSERNAME = @ "Strongx";

We can't change the value of username.

So when we need to define an immutable constant, we still need to place the "const" modifier behind the "*" pointer.

Be sure to use both static and const to define your variables.

It has been said that const is used to define a constant. static in C language (in OC) indicates that this variable is available only in the output file of the change amount (. m file), and if you do not add the "static" symbol, the compiler creates an "external symbol" for the variable. What are the consequences?
You can try to add the following code to the different compilation files:

NSString * Const KUSERNAME = @ "Strongx";

There may not be a problem with duplicate property names, although there is no reference to each other (because this is not an attribute, this is an external symbol), but the compiler still complains:

He's going to tell you. There is a duplicate symbol in the two target files (. 0 files are compiled output files of. m files). (OC does not have the concept of namespaces similar to C + +)

So when you need to declare a variable in your own. m file that is a local variable (k) that is only visible to you, be sure to use both "static" and "Const" two symbols.

Define global variables in the project

Many global constants will be defined in our project, and many people will create a "constant.h" file in which many constants are declared with #define , and then the header file is introduced into the PCH file. It is not possible to say that this is wrong, but as mentioned above, define may be modified, of course, in the case of a naming convention, this rarely happens, and it is highly efficient.

However, Apple recommends another approach : "extern", which has the advantage of keeping the constants from being modified, and certainly initializing them with type information.

We declare constants in the "constants.h" file:

extern nsstring *const Xusername;

Then define him in "CONSTANTS.M" :

NSString *const xusername = @ "Strongx";

Constants that are defined with "extern" must also be initialized only once, not necessarily and only once, so the compiler will remind you. You need to be aware of your naming when defining global variables, and you can use a well-defined prefix to name them.

"define" and "extern" each have their own advantages, but I personally still recommend the use of "extern". Because before in a project was define pit miserably! )。

The above is the entire content of this article, I hope this article mentioned knowledge points for everyone to develop iOS help, so that everyone can use the correct specifications to develop iOS.

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.