Ben*_*Ben 3 iphone automatic-ref-counting
我是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
Run Code Online (Sandbox Code Playgroud)
(编辑以匹配建议).
我在这里阅读了这些文档:http: //clang.llvm.org/docs/AutomaticReferenceCounting.html
在这里:http: //developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html
...但我发现很难得到任何编译的东西,更不用说任何正确的东西了.是否有人能够以适合ARC目标C的方式重写上面代码的jist?
您需要__autoreleasing在out参数上使用该属性:
- (void) barOutString:(NSString * __autoreleasing *)myString {
NSString* foo = [[NSString alloc] initWithString:@"hello"];
*myString = foo;
}
Run Code Online (Sandbox Code Playgroud)
预发布文档(由于NDA我不允许链接)将__autoreleasing两个'*' 放在中间,但它可能只是作为(__autoreleasing NSString **)
您也不能b像在原始代码中那样使用间接双指针().您必须使用此表单:
- (void) foo {
NSString* a = nil;
[self barOutString:&a];
NSLog(@"%@", a);
}
Run Code Online (Sandbox Code Playgroud)
您还dealloc直接调用一个完全错误的对象.我建议你阅读内存管理指南.
| 归档时间: |
|
| 查看次数: |
3946 次 |
| 最近记录: |