Article source: my.oschina.net/luoguankun/…

Scope of a member variable

@ public,

· Member variables of objects can be directly accessed anywhere

@ private,

· Can only be accessed in object methods of the current class

· @protected (optional, default)

· Directly accessible in object methods of the current class and subclasses

@ package,

· As long as you are in the same framework, you can directly access the object’s member variables

· @interface and @implementation cannot declare member variables with the same name

· Declare variables in @implementation. Default is @private

Second, point grammar

· The essence of dot syntax is still method calls, not calling member variables

· When the compiler encounters dot syntax, it turns the dot syntax into a method

?

123 p.age = 10;         //[p setAge:10]; intage = p.age;    //[p age];

3. Construction method

· The method used to initialize an object, object method, begins with a minus sign

· Member variables have fixed values in order for the object to be created

· Overriding constructor notes:

· Call the parent constructor first ([super init])

· Initialize the internal member variables of the subclass

· + alloc method

· Allocate storage space

· -init method

Initialization,

· This is how objects are actually created

?

1 Person *p5 = [[Person alloc] init]; // Allocate storage space before initialization

· Rewrite NSObject init method (no arguments)

?

1234567891011121314151617 If (self = [super init]) {// Init succeeded _age = 10; } returnself; }

· Construction method with parameters

?

123456789101112131415 – (id)initWithName:(NSString *)name andAge:(int)age {     if(self = [super init]) {         _age = age;         _name = name;     }     returnself; }

Four, classification,

· Classification, which can extend some methods to a class without changing the original model

· Classification can only add methods, not member variables

· Member variables declared in the original class can be accessed in the classification method implementation

· The classification has the highest priority, and the method should be found in the classification first, then in the original class, and finally in the parent class

· Classification can rewrite the methods in the original class, but it will overwrite them, which will make the original method invalid and no longer usable

· Priority of method calls: classification (priority of the last class involved in compilation) –> original class –> parent class

· The application of classification is generally the method of extending the system’s classes, such as the method of extending NSString class

?

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 + (int)numberCountOfString:(NSString *)str { intcount = 0; for(unsigned longi = 0; i < [str length]; i++) { unichar ch = [str characterAtIndex:i]; if(ch >= ‘0’&& ch <=’9′) { count++; } } returncount; //return [str numberCount]; } // Count the number of Arabic digits in a string (object method) – (int)numberCount {intCount = 0; for(unsigned i = 0 ; i < [self length]; i++) { unichar ch = [self characterAtIndex:i]; if(ch >= ‘0’&& ch <=’9′) { count++; } } returncount; //return [NSString numberCountOfString:self]; }

5. The nature of classes

· You can use a Class to create an object, but the Class itself is also an object, an object of type Class

· First use Class to create a Person Class object, then use the Person Class object to create an object of type Person

· Override the load() and initialize() methods

?

1234567891011 + (void)load{NSLog(@”Student’s load is called “); + (void)initialize{NSLog(@”Student-initialize method is called “); }

· Load () method:

· When the program starts, all classes and categories in the project are loaded, and the load method for each class and category is called after loading. It will only be called once

· When a class is used for the first time, the + Initialize method of the current class is called

· Load the parent class first, then load the child class (call the load method of the parent class first, then call the load method of the child class)

· Initialize the parent class first, and then the child class (call the initialize method of the parent class first, and then call the initialize method of the child class

?

1234567891011121314151617181920 Person *p = [[Person alloc] init]; Person *p2 = [[Person alloc] init]; Person *p3 = [[Person alloc] init]; Class c = [p Class]; Class c = [p Class]; Class c2 = [p class]; Class c3 = [Person Class]; NSLog(@”c=%p,c2 =%p,c3 =%p”,c,c2,c3); // The load method is called whenever the class is loaded. // The initialize method is called when the class is initialized for the first time. GoodStudent *stu = [[GoodStudent alloc] init]; GoodStudent *stu = [[GoodStudent alloc] init]; GoodStudent *stu = [GoodStudent alloc] init];

Automatically generate getters and setters

· @Property automatically generates a declaration of a member variable

· @synthesize automatically generates implementations of getters and setters, and accesses specified member variables

?

1234567891011121314 // The compiler automatically converts @property to setter and getter declarations // – (void)setAge (int)age; // – (int)age; @propertyintage; // Generate the _age member variable @propertydoubleheight; // Generate the _height member variable @propertydoubleWeight; // generate the _weight member variable @property NSString *name; //@synthesize automatically generates getter and setter implementations, and accesses the specified member variable @synthesize age = _age; @synthesize height = _height; // synthesize @weight = _weight,name = _name;

· @Synthesize can declare and implement getter and setter methods of member variables without writing @property

7, Description method

?

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 H >#import “person.h” intmain(){NSLog(@”%d”,__LINE__); NSLog(@”%s”,__FILE__); NSLog(@”%s”,__FILE__); Printf (“%s\n”,__FILE__); // Output the current function name NSLog(@”%s”,__func__); return0; } voidtest(){ Person *p = [[Person alloc] init]; p.age = 20; p.name = @”Jack”; //1. The object’s -description method is called first, returning the string //2. Get the return value of the -description method, // By default, the description method returns “class name + memory address” <Person 0x0232010> // So you need to override the description method // the equivalent of Java Object’s toString() method NSLog(@”%@”,p.description); Person *p2 = [[Person alloc] init]; p2.age = 22; p2.name = @”Ted”; NSLog(@”%@”,p2.description); } voidtest2(){ Class c = [Person class]; //1. The class’s +description method //2 is called first. Get the return value of the +description method (NSString *) and display it on the screen NSLog(@”%@”,c); } //NSLog(@”%@”,self); return[NSString stringWithFormat:@”age=%d,name=%@”,_age,_name]; } + (NSString *)description{ return@”sss”; }

8. Id type

· ID is a type

· ID is a universal pointer that can operate on any OC object. The id already contains *, so there is no need to add *

Example:

?

123456789101112131415161718192021 Intmain (){// intmain(){// intmain(){// intmain(){// intmain(){// intmain(); //Person *p = [Person new]; //id == NSObject* //NSObject *o = [Person new]; Person = [person new]; [person setAge:10]; [person setObj:@”luoguankun”]; NSLog(@”age=%d”,[person age]); test(person); return0; }

Nine, SEL

· SEL is actually a kind of packaging of method. The method is packaged into SEL type data, and the corresponding method address can be found to call the method

· Actually the message is SEL

?

12345678910111213141516171819202122232425262728293031323334353637383940414243444546 intmain(){ //[Person test]; Person *p = [[Person alloc] init]; //1. Wrap test2 as SEL data //2. //[p test2]; // Call test with SEL introduction [p performSelector:@selector(test2)]; //SEL s = @selector(test2); //[p performSelector:s]; // call the test3 method with SEL as an argument, don’t forget the colon [p performSelector:@selector(test3:andluo: withObject:@” Luo “withObject:@”feng”]; return0; SELNSString *name = @”test2″; SEL s = NSSelectorFromString(name); (void)test2{//[selformSelector :_cmd]; NSString * STR = NSStringFromSelector(_cmd); NSString * STR = NSStringFromSelector(_cmd); // print _cmd NSLog(@”%@”, STR); / / print test2}

 

\