iPhone 101
Code Tips
Resources
Links
Subscribe
More
This tutorial was written for Xcode 3.2; it's outdated now. For a more up-to-date tutorial, check out Apple's App Development Tutorial to learn how to write iOS apps. There are also lots of other excellent books and online tutorials you can learn from.
I'll still be posting code tips and shortcuts to idev101; click here for the index. If you'd like to get updates, follow me on Twitter, or by RSS or E-mail.
A method is a piece of code that performs some task. It may or may not return a value. In other languages these may be called functions or subroutines, but in object-oriented languages like Objective-C they're called methods.
Methods are reusable; they can be called over and over again. When you call a method on an object, we say you're sending that object a message.
Class Methods are called on the class name itself, independent of an instance:
[className classMethod];
Instance Methods are methods that an instance of this class can call:
[instanceName instanceMethod];
Creating Methods
Methods are declared in a class @interface file, and implemented in the @implementation file. You don't need to declare a method that is already declared on a parent class. For example, the dealloc method is declared on the root parent NSObject; you don't have to re-declare it. Just implement it in your .m file.
- (void)dealloc { [window release]; // releases the window object [super dealloc]; }
The dash "-" on the first line indicates that this is an instance method. (Class methods use a plus-sign "+" instead.)
(void) indicates the type of the value returned by the method. void means that this method returns no value.
dealloc is the name of the method.
The code for the method goes inside the { curly braces }.
Calling Methods
Objective-C methods are called a little differently than in most other languages. Here's a comparison:
| Language | Method Calling Syntax |
|---|---|
| Java | object.methodName(arg1, arg2) |
| Perl & PHP | functionName($arg1, $arg2); |
| C | functionName(arg1, arg2); |
| C++ | object->method(arg1, arg2); |
| Objective-C | [object methodName:arg1 withOther:arg2]; |
An argument is a value that you pass into the method.
To call an Objective-C method with no arguments, the syntax is [object methodName]
To call a method with one argument, the syntax is [object methodName:arg1]
To call a method with two arguments, the syntax is [object methodName:arg1 andName:arg2]
And so on. The UIButton class, for example, has this instance method:
- (void)setTitle:(NSString *)title forState:(UIControlState)state
This method name is setTitle:forState: . The number of colons (:) indicate how many arguments it takes - in this case two. The first argument is the string to set the title to. The second argument is the button state (such as normal or pressed) in which to set the title. Here's an example of this method in use:
[myButton setTitle:@"Clicked!" forState:UIControlStateHighlighted];
C Functions
Remember that Objective-C is an extension to the C programming language, so you can mix C with your Cocoa. C functions are called with the syntax functionName(arg1, arg2). You'll be using NSLog a lot as you test and debug your code:
NSLog(@"Button title was changed.");
NSLog requires an NSString argument; you can read more about strings here.
- Next:
- Xcode
