Interacting with the Runtime

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

9781430250500_Fig08-08.jpg

Figure 8-8Introspector 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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值