objc变量的获取
摘要:【objc变量的获取】 C++成员变量通过偏移来寻找,速度极快。But Objc中的变量通过方法调用来寻找,方法首先根据变量名,找到ivar_t,然后在ivar_t对象中取出偏移,再用此偏移来取值(这最后一步和C++一样)。ivar_t类结构如下: objc-runtime中的object_...
阅读全文
当property遇上category
摘要:【当property遇上category】 @property可以在类定义中,以及extension定义中使用,编译器会自动为@property生成代码,并在变量列表(ivar_list_t)中添加相应的以下划线开头的变量。 在category中,编译器允许定义@property,但不会为此@p...
阅读全文
Effective ObjectiveC 2.0 Note
摘要:【Effective ObjectiveC 2.0 Note】1、The memory for objects is always allocated in heap space and never on the stack。2、对于[NSArray arrayWithObjects:obj1,ob...
阅读全文
key things of ARC
摘要:【key things of ARC】1、使用原则。2、__weak变量的使用问题3、__autoreleasing的使用问题4、block中易造成的强引用环问题。5、栈变量被初始化为nil。6、在ARC下,不加修饰符的变量默认是strong。
阅读全文
copyWithZone详解
摘要:【copyWithZone详解】 NSObject实现了-copy、+copy、+copyWithZone方法。代码如下:+ (id)copy { return (id)self;}+ (id)copyWithZone:(NSZone *)zone { return (id)self;...
阅读全文
Collection Operators
摘要:【Collection Operators】 Collectionoperators are specialized key paths that are passed as the parameter to thevalueForKeyPath:method. The operator is specified by a string preceded by an at sign (@). The key path on the left side of the collection operator, if present, determines the array or set, re.
阅读全文
Accessor Search Implementation Details
摘要:[Accessor Search Implementation Details] Key-value coding attempts to useaccessor methodsto get and set values, before resorting to directly accessing the instance variable. Key-value coding 优先使用accessor methods走获取与设置数据,之后再使直接访问instance variable.[Accessor Search Patterns for Simple Attributes] De...
阅读全文
Tagged Pointers
摘要:【Tagged Pointers】1、what is tagged pointer? 2、原理剖析
阅读全文
New Objective-C Feature
摘要:【Advance Objective-C Feature】1、@import避免反复解析头文件,本地宏对框架API定义无影响。 2、可以导入单独一个头文件。 3、使用了@import后,不再需要选择linked binary 4、#import与#include会默认使用@import 6、@import特性对compile和indexing的速度都有显著提升 7、@import默认会被打开 8、ios7, osx10.9以后才能用,只有内置框架才能使用此特性。 【Garbage Collection】
阅读全文
Improved logging in Objective-C
摘要:【Improved logging in Objective-C】 Example of logging the current method and line number. Paste it into your project, and see what it prints! Preprocessor macros and for logging in C/C++/Objective-C.
阅读全文
Type Encoding
摘要:【Type Encodings】 The compiler encodes the return and argument types for each method in a character string and associates the string with the method selector. 编译器把方法的返回值类型与参数类型encode成字符串,然后把此字符串与method selector关联起来。 When given a type specification, @encode() returns a string encoding that type. Th...
阅读全文
Message Forwarding
摘要:【Preprocess】 在使用forwarding机制前,会先经历2个步骤,只有当这2个步骤均失败的情况下,才会激活forwarding。1、+(BOOL)resolveInstanceMethod:(SEL)selector、resolveClassMethod。 当第一次没找到SEL时,调...
阅读全文
Dynamic Method Resolution
摘要:【Dynamic Method Resolution】 @dynamic directive 用于声明属性的方法dynamic loading,which tells the compiler that the methods associated with the property will be provided dynamically。即编译期不实现此方法,而由动态加载。此directive的作用在于告诉编译器某个类的实例拥有某属性而不会报编译错误。 You can implement the methods resolveInstanceMethod: and resolveCla..
阅读全文
Represent nil with NSNull
摘要:【Represent nil with NSNull】 It’s not possible to add nil to the collection classes described in this section because nil in Objective-C means “no object.” If you need to represent “no object” in a collection, you can use the NSNull class:1 NSArray *array = @[ @"string", @42, [NSNull null]
阅读全文
Encapsulating Data
摘要:【Encapsulating Data】The synthesized methods follow specific naming conventions:The method used to access the value (thegettermethod) has the same name as the property. The getter method for a property calledfirstNamewill also be calledfirstName.The method used to set the value (thesettermethod) star
阅读全文
Working with nil
摘要:【Working with nil】 It’s always a good idea to initialize scalar variables at the time you declare them, otherwise their initial values will contain garbage from the previous stack contents: This isn’t necessary for object pointers, because the compiler will automatically set the variable to nil ...
阅读全文
Determining Equality of Objects
摘要:【Determining Equality of Objects】 If you need to determine whether one object is the same as another object, it’s important to remember that you’re working with pointers.The standard C equality operator == is used to test equality between the values of two variables, like this: When dealing with...
阅读全文
NSLog中的%@
摘要:【NSLog中的%@】 There is one additional substitution token available in Objective-C, %@, used to denote an object. At runtime, this specifier will be substituted with the result of calling either the descriptionWithLocale: method (if it exists) or the description method on the provided object. The desc.
阅读全文
详解objc_msgSend
摘要:【详解objc_msgSend】 1、为了性能,objc_msgSend用汇编写成。存在于objc-msg-x86_64.s中。 2、在上图代码中可以看到,objc_msgSend被分为2个过程:1)在cache中寻找SEL。2)在MethodTable寻找SEL。 3、CacheLookup中,不断地拿SEL与cache中的缓存比较,比较失败,则跳转到LCacheMiss标签继续在MethodTable中搜索。 如果想手动查找cache,则需要调用_cache_getimp函数(汇编实现),此函数是个对外接口层,用于保存与准备环境。 _cache_getImp在头文件中obj...
阅读全文
Mach-O Objc信息导出
摘要:【Mach-O Objc信息导出】1、__DATA,__objc_selrefs 类型:SEL,指向 __TEXT,__objc_methname 中的数据长度:len(section)/len(SEL)注意:对于一个包含objc类信息的bundle,在bundle加载时,objc-runtime会把此bundle中的selrefs改写为指向__TEXT,__objc_methname中的数据。若该seg中数据不存在,存在在堆中新建一个字符串。简而言就是bundle中的selrefs不能指向自己的__TEXT seg。节省内存。2、__DATA,__objc_classlist &&a
阅读全文