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

9781430250500_Fig08-07.jpg

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.

9781430250500_Fig08-08.jpg

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.


原创粉丝点击