简体   繁体   English

我怎样才能查找objective-c的属性@synthesize getter setter代码

[英]how can I look up objective-c's property @synthesize getter setter code

在采访中有一个问题“NSMutableArray属性可以使用副本为什么以及为什么不用”我已经搜索了答案,但我想看看auto generate的getter setter代码我该怎么办?

You cannot see the Obj-C code for the setter only the assembler. 您不能仅为汇编程序查看setter的Obj-C代码。 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" . 为此,首先在Xcode中选择包含属性的源文件,然后选择菜单项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. 搜索setPropertyName:其中PropertyName是您的属性的大写名称。

This will show you the setter calls _objc_setProperty_nonatomic_copy or _objc_setProperty_atomic_copy . 这将显示setter调用_objc_setProperty_nonatomic_copy_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: . 他们基本上只是调用copyWithZone: .

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 . 无论是简单的实验,还是通过上面的汇编程序挖掘,都表明copy属性总是copy而不是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;

then due to the copy the value actually assigned is an immutable array ( NSArray ), contradicting the NSMutableArray declared type. 然后由于copy ,实际分配的值是一个不可变数组( NSArray ),与NSMutableArray声明的类型相矛盾。 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 ];

without so much as a warning. 没有警告。

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. 面试官可能正在寻求的是你要解释一下,对于一个属性copy +可变类型在Objective-C中是没有意义的,因为副本将产生一个不可变的对象值。 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, eg: 但是,具有copy和不可变对象类型( NSArrayNSDictionary等)的属性确实有意义,因此如果为可变对象值分配了它的不可变副本,则会阻止属性的值意外更改,例如:

@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` 

So the general rules are: 所以一般规则是:

  • A property intended to have a mutable object value ( NSMutableDictionary et al ) should never specify copy ; 旨在具有可变对象值的属性( NSMutableDictionary )不应该指定copy ; and
  • A property intended to have an immutable object value, when there is a corresponding mutable object subclass (eg NSArray has NSMutableArray etc.), should always specify copy . 当存在相应的可变对象子类(例如NSArray具有NSMutableArray等)时,旨在具有不可变对象值的属性应始终指定copy

HTH HTH

You can't really see the source code. 你无法真正看到源代码。 You could step into it in Xcode's debugger though, and see an assembler version of it, which is reasonably readable if you know any platform's assembler or are good at guessing abbreviations for english words. 您可以在Xcode的调试器中进入它,并查看它的汇编程序版本,如果您知道任何平台的汇编程序或擅长猜测英语单词的缩写,那么它是相当可读的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM