Cocoa Objects
Object-Oriented Programming with Objective-C
1. The Dynamism of Objective-C
Dynamic typing—determining the class of an object at runtime
Dynamic binding—determining the method to invoke at runtime
Dynamic loading—adding new modules to a program at runtime
2. Extensions to the Objective-C Language
Categories
1. Categories give you a way to add methods to a class without having to make a subclass. The methods in the category become part of the class type and are inherited by all the class’s subclasses.
#import <Foundation/NSArray.h> // if Foundation not already imported
@interface NSArray (PrettyPrintElements)
- (NSString *)prettyPrintDescription;
@end#import "PrettyPrintCategory.h"
@implementation NSArray (PrettyPrintElements)
- (NSString *)prettyPrintDescription {
// implementation code here...
}
@end2. There are some limitations to categories. You cannot use a category to add any new instance variables to the class.
3. You can define categories that add methods to the root class, NSObject. Such methods are available to all instances and class objects that are linked into your code. Informal protocols—the basis for the Cocoa delegation mechanism—are declared as categories on NSObject.
Protocols
1. Protocol is simply a list of method declarations publishing an interface that any class can choose to implement. The methods in the protocol are invoked by messages sent by an instance of some other class.
2. The main value of protocols is that they, like categories, can be an alternative to subclassing. A protocol is a way for a class to declare an interface while concealing its identity. With a protocol, even classes that have no knowledge of another’s identity (that is, class type) can communicate for the specific purpose established by the protocol.
3. There are two types of protocols: formal and informal. Informal protocols are categories on NSObject; as a consequence, every object with NSObject as its root object (as well as class objects) implicitly adopts the interface published in the category. To use an informal protocol, a class does not have to implement every method in it, just those methods it’s interested in.
4. A formal protocol is usually what is designated by the term protocol in Cocoa.
A provider (which usually is a class) declares the formal protocol.
A client class adopts a formal protocol, and by doing so agrees to implement all required methods of the protocol.
A class is said to conform to a formal protocol if it adopts the protocol or inherits from a class that adopts it. (Protocols are inherited by subclasses.)
@protocol NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;
@end@protocol MyProtocol
// implementation of this method is required implicitly
- (void)requiredMethod;
@optional
// implementation of these methods is optional
- (void)anOptionalMethod;
- (void)anotherOptionalMethod;
@required
// implementation of this method is required
- (void)anotherRequiredMethod;
@end
@interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding>
if ([anObject conformsToProtocol:@protocol(NSCoding)]) {
// do something appropriate
}- (void)draggingEnded:(id <NSDraggingInfo>)sender;
Declared Properties
1. You can declare properties wherever methods can be declared in a class, category, or protocol declarative section.

2. The @dynamic directive tells the compiler that you are implementing accessor methods for the property, either directly or dynamically (such as when dynamically loading code). The @synthesize directive, on the other hand, tells the compiler to synthesize the getter and setter methods if they do not appear in the @implementation block. The syntax for @synthesize also includes an extension that allows you to use different names for the property and its instance-variable storage.
@synthesize title, directReports, role = jobDescrip; use the jobDescrip instance variable to back the role property
Objective-C编程详解:动态特性与扩展语言功能
本文深入探讨了Objective-C的动态特性,包括动态类型、绑定和加载,以及通过类别和协议实现语言扩展的方法。文章还介绍了如何使用类别添加方法到现有类,以及如何定义协议作为类间通信的接口。
323

被折叠的 条评论
为什么被折叠?



