Heliograph 是一个面向对象的、基于反射的可扩展封装,用于 Objective-C 的运行时反射功能。
将 Heliograph 包含到您的项目中最简单的方法是通过 CocoaPods
pod 'Heliograph'
进入反射世界的核心入口是 reflect
函数
#import <Heliograph/Heliograph.h>
HGClassMirror *classMirror = reflect([NSString class]);
HGProtocolMirror *protocolMirror = reflect(@protocol(NSSecureCoding));
HGObjectMirror *objectMirror = reflect(@"Heliograph");
有趣的事实:您可以使用 Heliograph 来理解 Heliograph 的功能!
#import <Heliograph/Heliograph.h>
HGClassMirror *class = reflect([HGClassMirror class]);
for (HGMethodMirror *each in [class methods]) {
NSLog(@"- %@", NSStringFromSelector([each selector]));
}
for (HGMethodMirror *each in [[class classMirror] methods]) {
NSLog(@"+ %@", NSStringFromSelector([each selector]));
}
// =>
// - adoptedProtocols
// - adoptProtocol:
// - instanceVariableNamed:
// - methodNamed:
// - subclasses
// - addInstanceVariableNamed:withEncoding:
// - addMethodNamed:withImplementation:andEncoding:
// ...
// + allClasses
HGMethodMirror *method = [class methodNamed:@selector(addMethodNamed:withImplementation:andEncoding:)];
for (id<HGTypeMirror> each in [method argumentTypes]) {
NSLog(@"%@", [each typeDescription]);
}
// =>
// SEL
// ^
// unknown type
注意:Heliograph 与 OpinionatedC 密不可分,因此这里列出的片段中许多都使用了 Heliograph 和 OpinionatedC!
如果您有任何问题、评论、想法或改进建议,请随时在打开一个问题。如果您即将创建一个拉取请求,只需注意以下几点:
The MIT License (MIT)
Copyright (c) 2016 Leo Schweizer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.