Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (8.37 MB, 640 trang )
/* Draw the rectangle here */
}
Discussion
Method overloading is a programming language feature supported by Objective-C,
C++, Java, and a few other languages. Using this feature, programmers can create
different methods with the same name, in the same object. However, method overloading in Objective-C differs from that which can be used in C++. For instance, in
C++, to overload a method the programmer needs to assign a different number of
parameters to the same method and/or change a parameter’s data type.
In Objective-C, however, you simply change the name of at least one parameter.
Changing the type of parameters will not work:
- (void) method1:(NSInteger)param1{
/* We have one parameter only */
}
- (void) method1:(NSString *)param1{
/* This will not compile as we already have a
method called [method1] with one parameter */
}
Changing the return value of these methods will not work either:
- (int) method1:(NSInteger)param1{
/* We have one parameter only */
return(param1);
}
- (NSString *) method1:(NSString *)param1{
/* This will not compile as we already have a
method called [method1] with one parameter */
return(param1);
}
As a result, you need to change the number of parameters or the name of (at least) one
parameter that each method accepts. Here is an example where we have changed the
number of parameters:
- (int) method1:(NSInteger)param1{
return(param1);
}
1.3 Defining Two or More Methods with the Same Name in an Object | 9
www.it-ebooks.info
- (NSString*) method1:(NSString *)param1
andParam2:(NSString *)param2{
NSString *result = param1;
if (param1 != nil &&
param2 != nil){
result = [result stringByAppendingString:param2];
}
return(result);
}
Here is an example of changing the name of a parameter:
- (void) drawCircleWithCenter:(CGPoint)paramCenter
radius:(CGFloat)paramRadius{
/* Draw the circle here */
}
- (void) drawCircleWithCenter:(CGPoint)paramCenter
Radius:(CGFloat)paramRadius{
/* Draw the circle here */
}
Can you spot the difference between the declarations of these two methods? The
first method’s second parameter is called radius (with a lowercase r) whereas the second
method’s second parameter is called Radius (with an uppercase R). This will set these
two methods apart and allows your program to get compiled. However, Apple has
guidelines for choosing method names as well as what to do and what not to do when
constructing methods. For more information, please refer to the “Coding Guidelines
for Cocoa” Apple documentation available at this URL:
http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/Co
dingGuidelines/CodingGuidelines.html
Here is a concise extract of the things to look out for when constructing and working
with methods:
• Have your method names describe what the method does clearly, without using
too much jargon and abbreviations. A list of acceptable abbreviations is in the
Coding Guidelines.
• Have each parameter name describe the parameter and its purpose. On a method
with exactly three parameters, you can use the word and to start the name of the
last parameter if the method is programmed to perform two separate actions. In
any other case, refrain from using and to start a parameter name.
10 | Chapter 1: Working with Objects
www.it-ebooks.info
• Start method names with a lowercase letter.
• For delegate methods, start the method name with the name of the class that invokes that delegate method. For more information about delegates, please refer to
Recipe 1.7.
See Also
Recipe 1.6
1.4 Defining and Accessing Properties
Problem
You would like to create an object that has properties of different data types.
Solution
Use the @property directive in the .h file of your object:
#import
@interface MyObject : NSObject {
@public
NSString
*stringValue;
@private
NSUInteger integerValue;
}
@property (nonatomic, copy)
NSString
@property (nonatomic, assign) NSUInteger
*stringValue;
integerValue;
@end
Only properties that are objects can be of type copy or retain. Usually only properties
that are scalars (such as NSUInteger, NSInteger, CGRect, and CGFloat) can have the
assign setter attributes. For more information about this, please refer to the “Declared
Properties” section of Apple’s “The Objective-C Programming Language” guide, available at the following URL:
http://developer.apple.com/iphone/library/documentation/cocoa/conceptual/objecti
vec/Introduction/introObjectiveC.html
Now use the @synthesize directive in the .m file of your object, like so:
#import "MyObject.h"
@implementation MyObject
@synthesize stringValue;
@synthesize integerValue;
1.4 Defining and Accessing Properties | 11
www.it-ebooks.info
- (id) init {
self = [super init];
if (self != nil){
stringValue = [@"Some Value" copy];
integerValue = 123;
}
}
return(self);
- (void) dealloc{
[stringValue release];
[super dealloc];
}
@end
Discussion
In Objective-C, you can create and access variable data within an object in two ways:
• Declaring properties
• Declaring variables
It is important to note that properties are not simply variables. They are in fact, depending on their declaration (whether they are synthesized or not), methods. Simple
variables can also be defined in Objective-C for an object, but they need to be managed
and accessed in a different way. A property can be created, managed, and released in
much more flexible ways than a mere instance variable. For example, a synthesized and
retained property of type NSObject will retain its new value and release the previous
value whenever we assign a new value to it with dot notation, whereas an instance
variable that is being managed by the programmer (not synthesized and not defined as
an @property) will not be accessible with dot notation and will not have a getter or a
setter method created for it. So, let’s focus on properties for now. You should declare
a property in three steps:
1. Declare the variable.
2. Declare the property.
3. Declare the implementation method of that property.
After step 2, our variable becomes a property.
By synthesizing our properties, Objective-C will create the setter and getter methods
(depending on whether the property being synthesized is read/write or read-only).
Whenever the program writes to the property using dot notation (object.property),
the setter method will be invoked, whereas when the program reads the property, the
getter method will be invoked.
12 | Chapter 1: Working with Objects
www.it-ebooks.info