IOS-runtime-1(笔记版)

文档:Objective-C Runtime Programming Guide

大概意思(有时间详细整理该章节):


Objectvie-c Runtime

The runtime system acts as a kind of opeatim system for the Objective-C language.


1.Runtime Versions and Platforms

1.1分两个版本,OCOC-2,新版本的runtime有新特性

1.2手机应用用的是最新版本的runtime, OS 10.5及其以上也是,其他的都是老版本的runtime


2.Interacting with the Runtime

OC runtime system交互,有三个levels:Objectiive-c Source Code,NSOject Methods,Runtim Functions:

Objective -C Source Code: 编译阶段

NSObject Methods:     主要是NSObject的一些方法调用,如:isKindOfClassisMemberOfClassrespondsToSelectorconformsToProtocol

Runtime Functions:

/usr/include/objc 里面的一些方法调用


3.Messaging

主要说 objc_msgSend 函数的调用。


Type Encodings


Declared Properties

可以获取到类里面的属性方法名


Property Type and Functions


an opaque handle to a property descriptor

typedef struct objc_property *Property;



用下面两个方法可以获取到类的全部属性和全部协议

objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)


objc_property_t *protocol_copyPropertyList(Protocol *proto, unsigned int *outCount)


如:

@interface Lender : NSObject {

    float alone;

}

@property float alone;

@end


获取属性列表:

id LenderClass = objc_getClass("Lender");
unsigned int outCount;
objc_property_t *properties = class_copyPropertyList(LenderClass, &outCount);

得到属性名字:

const char *property_getName(objc_property_t property)
objc_property_t class_getProperty(Class cls, const char *name)
objc_property_t protocol_getProperty(Protocol *proto, const char *name, BOOL isRequiredProperty, BOOL isInstanceProperty)


得到 @encode type的属性类型

const char *property_getAttributes(objc_property_t property)


例子:

id LenderClass = objc_getClass("Lender");
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(LenderClass, &outCount);
for (i = 0; i < outCount; i++) {
    objc_property_t property = properties[i];
    fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
}


得到Property Type String

如:

@property(nonatomic,weak)IBOutlet MKMapView *mapview;

  id LenderClass = objc_getClass("ViewController");
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList(LenderClass, &outCount);
    for (i = 0; i < outCount; i++) {
        objc_property_t property = properties[i];
        fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
    }

打印的结果是:

mapview T@"MKMapView",W,N,V_mapview

即,开始是属性名字,后面以 T开始,V结束,中间是这个property的各种属性,如nonatomic等,后面会紧跟属性的实例名


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值