您的位置:首页 > 其它

Interacting with the Runtime

2013-11-17 15:13 489 查看
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];
}
@end

int 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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