Original URL: http://tutuge.me/2015/03/11/Effective-Objective-C-%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0-Item-4-%E5%A6%82% e4%bd%95%e6%ad%a3%e7%a1%ae%e5%ae%9a%e4%b9%89%e5%b8%b8%e9%87%8f/
Objective
It takes time to write a blog than to read a book. =, the book is almost finished, only to write to the 4th quarter. But to sum it up, it was really impressive.
This time the theme is " constant ", very common, and very important things ~ Good code specification, design, and a variety of constants .
Item 4-prefer Typed Constants to preprocessor #define
This section focuses on the definition of constants, the difference between type constants and # define, and their respective characteristics. Finally, we should use constants with types, not # define.
A simple example
Application, there are all kinds of animation, but also to set the length of the animation, in general, we will set a short, medium and long several time constants, for different occasions, understand the C language children's shoes estimation of "#define" macro definition is not unfamiliar, so, may be as follows as defined:
1 2 3 |
#define Animation_duration_ Short 0.3 # define Animation_duration_middle 0.6 #< Span class= "com" >define Animation_duration_long 1.0 |
So, all the time used in the animation of the place, directly with the macro definition of the line.
At first glance, there seems to be no problem.
Type + non-volatile
Let's take a look at what the macro definition has done.
In general, #define就是 "Replace", at compile time, replace all macro definitions with what is defined later. (Refer to Baidu Encyclopedia)
The key is:
- The macro definition does not check for the substituted " type ", as long as it is encountered, it is replaced.
- The macro definition does not guarantee " immutable " because it can be defined repeatedly and does not guarantee that the value will not change. (Which I think is the easiest place to overlook)
Therefore, it is "dangerous" to define constants with a macro definition.
The right approach
Appropriate constants should have the appropriate constant name, the correct type, as well as const, extern and other key words of the modification, of course, according to the constant applicable " scope ", to make appropriate processing. For an illustrative example.
Constants that are used only in functions
Constants that are used only in functions:
1 2 3 4 5
|
- (void) { Animation time-length constant static const nstimeinterval Kanimationdurationlong = 1.0f //... } /span> |
Static, const defines that it is statically immutable;nstimeinterval Specifies the type of the constant "exact".
Only in one "
. M"Constants used in the file
It is also common to define constants in the class's implementation file "XXX.M" file.
You might think that since a class is used, it's OK to put it in the header file of the class.
But it's wrong to think so. =
Because all of the files that are used in this class are invisible and contain these constant declarations, exposing unnecessary data to the outer surface, which violates the design principle, and can cause conflicts if the constant name gets inappropriate.
Therefore, it should be as follows:
1 2 3 4 5 6 7 8
|
Xxx.m
//must be static static const nstimeinterval Kanimationdurationlong =< Span class= "PLN" > 1.0f @implementation xxx //... @end |
Points to note:
- Be sure to put it in the. m file.
- Must be static, indicating that this constant scope is only in this. m file. If you do not add static, the constants are declared in the global scope and may cause conflicts with other constants.
- const, of course.
- Nstimeinterval, determines the type.
- Kanimationdurationlong, the "K" begins with the naming, also is the Apple specification, the internal constant unified with "K" begins.
Global constants
In general, the most common global constants are the names of notification that are used to broadcast notification, often strings, as an example of this:
Assuming our class is called "ttgclass", our class will send out notification, so we need to define a notification name constant for the outside world to register, as follows:
1 2 3 4 5
|
TTGClass.h extern nsstring * const Ttgclassworkbeginnotification //ttgclass.m nsstring * const ttgclassworkbeginnotification = @ " Ttgclassworkbeginnotification " |
The essential:
- The header file only makes the declaration, does not make the definition. So use extern to indicate that constants are defined elsewhere, and hide the details as much as possible.
- The same class name is the beginning of the constant name, this is very important, because there is no "package" in the objective-c like Java, so must be named to classify different classes, constants of course to follow this rule.
- NSString, a type of determination, a const, indicating a constant; a notification suffix that indicates use. These are all things to be aware of.
Summarize
Good code, always have to define a variety of constants, to avoid "devil number, devil String." So we should try to avoid defining constants with # define, but instead using const constants with types, and narrowing the scope of the constants as much as possible.
The less detail you expose, the smaller the chance of error (forget where you see it). =
"Turn" effective-objective-c-reading notes-item-4-How to define constants correctly--good