1. If you want to create an instance using "-", use "+" as the class method ". 2. Select a return type and enclose it in parentheses. (Void) indicates that no return value is returned. (bool) indicates that a Boolean value is returned. (Nsobject *) returns an nsobject instance. Do not forget the ID ). 3. Give a method name, which usually starts with a lowercase letter, for example, sendemailto. 4. If you do not want to use the parameter, jump to 9 directly. 5. (OC is very disgusting, but it is also a very meaningful mechanism) select two names for your parameters. The first one will become part of the method name, it is then used for external calls (this is optional except the first parameter ). Another name will be used inside the method. This is an exception for the first name of the first parameter of the method (in fact, the name of the first parameter can be considered as a method name ). You only need to select the second name for the first parameter. 6. After selecting a name, you need to select a type for the parameter. The type is enclosed in brackets. 7. (for non-first parameters) Add ":" After the first name of the parameter, and then add the type in 6, followed by the second name of the parameter. 8. Repeat steps 5-7 to add other parameters. 9. Add {} to the method name or parameter.
-(Bool) sendemailto :( nsstring *) paramto withsubject :( nsstring *) paramsubject andemailmessage :( nsstring *) paramemailmessage {/* send the email and return an appropriate value */If ([paramto length] = 0 | [paramsubject length] = 0 | [paramemailmessage length] = 0) {/* one or some of the parameters are empty */nslog (@ "Empty parameter (s) is/are provided. "); return no;} return yes ;}
For the call method, you can do this: [self sendemailto: @ "someone@somewhere.com": @ "my subject": @ "please read my email. "]; you can also do this: [self sendemailto: @" someone@somewhere.com "withsubject: @" my subject "andemailmessage: @" please read my email. "]; or the latter is easier to understand Code . When you want to create a class method, the basic and the definition of the instance method is good, when you start to do so remember the following points: 1. Use "+" instead of "-". 2. You can use the self keyword in the class method (not clear, it seems that the instance method can also be used ). Class methods are useful for providing some new object initialization methods. For example, defining a method named allocandinit can implement the assignment and initialization tasks.