OC basics-self keywords, oc-self keywords

Source: Internet
Author: User

OC basics-self keywords, oc-self keywords

Use of Self:

1 self cannot leave the class and leave the class without any meaning

2. self automatically classifies methods and object methods.

3. When using self, you only need to pay attention to the method in which self is located.

If self is used in the class method, self indicates the current class.

If you use self in an object method, self indicates "the object that is currently calling this method"

Declaration file. h:

# Import <Foundation/Foundation. h> @ interface Phone: NSObject {// mobile brand NSString * _ type;}/* -------------------------- getter-setter method ---------------------- * // property reader-(void) setType: (NSString *) type;-(NSString *) type;/* ---------------------------- object method -------------------------------- * // call-(void) callWithNumber: (NSString *) number; // send a text message to-(void) sendMessageWithNumber: (NSString *) number andContent: (NSString *) content;/* ----------------------- class method warning * // warning + (void) alert; // shutdown + (void) turnOFF; @ end

Implementation file. m:

# Import "Phone. h "@ implementation Phone/* ---------------- implement attribute reader ------------------- */-(void) setType :( NSString *) type {_ type = type;}-(NSString *) type {return _ type;}/* ------------------ implementation object method -------------------- * // self = The object currently calling this method = phone // call-(void) callWithNumber :( NSString *) number {// use self to access the NSLog member variable in the object method (@ "Use % @ to call % @", self. type, number);} // send a text message-(void) sendMessageWithNumber :( NSString *) number andContent :( NSString *) content {NSLog (@ "Use % @ to send a text message to % @: % @", self. type, number, content); // use self to call other object methods in the object method [self callWithNumber: number];} /* ---------------- implementation class method -------------------- * // self = current class = Phone // warning + (void) alert {NSLog (@ "warning that your mobile phone is not fully charged in time"); // use self to call other methods in the class method [self turnOFF];} // shutdown + (void) turnOFF {NSLog (@ "The phone is about to shut down");} @ end

Main. m:

# Import <Foundation/Foundation. h> # import "Phone. h"
Int main (int argc, const char * argv []) {Phone * phone = [Phone new]; phone. type = @ "iPhone"; [phone sendMessageWithNumber: @ "13222334455" andContent: @ "Object method text message"]; [Phone alert]; return 0 ;}/ * 14:01:23. 277 self [509: 24866] Text message content sent to 13222334455 on an iPhone: Object method: Text message 14:01:23. 278 self [509: 24866] using an iPhone to call 13222334455 14:01:23. 278 self [509: 24866] Warning: the power of your phone is insufficient. Please recharge your phone in time at 14:01:23, September 30. 278 self [509: 24866] the mobile phone is about to shut down */

Note:

1. In the setter-getter method, you cannot use the self and point syntax to assign values to member attributes and perform value operations, which will lead to an endless loop.

/* ---------------- Implement attribute reader ------------------- */-(void) setType :( NSString *) type {// _ type = type; // self-> _ type = type; self. type = type; // It is equivalent to [self setType: type] loop call setType: Method}-(NSString *) type {// return _ type; // return self-> _ type; return self. type; // equivalent to [self type]; call the type method cyclically}

2. If you call a class method in an object method, an error is reported because self represents an object in the object method, and the class method needs to be called through the class.

// Send a text message-(void) sendMessageWithNumber :( NSString *) number andContent :( NSString *) content {NSLog (@ "Use % @ to send a text message to % @: % @", self. type, number, content); // use self to call other object methods in the object method [self callWithNumber: number]; // error message: no visible @ interface for 'phone' declares the selector 'alert'
[Self alert];}/* ---------------- implementation class method ---------------------- * // self = current class = Phone // warning + (void) alert {NSLog (@ "warning that your mobile phone is not fully charged in time"); // use self to call other methods in the class method [self turnOFF];}

3. You cannot call object methods or access member variables in class methods because object methods and member attributes belong to objects.

/* ---------------- Implementation object method -------------------- * // self = The object currently calling this method = phone // call-(void) callWithNumber :( NSString *) number {// use self to access the NSLog member variable in the object method (@ "Use % @ to call % @", self. type, number);}/* ---------------- implementation class method ---------------------- * // self = current class = Phone // warning + (void) alert {NSLog (@ "warning that your mobile phone is not fully charged in time"); // use self to call other methods in the class method [self turnOFF]; // error message: no known class method for selector 'callwithnumber: '[self callWithNumber: @ "13344552211"];}

4. It is impossible to use self to call the method of the current self in the object method or class method, which will lead to an endless loop.

// Call-(void) callWithNumber :( NSString *) number {// use self to access the NSLog member variable in the object method (@ "Use % @ mobile phone to call % ", self. type, number); [self callWithNumber: number]; // endless loop} // shutdown + (void) turnOFF {NSLog (@ "The phone is about to shut down "); [self turnOFF]; // endless loop}

Use Cases of self:

> It can be used to call each other between object methods.

> It can be used to call each other between class methods.

> It can be used to distinguish between member variables and local variables with the same name.

 

# Import "Phone. h "@ implementation Phone/* ---------------- implement attribute reader ------------------- */-(void) setType :( NSString *) type {// _ type = type; // self-> _ type = type; self. type = type; // equivalent to [self setType: type] endless loop
}-(NSString *) type {// return _ type; // return self-> _ type; return self. type; // equivalent to [self type]; dead loop}/* ---------------- implementation object method -------------------- * // self = The object currently calling this method = phone // call-(void) callWithNumber :( NSString *) number {// use self to access the NSLog member variable in the object method (@ "Use % @ to call % @", self. type, number); [self callWithNumber: number]; // call the method of the current endless loop} // send a text message-(void) sendMessageWithNumber :( NSString *) number andContent :( NSString *) content {NSLog (@ "Use % @ to send a text message to % @: % @", self. type, number, content); // use self to call other object methods in the object method [self callWithNumber: number]; // call class method error information in the object method: no visible @ interface for 'phone' declares the selector 'alert '[self alert];} /* ---------------- implementation class method -------------------- * // self = current class = Phone // warning + (void) alert {NSLog (@ "warning that your mobile phone is not fully charged in time"); // use self to call other methods in the class method [self turnOFF]; // call object method in class method error message: No known class method for selector 'callwithnumber: '[self callWithNumber: @ "13344552211"]; // error message for accessing member variables in the class method: No member named 'type' in 'struct objc_class 'NSLog (@ "type = % @", self. type);} // shutdown + (void) turnOFF {NSLog (@ "the mobile phone is about to shut down"); [self turnOFF]; // call the method currently in an endless loop
} @ End

 

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.