我定义了一个Person类作为测试。

其中Person.h:

//
// Person.h
// Test
//
// Created by zhanggui on 15/8/16.
// Copyright (c) 2015年 zhanggui. All rights reserved.
// #import <Foundation/Foundation.h>
#import <UIKit/UITabBar.h>
@protocol PersonDelete <NSObject> - (void)protocolMethod1;
- (void)protocolMethod2; @end
@interface Person : NSObject<UITabBarDelegate> @property (nonatomic,copy)NSString *name;
@property (nonatomic,assign)NSUInteger age;
@property (nonatomic,copy)NSString *address;
- (void)personRun;
- (void)eat;
- (void)goToSchool:(NSString *)vehicle; + (void)method1;
@end

Person.m:

//
// Person.m
// Test
//
// Created by zhanggui on 15/8/16.
// Copyright (c) 2015年 zhanggui. All rights reserved.
// #import "Person.h" @implementation Person - (void)personRun {
NSLog(@"run method was called");
}
- (void)eat {
NSLog(@"eat method was called");
}
- (void)goToSchool:(NSString *)vehicle {
NSLog(@"Person go to school by %@",vehicle);
}
+ (void)method1 {
NSLog(@"类方法");
}
@end

然后集中在ViewController中的viewDieLoad方法中测试:(具体的讲解都在注释中了)

- (void)viewDidLoad {
[super viewDidLoad];
Person *person = [[Person alloc] init];
unsigned int outCount = ;
Class cls = person.class; //得到Person的class
//--------------------类名
NSLog(@"类名是 :%s",class_getName(cls));
//-----------父类
NSLog(@"父类名是:%s",class_getName(class_getSuperclass(cls))); //是否是元类
if (class_isMetaClass(cls)) {
NSLog(@"Person是元类");
}else
{
NSLog(@"Person不是元类");
}
//返回指定类的元类
Class meta_class = objc_getMetaClass(class_getName(cls));
NSLog(@"%ss 的元类 是 %s",class_getName(cls),class_getName(meta_class)); //变量实例大小 %zu用来输出size_t类型的。
NSLog(@"实例大小::%zu",class_getInstanceSize(cls)); //成员变量 Iavr:一个不透明的类型代表实例变量。这里可以得到其所有的成员变量
Ivar *ivars = class_copyIvarList(cls, &outCount);
for (int i=;i<outCount;i++)
{
Ivar ivar = ivars[i];
NSLog(@"实例变量的名字:%s at index %d",ivar_getName(ivar),i);
}
free(ivars); //释放成员变量实例
//通过名字:获取指定的实例变量,,比如我想获取该类是否有_name这个变量就可以通过该方法获得。
Ivar string = class_getInstanceVariable(cls, "_name");
if (string!=NULL) {
NSLog(@"%s有实列变量:%s",class_getName(cls),ivar_getName(string));
}else
{
NSLog(@"%s没有指定的实列变量:%s",class_getName(cls),ivar_getName(string));
} //属性操作:objc_porperty:代表公开的属性.class_getProperty的参数中:cls代表该类的class,name是该类的属性
objc_property_t array = class_getProperty(cls, "name");
if (array!=NULL) {
NSLog(@"属性:%s",property_getName(array));
}
//方法操作:
Method *methods = class_copyMethodList(cls, &outCount);
for (int i=; i<outCount; i++) {
Method method = methods[i];
NSLog(@"方法签名是:%s",sel_getName(method_getName(method))); //sel_getName:得到方法名字
}
free(methods); //释放methods所占用的内存
// typedef struct objc_method *Method;
//这里得到的是类方法
// struct objc_method {
// SEL method_name OBJC2_UNAVAILABLE;
// char *method_types OBJC2_UNAVAILABLE;
// IMP method_imp OBJC2_UNAVAILABLE;
// }
//IMP Method classMethod = class_getClassMethod(cls, @selector(method1));
if (classMethod!=NULL) {
NSLog(@"%s 有该类方法:%s",class_getName(cls),sel_getName(method_getName(classMethod)));
}else
{
NSLog(@"%s 没有该类方法:%s",class_getName(cls),sel_getName(method_getName(classMethod)));
} //判断类的实例是否相应某个方法:比如alloc就不响应,而init就相应
if (class_respondsToSelector(cls, @selector(alloc))) {
NSLog(@"Person实例 响应 alloc");
}else
{
NSLog(@"Person实例 不响应 alloc");
}
//函数指针
IMP imp = class_getMethodImplementation(cls, @selector(eat));
imp();
//获得该类具有的协议
Protocol *__unsafe_unretained * protocols = class_copyProtocolList(cls, &outCount);
Protocol *protocol;
for (int i=; i<outCount; i++) {
protocol = protocols[i];
NSLog(@"协议名称:%s",protocol_getName(protocol));
}
//判断是否遵守协议
if (class_conformsToProtocol(cls, protocol)) {
NSLog(@"Person遵守协议%s",protocol_getName(protocol));
}else
{
NSLog(@"Person不遵守协议%s",protocol_getName(protocol));
} }

附:

参考与:http://southpeak.github.io/blog/2014/10/25/objective-c-runtime-yun-xing-shi-zhi-lei-yu-dui-xiang/

