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
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.
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.
相关文章推荐
- Objective-C 运行时编程指南 之 Interacting with the Runtime
- [译]Objective-C Runtime Programming Guide - Interacting with the Runtime (一)
- 与运行时交互(Interacting with the Runtime)
- Interacting with the User in a Service
- 官方文档 恢复备份指南四 Starting and Interacting with the RMAN Client
- 物联网与嵌入式系统概论-week2-Embedded Systems-Lesson3: Interacting with the Physical World
- Teddy's Aspect Weaver Version 0.3 with Great Updating and Fixing, Especially the Implementing of Getting Runtime Method Context Info and Method Arguments in MSIL Level
- MonkeyRunner_monkeyrunner: interacting with the Views
- Teddy's Aspect Weaver Version 0.3 with Great Updating and Fixing, Especially the Implementing of Getting Runtime Method Context
- Failed to initialize the Common Language Runtime (CLR) v2.0.50727 with HRESULT 0x80004005. You need to restart SQL Serve
- Using the ASP.Net Runtime for extending desktop applications with dynamic HTML Scripts
- Laravel 出现"RuntimeException inEncrypter.php line 43: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths."问题的解决办法
- iOS-Best Practices for Interacting with a Remote Peripheral Device(API Reference) the sixth part
- System.Runtime.InteropServices.COMException (0x8000401A): Retrieving the COM class factory for component with CLSID
- System.Runtime.InteropServices.COMException (0x8000401A): Retrieving the COM class factory for component with CLSID
- MonkeyRunner_monkeyrunner: interacting with the Views
- Interacting with the Master Page from the Content Page
- 官方文档 恢复备份指南四 Starting and Interacting with the RMAN Client
- 【摘要】Advanced Serialization: Format Your Way to Success with the .NET Framework Versions 1.1 and 2.0
- SQL SERVER 2005删除维护作业报错:The DELETE statement conflicted with the REFERENCE constraint "FK_subplan_jo