Differences between class methods and self in ios development and instance methods

Source: Internet
Author: User

Differences between class methods and self in ios development and instance methods

In Objective-C, there are both instance methods and class methods. Class methods are sometimes called Factory methods or Convenience methods ). Factory methods are obviously different from factory methods in the general sense. In essence, class methods can be executed independently of objects, therefore, in other languages, surface methods are sometimes called static methods.
Note 1: class methods
1. Class methods can call class methods.
2. The class method cannot call the instance method, but the class method can access the instance method by creating an object.
3. instance variables cannot be used for class methods. Class methods can use self, because self is not an instance variable.
4. Class methods as messages can be sent to classes or objects (in fact, class methods can be called through classes or objects ).
NOTE 2: self rules
Remember the following rules:
1. self In the instance method is the first address of the object.
2. self in the Class method is Class.
Although self is used in the same class, self has different interpretations. Self in the class method can be translated into class self; self in the instance method should be translated into object self. The self in the class method is essentially different from the self in the instance method, even though their names are all self.

Do you want to release the objects created by class methods with release?
A: You do not need to place this object in the Auto Release pool.

Private methods and private member variables in Object-C

By default, member variables are shared internally and private externally.

@ Interface Controller: NSObject
{

@ Private: NSString * something;
}
+ (Void) thisIsAStaticMethod;
-(Void) thisaninstancemethod;
@ End

@ Interface Controller (Private)
-(Void) thisaprivatemethod;
@ End

The following code gets private variables (remember to add the header file # import ):

NSString * str;
Mobj * obj = [[Mobj alloc] init];
Object_getInstanceVariable (obj, "mt _", (void *) & str );
NSLog (@ "% @", str );
[Obj release];

// ============================================Differences between IOS instance methods and class Methods: class methods and instance methods
1: The instance method is-the class starts with + the instance method is accessed by the instance object, and the class method object is a class rather than an instance. Generally, the object or tool class is created.
In the instance method, messages sent to self and super are actually sent to self according to the inheritance principle.
In the class method, self is the class method of other classes. In the class method, messages are sent to self. Only the class method self is the class super.
When to use the class method, to obtain a shared instance when creating an instance, or to obtain some common information about the Class 2:

Class method and instance method ). Class methods are limited within the scope of the class and cannot be called by the class instance (that is, they are run out of the instance ). Alloc is a type of method. The instance method is limited to the scope of the object instance (that is, it cannot be run before instantiation ). Init is an instance method called by the object instance returned by the alloc method.


NSObject * object1 = [[NSObject alloc] init];

Instance method Starts with a minus sign (-).
Class method Starts with the plus sign "+", which is equivalent to the static method.
3: see is healthier

Objective-C

1. OC is a C-based object-oriented language. It is a superset of C language and has the characteristics of C language.


2. OC defines, implements, and initializes classes.


// Declare the class interface to inherit the NSObject object (this object is the top-level parent class of all classes in OC, and all classes inherit from it)

@ Interface ClassName: NSObject

// Declaration of member attributes and member functions

+ (Void) function; // class method that can be called without instantiating an object

-(Void) function2 :( NSString *) arg; // member method, which must be called through an instantiated object

@ End


// Implementation class

@ Imlementation ClassName

// Member attribute initialization and method definition

@ End


Object initialization: ClassName * obj = [[ClassName alloc] init]

In OC, information is transmitted through the message mechanism, alloc messages are sent to allocate memory space to the class, init messages are sent to generate objects, and the Pointer Points to the object itself.


3. Call of class methods

[Obj function];

NSString * str = [NSString stringWithString: @ "hello"];

[Obj function2: str];


4. Output Functions

Output different values according to different output formats (% d: integer, % @: Object <发送description消息> , % S: string)

NSlog (@ "The result is % d", intNum );

CF stands for Core Foundation (Cocoa)

CFShow sends description to the object it displays. The CFShow printed information does not display the timestamp, NSLog is displayed, and CFShow does not require a format character string. It can only be used for objects.

CFShow (obj );


5. Attributes

Supports dot notation: myTableViewCell. textLabel. text = @ "hello" is equivalent to [[myTableViewCell textLabel] setText: @ "hello"];


Use property generator property

Declare in the H file: @ property int year

Synthesis generator in m file: @ synthesize year

Using obj. year = 1999 is equivalent to calling [obj setYear: 1999];


You can customize the value and value assignment methods (getter and setter)

-(Int) year

{

Return year;

}


-(Void) setYear: (int) newYear

{

// Some basic memory management methods are added here to retain new values and release previous values.

If (newYear! = Year)

{

[Year release];

Year = [newYear retain];

}

}


You can also bypass the oc naming conventions and specify the names of getter and setter methods;

@ Property (getter = isExist, setter = setExist :) BOOL exist;

@ Synthesize exist;

You can use either the newly defined method name or the previous method (dot notation)


Attribute features: readwrite readonly assign retain copy nonatomic

Assign: default action. assign is used when @ property int year is used, which is to assign a value to the instance variable.

Retain: implements two functions. One is to retain the objects passed during the assignment, and the other is to release the previous values before the assignment. Using retain can achieve the advantages of memory management discussed above, add @ property (retain) int year;

Copy: Send a copy message to the passed object, retain it, and release any previous values;

Nonactomic: Non-atomic accessors, which can improve the access speed. However, when two threads modify the same attribute at the same time, a problem occurs, the atomic attribute ensures that the attribute is not accessed by another thread and does not contain the atomic keyword. By default, all methods are automatically merged. (Similar to synchronized in java)

Readwrite: read/write

Readonly: Read-Only





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.