认识Runtime2的更多相关文章

  1. 腾讯云(Linux)安装.net core sdk2.1、net core runtime2.1

    按照微软指令安装: sdk2.1:https://www.microsoft.com/net/download/linux-package-manager/centos/sdk-current 1. ...

  2. “前.NET Core时代”如何实现跨平台代码重用 ——程序集重用

    除了在源代码层面实现共享("前.NET Core时代"如何实现跨平台代码重用 --源文件重用)之外,我们还可以跨平台共享同一个程序集,这种独立于具体平台的"中性" ...

  3. go runtime scheduler

     http://www.slideshare.net/matthewrdale/demystifying-the-go-scheduler http://www.cs.columbia.edu/~a ...

  4. .Net AppDomain.CurrentDomain.AppendPrivatePath(@"Libs");

    今天就说说.Net中通过反射取得某个类型时,我们怎么知道这个类型在硬盘上的哪个角落?比如说,假如我们需要要求服务端动态载入某个数据源,那服务端怎么知道数据源在哪?网上大部分的教程都写着,可以使用Ass ...

  5. 尝鲜delphi开发android/ios_环境搭建

    Delphi这又老树发新枝了,开始做终端程序开发了,这个东西的准确名字是:RAD Studio XE5,可以使用delphi和c++ builder进行终端开发. 我尽可能讲啰嗦一些,免得回头被人问. ...

  6. .Net冷知识之动态查找类型时的程序集路径问题

    今天就说说.Net中通过反射取得某个类型时,我们怎么知道这个类型在硬盘上的哪个角落?比如说,假如我们需要要求服务端动态载入某个数据源,那服务端怎么知道数据源在哪? 网上大部分的教程都写着,可以使用As ...

  7. 使用 pprof 和 Flame-Graph 调试 Golang 应用

    前言 最近用 Golang 实现了一个日志搜集上报程序(内部称 logger 项目),线上灰度测试过程发现 logger 占用 CPU 非常高(80% - 100%).而此项目之前就在线上使用,用于消 ...

  8. Android:CheckBox控件

    1)ChexkBox继承自CompoundButton组件: 2)isChecked()--确定是否选中:setChecked(bool checked)--设置选中或取消选中: 3)监听事件:Com ...

  9. 理解channel 工作原理以及源码

    Go 的并发特性  goroutines: 独立执行每个任务,并可能并行执行 channels: 用于 goroutines 之间的通讯.同步 一个简单的事务处理的例子  对于下面这样的非并发的程序: ...

随机推荐

  1. 【NS2仿真】TCP与UDP混合

    # # ftp # \ # tcp # \ # n0 sink # \ / # \ / # n1---5M 2ms---n3 # / \ # / \ # n2 null # / # udp # / # ...

  2. NPM使用详解(上)

    1.NPM是什么? NPM是JavaScript的包管理工具,在安装NodeJS(什么?你不知道node?来,我们合计合计:https://nodejs.org/)的时候,会自动安装上npm. 要查看 ...

  3. DDD:群里关于验证的结论

    @汤雪华 验证是为了让数据符合要求.各个层的验证是为了确保传递给各个层的数据符合当前层所需要的数据的要求. @小学僧 db model的验证主要是为了保证数据完整. domain model的验证主要 ...

  4. 关于4K Block Size的Device和 Aligned IO

    背景:最近采购了一批新的服务器,底层的存储设备的默认physical sector size从原有的 512B 改为了 4K. 装完系统以后,在做数据库物理备份恢复时xtrabackup报了这么一个错 ...

  5. Communication - 01.Foreword

    冷落博客已有一年,理由种种,想来是腾出了些时间,但未见得其他方面有了什么可观的进步.打理博客犹如健身,每天不抬几次杠铃活动活动筋骨则憋的荒.消耗了大量的体力,一天下来却倍感清爽,人清爽了做什么都很来劲 ...

  6. Cocos2d-x数据存储

    分别是使用UserDefault,内置文件管理和sqlite3数据库的一般方式: 主要代码: bool DataScene::init(){ if (!Layer::init()){ return f ...

  7. Android学习笔记之使用LBS实现定位

    PS:最近一直在搞使用LBS实现定位.一般现在涉及到日常生活交易平台的app.貌似都需要使用定位.比如说美团外卖,我请客等app. 学习内容: 1.LBS定位的简单介绍. 2.在Map上添加地图覆盖物 ...

  8. 随笔分类 - 无废话ExtJs系列教程

    随笔分类 - 无废话ExtJs系列教程 摘自:http://www.cnblogs.com/iamlilinfeng/category/385121.html ExtJs 入门教程 摘要: extjs ...

  9. 那晚征服的一道js经典的面试题

    今天朋友共享了一道js中经典的面试题,需求是这样的 给定你任意一个字符串,让你写出一个算法,求算出该字符串中出现次数最多的一个字符,并将其结果输出 刚拿到这道题的第一感觉便是定义一个count计时器, ...

  10. bootstrap布局 网格系统

    Bootstrap 网格系统 本章节我们将讲解 Bootstrap 的网格系统(Grid System). Bootstrap 提供了一套响应式.移动设备优先的流式网格系统,随着屏幕或视口(viewp ...