In Objective-c, there are both instance methods and class methods. Class methods are sometimes referred to as factory methods (Factory method) or as convenient methods (convenience method). The appellation of the factory method is obviously different from the factory method in the general sense, in essence, the class method can be executed independently of the object, so in other languages the class method is sometimes called the static method.
Note one: Class method
1, class methods can call class methods.
2, class methods can not invoke instance methods, but class methods can access instance methods by creating objects.
3, class methods are not allowed to use instance variables. The class method can use self because self is not an instance variable.
4, a class method, as a message, can be sent to a class or object (in fact, it is possible to invoke the meaning of a class method through a class or object).
Note Two: The Rule of self
You need to remember the following rules:
1, the self in the instance method is the first address of the object.
2, the self in the class method is class.
Although self is used within the same class, self has a different interpretation. Self in the class method can be translated into class self, and self in the instance method should be translated into object self. Self in the class method and self in the instance method are fundamentally different, even though their names are called self.
Does the object created by the class method not be released with release?
Do not need this object to be placed in the auto-release pool
Private methods and private member variables in OBJECT-C
The member variables are shared internally by default and are private to the outside.
@interface Controller:nsobject
{
@private: NSString *something;
}
+ (void) Thisisastaticmethod;
-(void) Thisisaninstancemethod;
@end
@interface Controller (Private)
-(void) Thisisaprivatemethod;
@end
The following code is how to get a private variable (remember to add a header file #import):
NSString *str;
Mobj *obj = [[Mobj alloc] init];
Object_getinstancevariable (obj, "Mt_", (void *) &STR);
NSLog (@ "%@", str);
[obj release];
=================================== the different class methods and instance methods of iOS instance methods and class methods
1: The instance method is-the class begins with a + instance method that is accessed with an instance object, the class method object is a class rather than an instance, typically creating an object or a tool class.
In the instance method, the message sent to self and super based on the principle of inheritance is actually sent to self
Inside the class method, self is the class method of the other class, and sending a message to self in a class method can only be a class method.
When to use a class method, to create an instance to get a shared instance, or to get some common information about the Class 2:
Class methods and instance methods (instance method). Class methods are scoped to the class and cannot be called by an instance of the class (that is, running out of the instance). Alloc is a class method. Instance methods are scoped to the object instance (that is, it cannot be run until instantiated). Init is an instance method that is called by the object instance returned by the Alloc method.
NSObject * Object1 = [[NSObject alloc] init];
Instance method begins with a minus "-"
Class method begins with a plus "+", which is equivalent to the static method
3:see See more healthy
Objective-c
1.OC is an object-oriented language based on C, which is a superset of C language and features C language.
2.OC definition and implementation of classes and initialization
Declares a class interface that inherits the NSObject object, which is the top-level parent class of all classes in OC, and all classes inherit from it
@interface Classname:nsobject
Declaration of member properties and member functions
+ (void) function;//class method, a method that can be called without instantiating an object
-(void) function2: (NSString *) arg;//member method, which must be called by an instantiated object
@end
Implementation class
@imlementation ClassName
member property initialization and method definition
@end
Initialization of the object: ClassName *obj = [[ClassName alloc] init]
In OC, a message is passed to send a alloc message to the class to allocate memory space, an INIT message is sent to generate the object, and the pointer points to the object itself.
3. Invocation of class methods
[obj function];
NSString *str = [nsstring stringwithstring:@ "Hello"];
[obj Function2:str];
4. Output function
Output different values according to different output formats (%d: Reshape,%@: Object,%s: string)
NSlog (@ "The result is%d", intnum);
CF stands for Core Foundation (COCOA)
Cfshow sends description to the object it displays, Cfshow printed information does not display a timestamp, NSLog is displayed, and cfshow does not need a format string, it can only be used for objects
Cfshow (obj);
5. Properties
Support point notation: MyTableViewCell.textLabel.text = @ "Hello" is equivalent to [[Mytableviewcell Textlabel] settext:@ "Hello"];
Using the Attribute Builder property
Declared in H file: @property int year
Composition generator in M file: @synthesize year
Using Obj.year = 1999 is equivalent to invoking [obj setyear:1999];
You can customize the value method and the assignment method (getter and setter)
-(int) year
{
Return year;
}
-(void) setyear: (int) newyear
{
Some basic memory management methods are added here, preserving the new values and releasing the previous values
if (newyear! = year)
{
[Year release];
Year = [newyear retain];
}
}
You can also bypass the naming convention of OC and specify the name of the getter and setter method yourself;
@property (getter = Isexist,setter = setexist:) BOOL exist;
@synthesize exist;
You can use the new defined method name or use the previous method (point notation)
Properties: ReadWrite readonly assign retain copy nonatomic
Assign: The default behavior, using @property int year, uses the assign behavior, which is to assign a value to the instance variable
Retain: Implemented two functions, one is to retain the assignment of the object passed, followed by the assignment before the release of the previous value, using retain can achieve the advantages of the memory management discussed above, using the @property (retain) int year;
Copy: Sends a copy of the message to the object being passed, retains it, and releases any previous values;
The difference between the instance method and the class method