Objective-C magic path [14-basic C language characteristics], objective-c14-
Master haomeng is devoted to his contribution and respects the author's Labor achievements. Do not repost them.
If the article is helpful to you, you are welcome to donate to the author, support haomeng master, the amount of donation is free, focusing on your mind ^_^
I want to donate: Click to donate
Cocos2d-X source code download: point I send
Here we will introduce some features that you may not need to know when writing Objective-C Programs.
In fact, most of these are derived from the associated basic C language features,
Although C language is a procedural language, some of its features are opposite to the idea of object-oriented programming,
However, language is an ideology and a tool. Finding the associations and differences between the two will give you a deeper understanding of Objective-C.
Array:
Defines a group of ordered data items, that is, arrays.
If an ending null character ('\ 0') is added at the end of the character array, a variable commonly called a string is generated.
Like other variable types, the array must be declared before use,
The declaration of an array involves declaring the numerical type of the elements contained in the array and the maximum number of elements that will be stored in the array.
Function:
If the automatic reference count (ARC) is used, the local object variables will be empty by default each time a function (method) is called.
Variables defined in functions (the same as in methods) are called automatic local variables.
Because each time the function is called, they are automatically "created" and their values are local for the function.
Functions and Methods: a function is a non-object process call. A method is a process call of an object.
The default return type of a function is different from that of a method by default.
If the return type is not specified for the method, the compiler will assume that it returns the value of the id type.
Similarly, the return type should be declared for the method. Do not rely on this fact.
The default return value of a function is an integer value.
When a function or method is called, the value passed as a parameter is copied to the corresponding parameter.
When an array is used, the content of the entire array is not copied to the array of form parameters,
Instead, a pointer is passed to indicate the computer memory address where the array is located.
Therefore, all changes made to the form parameter array are actually performed on the original array rather than the copy of the array.
Therefore, these changes remain valid when the function or method returns.
Block Blocks:
Block is an extension of C language.
It is not defined as part of standard ansi c, but added to the language by APPLE. Block
It looks more like a function.
In general, blocks cannot change external variables unless the _ block int num = 0; variable is used.
One of the advantages of the block is that the system can be allocated to other processors or other threads of the application for execution.
In short: thread security.
It is not recommended. It is not a standard. It is inconvenient to transplant the program.
Pointer:
The important features in C language are mandatory for interviews and mandatory for work.
Pointers can efficiently represent complex data structures and change the values passed to functions and methods as parameters,
And can process arrays more accurately and efficiently.
String constant:
"This is char." is a C-style string. They are not objects.
@ "This is char." This is an object-oriented String object.
The while LOOP uses the fact that the null character is equal to the value 0, which is often used by skilled Objective-C programmers.
Char * from;
Char *;
While (* from)
* To ++ = * from ++;
* To = '\ 0 ';
Function pointer:
1) A common application is to pass parameters to other functions.
2) A common application is to create a dispatch table. That is, the function list.
The above C language features should remember a basic principle: they are not objects.
This means you cannot deliver messages to them, or use them to obtain the maximum advantage of memory allocation policies provided by the Foundation framework.
Other language features:
Literal:
(Struct data) {. month = 7,. day = 2,. year = 2011}
Goto statement:
A lazy programmer will often abuse the goto statement. The execution of the goto statement leads to a direct branch reaching a specific point in the program.
If you are not lazy, we recommend that you do not use this statement frequently.
Empty statement:
Do not perform any operations.
Comma OPERATOR:
The underlying operators in the priority list,
In Objective-C, all operators generate a value,
Therefore, the value of the comma operator is the rightmost expression value.
Sizeof OPERATOR:
Objective-the sizeof operator is provided in C language,
It can be used to determine the data type or object size. The returned value is the byte size of the specified item.
Working principle:
Four facts about the relationship between Objective-C and C.
1) instance variables are stored in the structure.
When defining a class and its instance variables, these instance variables are actually stored in a structure.
This shows how to process objects. objects are actually structured and the Members in the structure are instance variables.
Therefore, the inherited instance variables and the variables you add in the class constitute a structure.
When alloc is used to allocate new objects, the system reserves enough space to store these structures.
One of the inherited members in the structure (obtained from the root object) is the protection member named isa, which determines the class to which the object belongs.
2) The object variable is actually a pointer.
When creating a new instance, you are allocating storage space (that is, the storage space of the structure) for the new instance of the object, then using the structure pointer, and storing the pointer variable in it.
3) The method is a function, while the message expression is a function call.
The method is actually a function. When a method is called, the function related to the receiver class is called.
Parameters of the parameter receiver (self) and method passed to the function.
The Objective-C compiler uses a combination of class names and method names to generate a unique name for each function.
4) The id type is a common pointer type.
The method to return the id type value only returns the pointer to an object in memory.
In C, it is void *.
After reading these four facts, you will find that these facts only reveal how the bottom layer of Objective-C object-oriented is implemented.
Basically, all object-oriented objects are implemented using a process-oriented extension, and the same applies to C ++.
That is to say, the underlying Implementation of the object is completed by the process.
In short:
All objects are a combination of structure struct and pointer.
We can see that many object-oriented languages are converted to the C language structure and pointer during compilation and are compiled in C language.
It indicates that the underlying implementation of object-oriented is similar.
Therefore, there is no major change in technical implementation.
But in terms of concept or ideology, this is huge.
In addition, the final implementation of explain execution languages such as perl and python is completed in C language.
Language is a tool, but it also contains a philosophy or an expression of ideas.
If you forget the syntax or details, you can check them again and understand your thoughts so that you can understand the language.