Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "OOP with Objective-C Categories, Protocols and Declared Properties."— Presentation transcript:

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

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

3 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

4 What is a Protocol? Formal Protocols: A formal protocol is a collection of method declarations defined independently of any class, example: @protocol Prot1 -(void) print; -(void) show; -(float) compute; @end Informal Protocols: These are actually categories.

5 Adopting a Protocol The protocol Prot1 can be adopted by any class: @interface Student.... @end The class Student must implement the methods in the protocol Prot1 in its implementation.

6 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.

7 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.

8 Grouping of classes Protocols allow the grouping of classes and objects according to which protocols they have implemented. This grouping overrides hierarchical relationships.

9 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.

10 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

11 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

12 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.


Download ppt "OOP with Objective-C Categories, Protocols and Declared Properties."

Similar presentations


Ads by Google