我如何查找Objective-C的属性@synthesize getter setter代码 [英] how can I look up objective-c's property @synthesize getter setter code
问题描述
在采访中有一个问题"NSMutableArray属性是否可以使用副本,为什么不使用副本",我已经搜索了答案,但是我想看看自动生成的getter setter代码怎么办?
in interview has a question "Could NSMutableArray property use copy why and why not " I have searched the answer but I want to see the auto generate's getter setter code how can I do?
推荐答案
您只能看到汇编程序的安装程序的Obj-C代码.为此,首先选择包含Xcode中的属性的源文件,然后选择菜单项Produce > Perform Action > Assemble "File.m"
.这将打开一个包含汇编代码的窗口.搜索setPropertyName:
,其中PropertyName
是资产的大写名称.
You cannot see the Obj-C code for the setter only the assembler. To do this first select the source file containing the property in Xcode and then choose the menu item Produce > Perform Action > Assemble "File.m"
. This will open up a window containing the assembly code. Search for setPropertyName:
where PropertyName
is the capitalised name of your property.
这将向您显示设置程序调用_objc_setProperty_nonatomic_copy
或_objc_setProperty_atomic_copy
.要查看那些代码,您将需要使用调试器并逐步使用它们.他们实际上只是调用copyWithZone:
.
This will show you the setter calls _objc_setProperty_nonatomic_copy
or _objc_setProperty_atomic_copy
. To see the code for those you will need to use the debugger and step into them. They essentially just call copyWithZone:
.
更重要的是面试问题背后的原因.通过简单的实验或如上所述的汇编程序挖掘,都表明copy
属性属性始终执行copy
而不是mutableCopy
.所以声明:
More important is the reason behind the interview question. Either simple experimentation, or digging through the assembler as above, shows that the copy
property attribute always does a copy
and not a mutableCopy
. So the declaration:
@property (nonatomic, copy) NSMutableArray *shouldBeInvalid;
在理想情况下会生成编译器错误.如果您将可变数组值分配给属性:
would in an ideal world generate a compiler error. If you assign a mutable array value to the property:
self.shouldBeInvalid = @[ @24 ].mutableCopy;
然后由于copy
实际分配的值是不可变数组(NSArray
),与NSMutableArray
声明的类型相反.稍后尝试将属性值用作可变数组:
then due to the copy
the value actually assigned is an immutable array (NSArray
), contradicting the NSMutableArray
declared type. Trying to use the property value later as a mutable array:
[self.shouldBeInvalid addObject:@42];
会产生运行时错误,因为该属性的值是一个与其声明的类型相反的不可变对象...
will produce a runtime error as the property's value is an immutable object contrary to its declared type...
您还将发现编译器很高兴允许您将一个不变数组分配给该属性:
You'll also find that the compiler happily allows you to assign an immutable array to the property:
self.shouldBeInvalid = @[ @24 ];
没有警告.
面试官可能正在寻找的是让您解释的是,对于属性copy
+可变类型,在Objective-C中没有意义,因为副本将产生不可变的对象值.但是,具有copy
和不可变对象类型(NSArray
,NSDictionary
等)的属性确实有意义,因此,如果分配了可变对象值,则会生成该对象的不可变副本,从而防止了属性值的更改意外地,例如:
What the interviewer was probably seeking was for you to explain that for a property copy
+ mutable type makes no sense in Objective-C as the copy will produce an immutable object value. However a property with copy
and an immutable object type (NSArray
, NSDictionary
, etc.) does make sense so that if a mutable object value is assigned an immutable copy of it is made, which prevents the property's value changing unexpectedly, e.g.:
@property (nonatomic) NSArray *shouldHaveCopy;
NSMutableArray *sample = @[ @"oops" ].mutableCopy;
self.shouldHaveCopy = sample;
[sample addObject:@"'immutable' property changed"];
// self.shouldHaveCopy now references a two element array despite
// its type being `NSArray`
所以一般规则是:
- 旨在具有可变对象值(
NSMutableDictionary
等)的属性永远不应指定copy
;和 - 当具有对应的可变对象子类(例如,
NSArray
具有NSMutableArray
等)时,旨在具有不变对象值的属性应始终指定copy
.
- A property intended to have a mutable object value (
NSMutableDictionary
et al) should never specifycopy
; and - A property intended to have an immutable object value, when there is a corresponding mutable object subclass (e.g.
NSArray
hasNSMutableArray
etc.), should always specifycopy
.
HTH
这篇关于我如何查找Objective-C的属性@synthesize getter setter代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!