2014-09-24 36 views
0

簡單問題:可可與ARC:設置一個強大的屬性

我有一個AVPlayer屬性調用播放器(可以是任何強大的性能,它只是AVPlayer例如的緣故)。

如果它已經分配(而不是零),並沒有設定我重新分配它,它爲nil:

self.player = [[AVDelegatingPlayer alloc] initWithURL:[NSURL URLWithString:urlString]]; 

處於ARC環境這有記憶的影響?

+1

不,這是由ARC自動處理的。 – 2014-09-24 19:29:22

回答

1

查看clang源代碼,將對象存儲到__strong變量中。

https://github.com/llvm-mirror/clang/blob/master/lib/CodeGen/CGObjC.cpp#L2108-L2119

// Retain the new value. 
newValue = EmitARCRetain(type, newValue); 

// Read the old value. 
llvm::Value *oldValue = EmitLoadOfScalar(dst, SourceLocation()); 

// Store. We do this before the release so that any deallocs won't 
// see the old value. 
EmitStoreOfScalar(newValue, dst); 

// Finally, release the old value. 
EmitARCRelease(oldValue, dst.isARCPreciseLifetime()); 

所以,你的代碼將被編譯成以下。

id newValue = [[AVDelegatingPlayer alloc] initWithURL:[NSURL URLWithString:urlString]]; 
id oldValue = self.player; 
self.player = newValue; 
[oldValue release]; 
+0

它是我的想法。謝謝。 – Morkrom 2014-09-24 21:08:43

相關問題