ARC禁止自动释放? [英] ARC forbids autorelease?
问题描述
ios的新手并尝试从函数返回NSString。据我了解,我需要 [NSString ... alloc] init]
才能创建返回字符串。此外,由于我调用了 alloc
,我想我必须自己自动释放该对象,但是我收到消息ARC禁止'autorelease'的显式消息发送,所以..当我完成分配的NSString时如何告诉ios?
New to ios and trying to return an NSString from an function. As I understand, I need to [NSString... alloc] init]
in order to create the string for return. Also, since I called alloc
, I guess I have to autorelease the object myself however I'm getting the message "ARC forbids explicit message send of 'autorelease'" so.. how do I tell ios when I'm done with that allocated NSString?
- (NSString *) generateUID
{
char foo[32];
// create buffer of all capital psuedo-random letters
for (int i = 0; i < sizeof(foo); i++)
foo[i] = (random() % 25) + 65; // 0-25 + CAPITAL_A
NSString *uid = [[NSString alloc] initWithBytes:foo length:sizeof(foo) encoding:NSASCIIStringEncoding];
NSLog (@"uid: %@", uid);
return (uid);
}
推荐答案
ARC =自动引用计数=编译器根据对代码的分析添加必要的版本和自动回复。你当然不会看到这一点,因为它发生在幕后。正如sosbom在评论中提到的,你真的应该阅读适用的 Apple网站上的文档。
ARC = automatic reference counting = the compiler adds the necessary releases and autorelases based on its analysis of your code. You don't see this of course because it happens behind the scenes. As mentioned by sosbom in his comment, you really should read the applicable documentation on the Apple website.
这篇关于ARC禁止自动释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!