本文介绍了我需要手动释放 CFStringRef 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你能告诉我哪种方法是正确的,为什么在非 ARC 世界中.
Can you please tell me which is the right way and why in non ARC world.
+ (NSString *)getUUID {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return [(NSString*) string autorelease];
}
或
+ (NSString *)getUUID {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return (NSString*)string;
}
推荐答案
其他答案对于手动保留计数是正确的.当你清醒过来 ;^) 并切换到 ARC 时,你将无法发送 autorelease
.相反,在 ARC 下,这样做:
The other answers are correct for manual retain counting. When you come to your senses ;^) and switch to ARC, you won't be able to send autorelease
. Instead, under ARC, do it this way:
+ (NSString *)getUUID {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return CFBridgingRelease(string);
}
CFBRidgingRelease
等价于 CFRelease
,用于平衡 CFUUIDCreateString
返回的 +1 保留计数,但也返回一个静止的-ARC 将负责发布的有效引用.
A CFBridgingRelease
is equivalent to a CFRelease
for the purposes of balancing the +1 retain count returned by CFUUIDCreateString
, but also returns a still-valid reference that ARC will take care of releasing.
这篇关于我需要手动释放 CFStringRef 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!