AAV*_*AAV 3 memory-management objective-c core-foundation ios
你能告诉我哪种方式正确,为什么在非ARC世界.
+ (NSString *)getUUID {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return [(NSString*) string autorelease];
}
Run Code Online (Sandbox Code Playgroud)
要么
+ (NSString *)getUUID {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return (NSString*)string;
}
Run Code Online (Sandbox Code Playgroud)
其他答案对于手动保留计数是正确的.当你意识到; ^)并切换到ARC,你将无法发送autorelease.相反,在ARC下,这样做:
+ (NSString *)getUUID {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return CFBridgingRelease(string);
}
Run Code Online (Sandbox Code Playgroud)
A CFBridgingRelease等效于a,CFRelease用于平衡返回的+1保留计数CFUUIDCreateString,但也返回ARC将负责释放的仍然有效的引用.