text
stringlengths 0
13.4k
|
---|
Methods Define an Object’s Behavior |
Methods define what an object can do. A method is a piece of code that you define to perform a task or |
subroutine in a class. Methods have access to data stored in the class and can use that information to perform |
some kind of operation. |
For example, to give a to-do item (XYZToDoItem) the ability to get marked as complete, you can add a |
markAsCompleted method to the class interface. Later, you’ll implement this method’s behavior in the class |
implementation, as described in “Implementing Methods” (page 91). |
@interface XYZToDoItem : NSObject |
@property NSString *itemName; |
@property BOOL completed; |
@property (readonly) NSDate *creationDate; |
- (void)markAsCompleted; |
@end |
2013-10-22 | Copyright © 2013 Apple Inc. All Rights Reserved. |
89 |
Writing a Custom Class |
Methods Define an Object’s Behavior |
The minus sign (-) at the front of the method name indicates that it is an instance method, which can be |
called on an object of that class. This minus sign differentiates it from class methods, which are denoted with |
a plus sign (+). Class methods can be called on the class itself. A common example of class methods are class |
factory methods, which you learned about in “Working with Foundation” (page 75). You can also use class |
methods to access some piece of shared information associated with the class. |
The void keyword is used inside parentheses at the beginning of the declaration to indicate that the method |
doesn’t return a value. In this case, the markAsCompleted method takes in no parameters. Parameters are |
discussed in more detail in “Method Parameters” (page 90). |
Method Parameters |
You declare methods with parameters to pass along some piece of information when you call a method. |
For example, you can revise the markAsCompleted method from the previous code snippet to take in a single |
parameter that will determine whether the item gets marked as completed or uncompleted. This way, you can |
toggle the completion state of the item instead of setting it only as completed. |
@interface XYZToDoItem : NSObject |
@property NSString *itemName; |
@property BOOL completed; |
@property (readonly) NSDate *creationDate; |
- (void)markAsCompleted:(BOOL)isComplete; |
@end |
Now, your method takes in one parameter, isComplete, which is of type BOOL. |
When you refer to a method with a parameter by name, you include the colon as part of the method name, |
so the name of the updated method is now markAsCompleted:. If a method has multiple parameters, the |
method name is broken up and interspersed with the parameter names. If you wanted to add another parameter |
to this method, its declaration would look like this: |
- (void)markAsCompleted:(BOOL)isComplete onDate:(NSDate *)date; |
Here, the method’s name is written as markAsCompleted:onDate:. The names isComplete and date are |
used in the implementation to access the values supplied when the method is called, as if these names were |
variables. |
2013-10-22 | Copyright © 2013 Apple Inc. All Rights Reserved. |
90 |
Writing a Custom Class |
Methods Define an Object’s Behavior |
Implementing Methods |
Method implementations use braces to contain the relevant code. The name of the method must be identical |
to its counterpart in the interface file, and the parameter and return types must match exactly. |
Here is a simple implementation of the markAsCompleted: method you added to your XYZToDoItem class |
interface: |
@implementation XYZToDoItem |
- (void)markAsCompleted:(BOOL)isComplete { |
self.completed = isComplete; |
} |
@end |
Like properties, methods can be private or public. Public methods are declared in the public interface and so |
can be seen and called by other objects. Their corresponding implementation resides in the implementation |
file and can’t be seen by other objects. Private methods have only an implementation and are internal to the |
class, meaning they’re only available to call inside the class implementation. This is a powerful mechanism for |