oop with objective-c categories, protocols and declared properties

12
OOP with Objective-C Categories, Protocols and Declared Properties

Upload: joan-barrett

Post on 18-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OOP with Objective-C Categories, Protocols and Declared Properties

OOP with Objective-C

Categories, Protocols and Declared Properties

Page 2: 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.

Page 3: OOP with Objective-C Categories, Protocols and Declared Properties

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

Page 4: OOP with Objective-C Categories, Protocols and Declared Properties

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.

Page 5: OOP with Objective-C Categories, Protocols and Declared Properties

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.

Page 6: OOP with Objective-C Categories, Protocols and Declared Properties

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.

Page 7: OOP with Objective-C Categories, Protocols and Declared Properties

Checking for conformityDuring 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.

Page 8: OOP with Objective-C Categories, Protocols and Declared Properties

Grouping of classes

Protocols allow the grouping of classes and objects according to which protocols they have implemented.

This grouping overrides hierarchical relationships.

Page 9: OOP with Objective-C Categories, Protocols and Declared Properties

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.

Page 10: OOP with Objective-C Categories, Protocols and Declared Properties

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

Page 11: OOP with Objective-C Categories, Protocols and Declared Properties

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

Page 12: OOP with Objective-C Categories, Protocols and Declared Properties

Summary ofCategories, 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.