iphone - ARC 禁止自动释放?

标签 iphone ios nsstring

ios 新手,尝试从函数返回 NSString。据我了解,我需要 [NSString... alloc] init] 才能创建要返回的字符串。另外,由于我调用了 alloc,我想我必须自己自动释放对象,但是我收到消息“ARC 禁止显式发送‘autorelease’消息”,所以……我该如何告诉 ios 什么时候我已经完成分配的 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 在他的评论中提到的,你真的应该阅读适用的 documentation on the Apple website .

关于iphone - ARC 禁止自动释放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17603551/

相关文章:

ios - Bing map 给我错误消息

ios - 如果我使用 ARC,-didReceiveMemoryWarning 是否还有用?

objective-c - 如何在 Objective-C 中将 NSString 解析为 BOOL?

ios - Objective-C 中的常量

javascript - AWS Amplify Authenticator React Native Tab Bar Navigation白屏,而不是呈现我的应用

iOS比较英语和德语字符

iphone - 使用 Core Graphics 绘制部分图像或对角剪裁

iphone - sqlite 文件已加密或不是数据库 iPhone sms.db

php - 如何知道 .pem 文件是否正确

ios - NSLocationWhenInUseUsageDescription 警告,但我已经添加了它