iOS开发之-Cocoa Fundamental Guide 2-Cocoa Objects

本文深入探讨了Objective-C的动态特性,包括动态类型、绑定和加载,以及通过类别和协议实现语言扩展的方法。文章还介绍了如何使用类别添加方法到现有类,以及如何定义协议作为类间通信的接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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...
}
@end


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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值