IOS Dev (16) is one of the sections on some basic OC knowledge points.
- Author: CSDN laruence
- Blog: self in static methods of http://blog.csdn.net/prevention1
Use self and self to represent this class in static methods (class methods. For example, the following two methods mean the same:
+ (void) wtf { [self alloc]; } + (void) wtf { [ClassName alloc]; }
The true meaningAn object in OC is followed by a bunch of things after ".", indicating the get method or the set method, rather than the member variable. To get or set, it depends on whether the application scenario requires the return value or the void that does not need the return value. So there are:
[[UIViewController alloc] init]; you can actually write it as UIViewController. alloc. init;
3. Private MethodThe methods implemented by OC in. m, but not in. h, are private methods.
4. Default scope of member variablesLike C ++, there are three scopes:
- Public
- Protected
- Private
The member variables declared by OC in. h are protected by default. For example:
@interface ClassName: NSObject{ int _age; int _sex;}@end
The above age and sex are both protected, that is, they can be accessed in the class and subclass.
4. How do I specify the scope of member variables?Go directly to the Code:
@interface ClassName: NSObject{ @public int _age; @private int _sex; }@end
5. formal writing of get and set methods@interface ClassName: NSObject{ int _age; int _sex;}- (int) age;- (void) setAge:(int)age;@end
6. Write a constructor.There are two writing methods. Note that there is * In the first method, which indicates a pointer.
- (ClassName *)initWithArg:(int)arg{}
You can also use the id ~
- (id)initWithArg:(int)arg{}
7. Call the constructor of the parent class after inheritance- (id)initWithArg:(int)arg{ if (self = [super init]) { _arg = arg; } return self;}
8 simple Syntax of [[Blabla alloc] init]ClassName * cn = [[ClassName alloc] init]; ClassName * bn = [ClassName new]; // not recommended
-
Reprinted please indicate from: http://blog.csdn.net/prevention