runtime笔记
来源:互联网 发布:知乎自媒体营销模式 编辑:程序博客网 时间:2024/06/17 13:30
想来也是第一次接触runtime,以前在大神的帖子中也能看到runtime,但一直不明白用意。
首先,oc中最重要的元素就是对象。在objc文件中,对象实际上是一个结构体(oc是c的封装)。内部是一个isa指针,这个isa指针指向类对象。
struct objc_object {
Class isa OBJC_ISA_AVAILABILITY;
};
/// A pointer to an instance of a class.
typedef struct objc_object *id;
类:
struct objc_class {
Class isa OBJC_ISA_AVAILABILITY;
#if !__OBJC2__
Class super_class OBJC2_UNAVAILABLE;
const char *name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list *ivars OBJC2_UNAVAILABLE;
struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
struct objc_cache *cache OBJC2_UNAVAILABLE;
struct objc_protocol_list *protocols OBJC2_UNAVAILABLE;
#endif
} OBJC2_UNAVAILABLE;
/* Use `Class` instead of `struct objc_class *` */
在c++中class是个类型,在oc中class是个结构体。其中包含了方法列表,类名,实例变量列表,协议列表等等。其中还有一个isa指针,指向原类对象。原类对象中存放的是类方法。
runtime方法用的较为普遍的是动态添加实例变量。对于类目是无法向类中添加实例变量的。
@interface Person (Extension)
@property (nonatomic, assign)NSInteger age;
@end
@implementation Person (Extension)
- (void)setAge:(NSInteger)age {
NSNumber *number = [NSNumber numberWithInteger:age];
objc_setAssociatedObject(self, @"_age", number, OBJC_ASSOCIATION_ASSIGN);
}
- (NSInteger)age {
return [objc_getAssociatedObject(self, @"_age") integerValue];
}
@end
objc_setAssociatedObject(self, @"_类型名",value, 内存策略)来存储数据,达到向类中添加实例变量的效果。
- runtime笔记
- iOS Runtime 学习笔记
- iOS runtime学习笔记
- Cocoa Runtime学习笔记
- Objc Runtime 学习笔记
- iOS 笔记之 RunTime
- NT_iOS笔记—Runtime
- Runtime学习笔记
- 学习笔记 - Runtime学习
- runtime文档学习笔记
- iOS笔记-runtime objc_msgSend
- OC runtime学习笔记
- runtime学习笔记
- Runtime学习笔记
- Tuxedo Administrative Runtime学习笔记
- 学习笔记之Runtime类
- OC runtime学习笔记一
- OC runtime学习笔记二
- (一)委托模式简介 委托模式是基本的设计模式之一。委托,即是让另一个对象帮你做事情。 许多其他的模式,如状态模式、策略模式、访问者模式本质上是在更特殊的场合采用了委托模式。 委托模式使得我们可以用聚合
- 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?
- EcStore中的App是什么东西?
- code视频记录
- 如何在SVN创建自己的项目
- runtime笔记
- 关于github使用的个人经验
- 6. SpringMVC注解类实践例子2
- 实用网络快速查线方法
- 8. String to Integer (atoi) ---Leetcode
- 用php做消息推送
- linux(ubuntu)myeclipse+eclipse+hadoop系统搭建(三)
- Node.js笔记之模块机制
- 底部视图跟随键盘移动(通知)