Objective-C basic syntax review notes

Source: Internet
Author: User

After reviewing the basic syntax of Objective-c, each review will have a new mind. The following are the summary points:

1. If the allocated memory is not effectively recycled, memory leakage may occur.

2. Determine whether the memory of a variable is allocated to the heap memory or the stack memory. It mainly depends on whether it is a pointer type.

3. When declaring a local variable, the memory of the variable is usually allocated and released every time it enters the scope of the local variable. This type of storage is called automatic storage or modified by the keyword auto

4. The static keyword ixushide is used to allocate memory when the variable is being generated. Therefore, the memory is allocated only once during the running of the program. when accessing the variable each time, the original memory is actually accessed.

1 void someFunction () {2 // no matter how many times you call this function, the person variable will only be created and initialized once. 3 static Person * person = [[Person alloc] init]; 4}

5. The global variable is in the global scope by default. Its behavior is the same as that of the static variable. The memory is allocated only once and its value remains unchanged throughout the program running.

If the global variable uses static modification, it can only be used inside the file that declares it; if there is no static modification, it can be accessed anywhere

6. Structure

Declare a struct

1 struct Point{2   int x;3   int y;    4 }

Lifecycle structure example

1 struct Point p;

Composite Structure

1 struct Line2 {3   struct Point start;4   struct Point end;    5 }

Use struct

1 struct Point p;2 p.x = 20;3 p.y = 50;

Applicable Type Definition

1 typedef struct2 {3     float x;4     float y;5 } Point;6 7 Point p;8 p.x = 100.0f;9 p.y = 200.0f;

8. Use enum

Create Enumeration type

1 enum MyEnum{2   Value1,3   Value2,4   Value3  5 };

Use Enumeration

1 // declaration and value assignment 2 Enum myenum Foo; 3 Foo = value1; 4 5 // function return value 6 Enum myenum myfuncation (); 7 8 // as function parameter 9 void myfunction (Enum myenum Foo); 10 11 // type definition 12 typedef myenum Foo; 13 14 // value 15 Enum myenum {16 value1 = 20, 17 value2 = 121, 18 value3 = 19}

9. A pointer is a variable that contains the address of another variable.

Pointer operator (*), get address operator (&)

1 int x = 5; 2 int * Y = & X; 3 // print the address of x 4 nslog (@ "X: % lD", y ); 5 // print the value of X 6 nslog (@ "x = % lD", * y );

Pointers can be computed with conventional variables, and new address pointers can be taken.

The typical usage of pointers in OC is mainly to declare objects. Objects in OC are actually pointers.

10. The parameters and return values of functions in OC are transmitted by "passing values". That is to say, when you pass a parameter to a function, A copy of the input value is actually created during the runtime. This means that changing these values within the function will not affect the value in the call function. However, if a pointer is passed, any changes to the pointer variable will affect the original variable in the call function. At the same time, be sure not to return a pointer or reference to a variable that has exceeded the scope when the function was launched.

Example:

1 # import <Foundationi/Foundation. h> 2 3 void myFunc (int a, int * B) {4 a = 20; 5 * B = 20; // reverse reference pointer to access Original Value 6} 7 8 int main (int argc, const char * argv []) {9 int a = 10; 10 int B = 10; 11 myFunc (a, & B); 12 13 NSLog (@ "a = % d, B = % d", a, B); 14 15 return 0; 16}

11. You can declare a function in. h to implement the function in. m (I didn't know it before)

 1 #import <Foundation/Foundation.h> 2  3 @interface Person : NSObject 4  5 extern long int calculateFactorial(int value); 6  7 @end 8 ----------------------------------------- 9 #import "Person.h"10 11 12 @implementation Person13 extern long int calculateFactorial(int value){14     return value * value;15 }16 @end17 -----------------------------------------18 #import <Foundation/Foundation.h>19 #import "Person.h"20 21 int main(int argc, const char * argv[])22 {23 24     @autoreleasepool {25         printf("========== here ---------------%ld",calculateFactorial(100));26     }27     return 0;28 }

12. The difference between <> and "" in the import Statement is: if it is a system or a third party, use <>. If it is created by yourself, use ""

13. In OC, the control variable of the case clause in switch-case can only be an integer.

14. OC supports two types of for loops:

1     for (<#initialization#>; <#condition#>; <#increment#>) {2         <#statements#>3     }4 5     for (<#type *object#> in <#collection#>) {6         <#statements#>7     }

 

 

 

 

 

 

 

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.