Precautions for using self in class methods and precautions for using self in class methods

Source: Internet
Author: User

Precautions for using self in class methods and precautions for using self in class methods

What is the result of using self in class methods? This problem is encountered in coding in the afternoon: "self is directly used in class methods, so that the function is not called ." Then, by the way, I studied how self is represented in the class (or instance) method.


● First, pay special attention to using self as a parameter in class methods.

● Second, use self in the class (or instance) method to call the method (if it is incorrect, the compiler will give a compilation error ).


First case: Use self as a parameter in class methods. Generally, when the parameter specifies a type, the self passed by the programmer is also of the corresponding type. However, the compiler gives a warning.

The compiler prompts developers to use the Class as TabBarController.

In another case, if the parameter is of the id type, the compiler will not report an error. The following code:
+ (Void) goToInfoPage {RequestItem * requestItem = [[RequestItem alloc] initWithOwner: [TabBarController sharedInstance];/* compile with self, but it fails to achieve the expected results */[requestItem setDelegateTarget: [TabBarController sharedController]/* use self to compile but not achieve the expected results */succeedMethod: @ selector (requestInfoSuccess :) failedMethod: @ selector (requestInfoFailure :)];}

The initWithOwner and setDelegateTarget parameters are of the id type. The above code can be compiled using self, but after the request, neither requestInfoSuccess nor requestInfoFailure is executed. In the initWithOwner function, print the following parameter values:
---- Use self ---- (lldb) po delegateTabBarController ---- use [TabBarController sharedInstance] ---- (lldb) po delegate <TabBarController: 0x18952020>

Obviously: Using self is actually a Class type and is not the expected Class object. Therefore, the response selector is not determined in RequestItem.
For the second scenario: Use self in the class (Instance) method to call the method 1. In the instance method, self [can] Call the instance method (most common)
2. In the instance method, self [cannot] Call the Class method. At this time, self is not a Class. [Compilation Error] No visible @ interface for 'xx' declares the selector 'xx'
3. In Class methods, self [can] call Class methods. At this time, self is a Class
4. In the class method, self [cannot] Call the instance method. [Compilation Error] No known class method for selector 'xxxxx'
Self varies with the nature of the function. Therefore, pay special attention to the use of self in class methods.

[Conclusion]: 1. the self in the instance method is the first address of the object.
2. self in the Class method is Class. You cannot use instance variables for class methods, but you can use self because self is not an instance variable. At this time,. self is a 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.

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.