4. Category, Protocols, Property, Synthesize

Download Report

Transcript 4. Category, Protocols, Property, Synthesize

OOP with Objective-C
Categories, Protocols and
Declared Properties
What is a Category?
• Objective-C categories provide a means to add
methods to a class. Methods that you add through a
category become part of the class definition.
• If you add a method to the NSString class, any
instance, or subclass, of NSString will have access to
that method.
• Defining a category is identical to defining the
interface for a class, with one small exception: you
add a category name inside a set of parenthesis after
the interface declaration. The format is shown on the
next slide.
Category Format
@interface ClassToAddMethodsTo (category)
...methods go here @end
For example, below is a category that adds a method
to the NSString class. The method reverseString
adds the capability to all NSString objects to
reverse the characters in the string.
@interface NSString (reverse) -(NSString *)
reverseString; @end
See the ReverseString Tutorial
What is a Protocol?
Formal Protocols:
A formal protocol is a collection of method declarations defined
independently of any class, example:
@protocol Prot1 <NSObject>
-(void) print;
-(void) show;
-(float) compute;
@end
Informal Protocols:
These are actually categories.
Adopting a Protocol
The protocol Prot1 can be adopted by any class:
@interface Student <Prot1>
....
@end
The class Student must implement the methods
in the protocol Prot1 in its implementation.
Failure to implement
• If the class that adopts a protocol does not
implement the methods in the protocol, the
compiler will issue a warning for each missing
method.
• As long as the adopted methods are not
called, there will be no consequences for the
running code.
Checking for conformity
During running of the program, checking can be performed whether or not a
given class (or a given object) conforms to a given protocol. The checking
is done like below:
pt is a pointer to an object,
prot1 is a pointer to a Protocol
[pt conformsToProtocol:prot1]
This method returns true if the class of the object pt has
adopted the protocol and false otherwise.
It does not check whether the methods in the protocol are
implemented or not.
Grouping of classes
Protocols allow the grouping of classes and
objects according to which protocols they
have implemented.
This grouping overrides hierarchical
relationships.
Declared Properties
Declaration: If a property is supposed to provide accessors
for the instance variable float value, then declare in the
interface of the class:
@property float value;
They provide a shortcut for defining getter and setter
methods for the instance variables of a class.
They serve only the convenience of the programmer – they
are not an essential feature. Everything they do can be
done with the basic Objective-C features alone.
They work in conjunction with the @synthesize directive.
Declared Properties Specifics
If the programmer sticks to the defaults, then
a) There is convenience in programming the
accessor methods
b) The name of the declared property is the
same as the instance variable: xyz
c) The created accessor methods are:
xyz for the getter, setXyz for the setter
Overriding Defaults
• All defaults for properties can be overridden.
• The name of the property can be different than the
instance variable
• The names of the getter and setter can be set as
desired
• The property can be set to do either assign, or retain
or copy in the setter method.
• The @synthesize directive tells which instance
variable a declared property refers to
Summary of
Categories, Protocols and Properties
• Try them out. See the examples provided.
• Categories are sort of like Interfaces in other
object-oriented programming languages.
• A Protocol is sort of like an interface or
abstract class as well in other object-oriented
programming languages.