Skip to content

Commit 63394cd

Browse files
committed
更新第6题property的本质是什么的回答
1 parent 0f7989c commit 63394cd

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

01《招聘一个靠谱的iOS》面试题参考答案/《招聘一个靠谱的iOS》面试题参考答案(上).md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,38 @@ typedef NS_ENUM(NSInteger, CYLSex) {
686686
@end
687687
```
688688
689+
**更新**:
689690
691+
property在runtime中是`objc_property_t`定义如下:
692+
693+
```objective-c
694+
typedef struct objc_property *objc_property_t;
695+
```
696+
697+
`objc_property`是一个结构体,包括name和attributes,定义如下:
698+
699+
```objective-c
700+
struct property_t {
701+
const char *name;
702+
const char *attributes;
703+
};
704+
```
705+
706+
而attributes本质是`objc_property_attribute_t`,定义了property的一些属性,定义如下:
707+
708+
```objective-c
709+
/// Defines a property attribute
710+
typedef struct {
711+
const char *name; /**< The name of the attribute */
712+
const char *value; /**< The value of the attribute (usually empty) */
713+
} objc_property_attribute_t;
714+
```
715+
716+
而attributes的具体内容是什么呢?其实,包括:类型,原子性,内存语义和对应的实例变量。
717+
718+
例如:我们定义一个string的property`@property (nonatomic, copy) NSString *string;`,通过 `property_getAttributes(property)`获取到attributes并打印出来之后的结果为`T@"NSString",C,N,V_string`
719+
720+
其中T就代表类型,可参阅[Type Encodings](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100-SW1),C就代表Copy,N代表nonatomic,V就代表对于的实例变量。
690721

691722

692723

0 commit comments

Comments
 (0)