Verify Object Capabilities at Runtime

Verify Object Capabilities at Runtime

Introspection, a powerful and useful feature of Objective-C and the NSObject class, enables you to learn certain things about objects at runtime. You can thus avoid mistakes in your code such, as sending a message to an object that doesn’t recognize it or assuming that an object inherits from a given class when it doesn’t.

运行时需要验证对象的三种性质,可以避免错误,三种性质包括以下:

There are three important types of information that an object can divulge about itself at runtime:

  • Whether it’s an instance of a particular class or subclass

  • Whether it responds to a message

  • Whether it conforms to a protocol

Discover Whether an Object Is an Instance of a Particular Class or its Subclasses

某对象是否是某特定类或其子类的实例对象;

To discover whether an object is an instance of a class or its subclasses, call the isKindOfClass: method on the object. An app sometimes makes this check when it wants to discover the messages (implemented or inherited) that an app responds to.

static int sum = 0;
for (id item in myArray) {
if ([item isKindOfClass:[NSNumber class]]) {
int i = (int)[item intValue];
sum += i;
}
}

The isKindOfClass: method takes an object of type Class as a parameter; to get this object, you call the class method on the class symbol. Then evaluate the Boolean value returned by this method and proceed accordingly.

NSObject declares other methods for discovering information about object inheritance. The isMemberOfClass: method, for example, tells you whether an object is an instance of a specific class, whereas isKindOfClass: tells you whether the object is a member of that class or any of its descendent classes.

isMemberOfClass: 方法告诉我们某对象是否某特定类的实例(不包括子类),

isKindOfClass: 方法告诉我们某对象是否某特定类的实例 或者 该类子类的实例。

Discover Whether an Object Responds to a Message

某对象是否能够某信令作出反应;

To discover whether an object responds to a message, call the respondsToSelector: method on the object. App code often verifies that an object responds to a message before it sends the message to the object.

if ([item respondsToSelector:@selector(setState:)]) {
[item setState:[self.arcView.font isBold] ? NSOnState : NSOffState];
}

The respondsToSelector: method takes a selector as its parameter. A selector is an Objective-C data type for runtime identifiers of methods; you specify a selector using the @selector compiler directive. In your code, evaluate the Boolean value returned by this method and proceed accordingly.


[item respondsToSelector:@selector(setState:)]

For identifying the messages an object responds to, calling respondsToSelector: is generally more useful than evaluating class type. For example, a more recent version of a class might implement a method that isn’t found in a prior version.

Discover Whether an Object Conforms to a Protocol

某对象是否遵守某协议

To discover whether an object conforms to a protocol, call the conformsToProtocol: method on the object.

- (void) setDelegate:(id __weak) obj {
NSParameterAssert([obj conformsToProtocol:@protocol(SubviewTableViewControllerDataSourceProtocol)]);
delegate = obj;
}

[obj conformsToProtocol:@protocol(SubviewTableViewControllerDataSourceProtocol)]

The conformsToProtocol: method takes a runtime identifier of a protocol as a parameter; you specify this identifier using the@protocol compiler directive. Evaluate the Boolean value returned by this method and proceed accordingly. Note than an object can conform to a protocol without implementing its optional methods.


转载于:https://my.oschina.net/zhmsong/blog/92052

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值