简体   繁体   English

在iOS上的Assign属性上会发生什么

[英]what happen on assign property on ios

i declare two properties on header file: @property(strong)NSString *p1; @property(assign)NSString *p4; 我在头文件上声明了两个属性: @property(strong)NSString *p1; @property(assign)NSString *p4; @property(strong)NSString *p1; @property(assign)NSString *p4; now on implementation file, i write two method to test assign property: 现在在实现文件中,我编写了两种方法来测试assign属性:

- (void)testAssign {
    NSString *rawString = @"Hello, ";
    NSString *resultString = [rawString stringByAppendingString:@"World"];
    self.p4 = resultString;
    resultString = nil;
    (@"My group is %@", self.p4);

 }

On this scene will be an error. 在此场景中将出现错误。

but when the code as below , this do well: 但是当代码如下时,这样做很好:

- (void)testString {
    NSString *rawString = @"Hello, ";
    NSString *resultString = [rawString stringByAppendingString:@"World"];
    self.p4 = resultString;
    self.p1 = resultString;
    NSLog(@"now result is %@, %@", self.p4, self.p1);
    resultString = nil;
    NSLog(@"now result is %@, %@", self.p4, self.p1);
    self.p1 = nil;
    NSLog(@"now result is %@, %@", self.p4, self.p1);
 }

the result is : 结果是:

2014-06-29 19:10:55.798 TestDemo[20853:303] now result is Hello, World, Hello, World
2014-06-29 19:10:55.798 TestDemo[20853:303] now result is Hello, World, Hello, World
2014-06-29 19:10:55.799 TestDemo[20853:303] now result is Hello, World, (null)

what happen on the second snippet? 第二个片段会发生什么? why is there no error? 为什么没有错误?

You receive an access violation in the first snippet due to the use of an assign property and releasing the object that it points to. 由于使用了assign属性并释放了它指向的对象,因此您在第一个代码段中收到访问冲突。

You don't get one in the second snippet even though it should have the same problem - both the local variable reference and the strong property reference are nilled, which should leave an invalid pointer. 即使第二个代码段也有相同的问题,您也不会得到一个-局部变量引用和强属性引用都被置零,这将留下一个无效的指针。

My suspicion is that this is just something to do with compiler optimisation and timing. 我怀疑这只是与编译器优化和时序有关。 If I single step in the debugger, both snippets work fine, indicating that the single-step process is also changing things. 如果我在调试器中单步执行,则两个代码片段都可以正常工作,这表明单步过程也正在改变事情。

In the second case you are looking at an eventual crash if you continue to access p4, which I confirmed by calling your code in my app delegate, didFinishLaunching and then accessing self.p4 from willResignActive . 在第二种情况下,如果继续访问p4,您将看到最终崩溃,我通过在我的应用程序委托didFinishLaunching调用您的代码,然后从willResignActive访问self.p4来确认。 Enabling Zombies makes it even clearer - "message sent to deallocated instance" in both cases. 启用“僵尸”功能更加清晰-两种情况下都是“发送到已释放实例的消息”。

Under ARC weak is preferred as this will replace the invalid reference with 'nil' - you will still have a bug, but not a crash. 在ARC下, weak是首选,因为它将用'nil'替换无效的引用-您仍然会遇到错误,但不会崩溃。

See also - Objective-C ARC: strong vs retain and weak vs assign 另请参阅-Objective-C ARC:强对保留和弱对分配

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

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