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 )
readonly directive, only its getter method will be generated. Assigning a value to a
property without a setter method will throw an error.
This means that, by referring to a property in Objective-C using dot notation, you are
accessing the setter/getter methods of that property. For instance, if you attempt to
assign a string to a property called myString of type NSString in an object using dot
notation, you are eventually calling the setMyString: method in that object. Also,
whenever you read the value of the myString property, you are implicitly calling the
myString method in that object. This method must have a return value of type
NSString; in other words, the return value of the getter method of this property (called
myString) must return a value of the same data type as the property that it represents.
This method must not have any parameters.
Instead of using the @synthesize directive to generate getter and setter methods for your
properties automatically, you can create the getter and setter methods manually as
explained before. This directive will generate the getter and setter methods of a property
if they don’t already exist. So, you can synthesize a property but still specify its getter,
its setter, or both manually.
You might be asking: why should I create my own getter and setter methods? The
answer is that you may want to carry out custom operations during the read or write.
A good way to explain this is through an example.
Imagine you have an object with a property called addressLine of type NSString. You
want this address to accept only strings that are 20 characters or less in length. You can
define the setter method in this way:
#import
@interface MyObject : NSObject {
@public
NSString *addressLine;
}
@property (nonatomic, copy) NSString
*addressLine;
@end
Here is the implementation of the validation rule on our property’s setter method:
#import "MyObject.h"
@implementation MyObject
@synthesize addressLine;
- (void) setAddressLine:(NSString *)paramValue{
if ([paramValue length] > 20){
return;
}
14 | Chapter 1: Working with Objects
www.it-ebooks.info
if (paramValue != addressLine){
[addressLine release];
addressLine = [paramValue copy];
}
}
- (void) dealloc {
[addressLine release];
[super dealloc];
}
@end
Now we will attempt to set the value of this property to one string that is 20 characters
and another string that is 21 characters in length:
MyObject *someObject = [[MyObject alloc] init];
someObject.addressLine = @"12345678901234567890";
NSLog(@"%@", someObject.addressLine);
someObject.addressLine = @"123456789012345678901";
NSLog(@"%@", someObject.addressLine);
[someObject release];
What we can see in the console window proves that our validation rule is working on
our custom setter method (see Figure 1-4).
Figure 1-4. The 21-character string was not set
1.5 Managing Properties Manually | 15
www.it-ebooks.info
As you can see, the value of our property stayed intact after we tried to change that
property’s value to a string of 21 characters.
See Also
Recipe 1.4
1.6 Reusing a Block of Code
Problem
You want to be able to reuse a block of code you’ve written or call another reusable
block of code somewhere else in your application.
Solution
Create instance or class methods for your classes in order to create reusable blocks of
code, or simply call a method in your program.
Discussion
In programming languages such as C, we create procedures and functions. A procedure
is a block of code with a name and an optional set of parameters. A procedure does not
have a return value. A function is a procedure with a return value. Here is a simple
procedure (with an empty body) written in C:
void sendEmailTo(const char *paramTo,
const char *paramSubject,
const char *paramEmailMessage){
}
/* send the email here ... */
This procedure is named sendEmailTo and has three parameters, namely paramTo,
paramSubject, and paramEmailMessage. We can then call this procedure in this way:
sendEmailTo("somebody@somewhere.com",
"My Subject",
"Please read my email");
Turning this procedure into a function that returns a Boolean value, we will have code
similar to this:
BOOL sendEmailTo(const char *paramTo,
const char *paramSubject,
const char *paramEmailMessage){
/* send the email here ... */
16 | Chapter 1: Working with Objects
www.it-ebooks.info
if (paramTo == nil ||
paramSubject == nil ||
paramEmailMessage == nil){
/* One or some of the parameters are nil */
NSLog(@"Nil parameter(s) is/are provided.");
return(NO);
}
}
return(YES);
Calling this function is similar to calling the sendEmailTo procedure except that with a
function, we can retrieve the return value, like so:
BOOL isSuccessful = sendEmailTo("somebody@somewhere.com",
"My Subject",
"Please read my email");
if (isSuccessful == YES){
/* Successfully sent the email */
} else {
/* Failed to send the email. Perhaps we should display
an error message to the user */
}
In Objective-C, we create methods for classes. Creating Objective-C methods is quite
different from writing procedures and functions in a programming language such as C.
As mentioned before, we can have either instance or class methods. Instance methods
are methods that can be called on an instance of the class, and class methods are methods that get called on the class itself and do not require an instance of the class to be
created by the programmer. To create a method in Objective-C, follow these steps in
the .m file of your target class:
1. Type − if you want an instance method or + if you want a class method.
2. Choose the return type of your method and enclose it within parentheses—for
instance, (void) for no return value, (BOOL) for a Boolean value, (NSObject *) to
return an instance of NSObject, and so on.
3. Choose a name for your method. Start the name with a lowercase letter. It is common in Objective-C to start method names with a lowercase letter—for instance,
sendEmailTo instead of SendEmailTo.
4. If you do not want any parameters for your method, jump to step 9.
5. Choose two names for your parameter. One name becomes a part of the method
name and will be used from outside the method (this is an optional name). The
other name will be used as a parameter name inside the method. There is an exception to this in which the first name of the first parameter of a method is part of
the name of the method that you chose in step 3. For this first parameter, you must
only choose a second name, which becomes the parameter name used inside the
method itself.
1.6 Reusing a Block of Code | 17
www.it-ebooks.info