The interface declares the class properties and methods.
The implementation defines class instance variables, properties and methods.
Scope of instance variables:
- @private The instance variable is only accessible within class that declares it and other instances of this class type.
- @protected The instance variable is accessible within the class that declares it and the instance methods of any of its subclasses. This is default scope.
- @public The instance variable is accessible from everywhere.
- @package The instance variable is accessible from any other class instances or functions, but outside the package, it is treated as private. This scope can be useful for libraries or framework classes.
- nonatomic Accessors are not atomic and, consequently, could provide different results when accessed concurrently by multiple threads. If not specified, accessors are atomic; that is, their values are always fully set/retrieved.
- assign The setter method performs a simple assignment of properties value without using copy or retain. This is default setting.
- retain On assignment, the input value will be sent retain message and the previous value will be sent a release message.
- copy A copy of new message will be set on assignment and the previous value will be sent a release message.
- strong This attribute (used when ARC memory management is applied on a property) is equivalent to the retain attribute.
- weak This attribute (used when ARC memory management is applied on a property) is similar to the assign attribute except that if the affect property is released, its value is set to nil.
- read/write The property can be read or written to. Both getter and setter methods must be implemented. This is the default setting.
- read-only The property can be read but not written to. The getter method must be implemented.
- getter=getterName Renames the getter to the specified getterName.
- setter=setterName Renames the setter to the specified setterName.
The method type identifier specifies whether the method is a class or an instance method. A class method is declared with a + (plus) sign and indicates that the method has class scope, meaning that it operates at the class level and does not have access to the instance variables of the class (unless they are passed as parameters to the method). An instance method is declared with a – (minus) sign and indicates that the method has object scope. It operates at the instance level and has direct access to the instance variables of the object and its parent objects (subject to the access controls on the instance variables).
A protocol declares methods and properties that can be implemented by any class. A class interface is directly associated with a specific class and, hence, a class hierarchy. On the other hand, a protocol is not associated with any particular class, thus it can be used to capture similarities among classes that are not hierarchically related. Protocols provide Objective-C with the capability to support the concept of multiple inheritance of specification (i.e., of method declarations). A protocol can also be used to define the messages that an object can send (by specifying properties that conform to a protocol).
A category enables the addition of new functionality to an existing class without subclassing it. Typically, categories are used 1) to extend classes defined by others (even if you don’t have access to the source code); 2) as an alternative to a subclass; or 3) to distribute the implementation of a new class into multiple source files. An extension can be considered an anonymous category; however, its declarations must be implemented in the main implementation block of the class. Extensions can also declare instance variables and properties.
An interface may employ inheritance to obtain the properties and methods of subclasses in a class hierarchy.
By convention, a class interface is stored (on disk) in a header file (suffixed with .h) and its implementation is stored in a file suffixed with .m.
Xcode provides templates for creating Objective-C classes, protocols, categories, and extensions, thereby making it easy to get started developing your own classes.
This has been a detailed primer on developing classes using Objective-C and Xcode, so this is
a good time to take a break and review what you’ve gone over. In the next chapter, you’ll pick up where you left off by exploring the details of object messaging using Objective-C.
No comments:
Post a Comment