Interacting with the Runtime
来源:互联网 发布:iphone照片直接导入mac 编辑:程序博客网 时间:2024/05/17 18:48
Objective-C programs interact with the runtime system to implement the dynamic features of the language. In Figure 8-7, this interaction occurs at three levels:
- Objective-C source code
- Foundation Framework NSObject methods
- Runtime library API
Figure 8-7. Interacting with the runtime system
In the preceding sections, you discussed the role of the compiler and the runtime library. Now you’ll spend time looking at the runtime features of the Foundation Framework NSObject class.
NSObject Runtime Methods
As discussed throughout this chapter, the Objective-C language provides many dynamic programming capabilities. The runtime system provides a set of APIs that enable you to directly interact with the runtime; however, these are coded in C and thus mandate a procedural programming approach. As an alternative, the Foundation Framework NSObject class provides a set of methods that duplicate much of the functionality available from the runtime APIs. As your custom classes (and nearly all of the Cocoa framework classes) descend from NSObject, your code inherits these methods and thus can use them directly. The functionality provided by the NSObject runtime methods includes:
- Object introspection
- Message forwarding
- Dynamic method resolution
- Dynamic loading
Object Introspection:
Listing 8-10. Introspector main.m File
#import <Foundation/Foundation.h>// Test class 1@interface Greeter : NSObject@property (readwrite, strong) NSString *salutation;- (NSString *)greeting:(NSString *)recipient;@end@implementation Greeter- (NSString *)greeting:(NSString *)recipient{ return [NSString stringWithFormat:@"%@, %@", [self salutation], recipient];}@endint main(int argc, const char * argv[]){ @autoreleasepool { Greeter *greeter = [[Greeter alloc] init]; [greeter setSalutation:@"Hello"]; if ([greeter respondsToSelector:@selector(greeting:)] && [greeter conformsToProtocol:@protocol(NSObject)]) { id result = [greeter performSelector:@selector(greeting:) withObject:@"Monster!"]; NSLog(@"%@", result); } } return 0;}
Immediately after the import statement, the code defines the Greeter class. This class defines a property and a method that returns a simple greeting. In the main() function, you first create aGreeter instance and set the value of the property. Next, you use the NSObject runtime methods to perform object introspection. Specifically, you test the NSObject respondsToSelector: andconformsToProtocol: methods. If the result returned from these two conditional expressions is YES, the code sends a message to the Greeter instance using the NSObject runtime methodperformSelector:withObject:. Finally, the result returned from this method is logged to the output pane. When you compile and run the program, you should observe output similar to that shown inFigure 8-8.
Figure 8-8. Introspector program output
The complete list of NSObject runtime methods is defined in the NSObject class reference and theNSObject protocol reference. These are found in the Foundation Framework Reference Guide.
- Interacting with the Runtime
- 与运行时交互(Interacting with the Runtime)
- [译]Objective-C Runtime Programming Guide - Interacting with the Runtime (一)
- MonkeyRunner_monkeyrunner: interacting with the Views
- MonkeyRunner_monkeyrunner: interacting with the Views
- Objective-C 运行时编程指南 之 Interacting with the Runtime
- Interacting with the User in a Service
- WordPress Essentials: Interacting With The WordPress Database
- INTERACTING WITH FSDMGR(转)
- Interacting with Linknx
- Interacting with Other Apps
- Interacting with Objective-C APIs
- android-Interacting with Other Apps
- Interacting with Other Apps笔记
- 与模拟的硬件按键交互(Interacting with the Simulated Hardware Keys)
- 官方文档 恢复备份指南四 Starting and Interacting with the RMAN Client
- 物联网与嵌入式系统概论-week2-Embedded Systems-Lesson3: Interacting with the Physical World
- iOS-Best Practices for Interacting with a Remote Peripheral Device(API Reference) the sixth part
- 基于 HTML5 的人脸识别技术
- python的range函数
- Linux脚本编程(一)
- 商品管理系统Debug札记
- mipsel-linux-uclibc交叉编译libz
- Interacting with the Runtime
- The Ruby On Rials Gudie -- Active Record Query Interface
- CentOS:安装libvmi和libvirt
- cocos2d-x 中文显示问题的解决方案 (自己封装一个类一次编写终身无忧)
- 跨交换机VLAN及VLAN之间通信
- win732位安装,卸载oracle10g,备份还原方案
- 电脑上同时装IIS和Apache 出现问题怎么办
- php 不等待返回的实现方法(异步调用)
- 使用PHP进行异步HTTP请求