我是Objective C的新手,在使用新的ARC编译器编译代码时,我不知道如何使用out参数创建和调用方法。
这是我在非ARC目标C中想要完成的事情(无论如何这可能都是错误的。)
//
// Dummy.m
// OutParamTest
#import "Dummy.h"
@implementation Dummy
- (void) foo {
NSString* a = nil;
[self barOutString:&a];
NSLog(@"%@", a);
}
- (void) barOutString:(NSString * __autoreleasing *)myString {
NSString* foo = [[NSString alloc] initWithString:@"hello"];
*myString = foo;
}
@end
(编辑以匹配建议)。
我在这里阅读了文档: http://clang.llvm.org/docs/AutomaticReferenceCounting.html
...但我发现很难得到任何编译的东西,更不用说任何正确的东西了。是否有人能够以适合ARC目标C的方式重写上述代码的jist?
答案 0 :(得分:8)
您需要在out参数上使用__autoreleasing
属性:
- (void) barOutString:(NSString * __autoreleasing *)myString {
NSString* foo = [[NSString alloc] initWithString:@"hello"];
*myString = foo;
}
预发布文档(由于NDA我不允许链接)会将__autoreleasing
置于两个'*'的中间,但它可能只能作为(__autoreleasing NSString **)
< / p>
您也不能像原始代码那样使用间接双指针(b
)。您必须使用此表单:
- (void) foo {
NSString* a = nil;
[self barOutString:&a];
NSLog(@"%@", a);
}
您还在一个完全错误的对象上直接调用dealloc
。我建议你阅读内存管理指南。