From the C-language variable declaration to the block syntax in OBJECTIVE-C]

Source: Internet
Author: User
Tags arithmetic operators modifiers



Original: From C declarators to Objective-c Blocks Syntax



Nils Hayat



Translator: Cocoachina--sunshine



In this article, we start with various declarations in the simple C language, as well as complex declaration combinations to the syntax of the code block BOKCK in the last objective-c.



Take some time to understand the code block (block) derivation and organization, once you understand these, it is convenient to declare and use it, and do not need to go to Google every time.



If you want to show what you can think of with a block statement, read on!



Declarators (specifier)



In C language, variables are declared by specifiers.



A specifier has two functions:


    1. Define the type of the variable;

    2. Define a name for the variable so that it is used in its scope;


Let's start with one of the most basic specifiers:?






It's like the first line of C code you've ever written.



int is the basic variable type, and a is the name (or identifier) of the variable.



When you start looking at a specifier, you should start with the variable name (specifier), look at the part to the right of the variable name, and look at the section to the left of the variable name (the next section explains why).



int A; There is nothing in the right part of the variable name A, so it directly illustrates that a is a variable of type int.



The declaration of a variable has only one base type, which is the leftmost part of the specifier.



Specifiers can derive new types by modifying the basic data types with modifiers. The modifier is the following four kinds of symbols:*,[],(),^.



Pointer Modifier (*)






The type is still int, and the variable name is a. But the pointer modifier (*) means that A is a pointer to the value of an int, which is not itself a value of int.



The pointer modifier (*) is always on the left side of the variable.



Array modifier ([])






The above array modifier ([]) indicates that a is a set of int, not a simple int value. When declaring, add the length of the array, for example, int a[10];



The array modifier ([]) is always on the right side of the variable.



function modifier (())









The function modifier means: f is a return int worthwhile function. The function modifier can carry arguments, such as int f (long), meaning that f is a function that carries a long parameter and returns an int type.



The function modifier () is always on the right side of the variable.



Combination of modifiers



pointer modifier (*) and array modifier ([])



Combination



Modifiers can be grouped together to produce complex variable types. Just like arithmetic operators, arithmetic operators can be grouped together, and then the actions performed first (with the priority of * and/are higher than +/-), and the modifiers are the same. modifiers [] and () have precedence over whether the show is * and ^. because [] and () have high precedence, they are placed on the right side of the variable, so when encountering a complex specifier, you should start with the variable name (the identifier) and look at the right part, looking at the left part.



For example






Or, to improve reading, use parentheses to expand the variable name and the right part.






Are all representations: A is an array of pointers, and each element in the array points to a value of type int.



Someone might ask, how can you declare a pointer to a set of int worth? The following declaration methods can be implemented:






The pointer modifier has a lower precedence than the array modifier, so it is placed in parentheses to increase its precedence. This means: A is a pointer to a set of int worth.



Array modifier ([]) and function modifier () combination



You cannot declare an array, each element in the array is a function, and you cannot declare a function that returns an array or a function. But the argument to a function can be an array.



For example?






This means that f is a function with an argument that is an array of length 10 (each element in the array is a value of type int), and the function returns an int type value.



Pointer modifier (*) and function modifier () combination






The above two declarations all represent: F is a function that returns a pointer to a value of type int.



If you ever want a pointer, point to a function, what do you do?






The above declaration indicates that F is a function pointer that points to a function that returns an int type worth.



code block (also called closure) modifier (^)



Apple introduced the modifier in its proposed extension of the ANSI-C standard. is called the code block pointer modifier (closure modifier). This modifier is similar to pointing to pointer modifiers. Declaring a block of code is the same as declaring a pointer, pointing to a function.



The modifier can only be used for functions, so int ^a; This explains why an int ^b () is illegal and causes a compiler error. If you look at the specifier as described above in the reading specifier, B is a function, and the return value is a code block pointer to a value of type int. This is why you want to put ^b in parentheses.






B means: A block of code that points to a function that returns an int type value, or a code block that returns a value of type int.



You can also carry parameters when defining code functions, such as






Represents: a code block has a long parameter, a return value, and an int type.



This is the origin of the code block Declaration.



In order to use code blocks in Objective-c, there are some other syntax that you need to remember. 1. Define the syntax of the code block, 2. How to pass a block of code to a Objective-c method.



abstract specifier



An abstract descriptor consists of 2 parts: An abstract specifier, a variable name.



Abstract specifiers have 3 usage scenarios in standard C language:



1.int *a;long *b = (long *) A; (long *) is an abstract specifier: Represents a pointer to a long type.



2. As a parameter to sizeof (): malloc (sizeof (long));



3. As the parameter type of the function: int f (long *);



Objective-c uses the abstract specifier as the parameter of the method or the return value of the method;


1 -?(long?**)methodWithArgument:(int?*)a;



Here (long *) and (int *) are abstract specifiers.



So in order to be able to use blocks of code as arguments or return values in the Objective-c method, we need to find the code's fast abstract specifiers. can be obtained by removing the variable name from the descriptor;



For example:



Int (^b) () removes the variable name B to get the abstract specifier int (^) () and int (^b) (long) to remove the variable name B to get the abstract descriptor int (^) (long).



Example:


-?(void)methodWithArgument:(int(^)())block;
-?(void)anotherMethodWithArgument:(void(^)(long?arg1))block;





The name of the argument in the abstract descriptor can be omitted. Because XOCDE will automatically help you to complete these.


Block literal



When you write int a = 2, int A is a specifier, and 2 is the value of a, also known as implementation.



The caret (^) is also used as a unary operator to convert a function implementation into a block of code. You can automatically infer from the return statement that you don't have to specify the return value of the code block.



Because it is the implementation part of the code block, you need to define the name of the parameter.



For example: Int (^block) (long, long), the implementation is as follows


block?=?^(long?a,?long?b)?{
??int?c?=?a?+?b;
??return?c;
}


Summarize


It looks complicated, and the block syntax in OBJECTIVE-C is built on standard C syntax. The block in the objective-c is like a pointer to a function. Once you understand this, add a little touch and you'll find that block is easy to master.



From the C-language variable declaration to the block syntax in OBJECTIVE-C]


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.