ARC Objective-C 中的输出参数

发布于 2024-12-09 07:55:19 字数 986 浏览 0 评论 0原文

我正在使用 Objective-C,并且在使用 ARC 编译器编译代码时,我不知道如何创建和调用不带参数的方法。

这就是我试图在非 ARC Objective-C 中完成的事情(无论如何这可能是错误的)。

//
//  Dummy.m
//  OutParamTest

#import "Dummy.h"

@implementation Dummy

- (void) foo {
    NSString* a = nil;
    [self barOutString:&a];
    NSLog(@"%@", a);
}

- (void) barOutString:(NSString **)myString {
    NSString* foo = [[NSString alloc] initWithString:@"hello"];
    *myString = foo;
}

@end

我已阅读此处的文档: https://clang.llvm.org/docs/AutomaticReferenceCounting.html

和此处: https://developer.apple.com/ Library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html

...但是我发现很难得到任何可以编译的东西,没关系任何正确的事情。有人能够以适合 ARC Objective-C 的方式重写上面代码的要点吗?

I'm using Objective-C, and I don't know how to create and call a method with out parameters when compiling the code with the ARC compiler.

This is the kind of thing I'm trying to accomplish in non-ARC Objective-C (this is probably wrong anyway).

//
//  Dummy.m
//  OutParamTest

#import "Dummy.h"

@implementation Dummy

- (void) foo {
    NSString* a = nil;
    [self barOutString:&a];
    NSLog(@"%@", a);
}

- (void) barOutString:(NSString **)myString {
    NSString* foo = [[NSString alloc] initWithString:@"hello"];
    *myString = foo;
}

@end

I've read the documentation here:
https://clang.llvm.org/docs/AutomaticReferenceCounting.html

and here:
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html

...but am finding it difficult to get anything that compiles, never mind anything that is correct. Would anybody be able to rewrite the jist of the code above, in a way that is suitable for ARC Objective-C?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的那抹悲凉 2024-12-16 07:55:19

您需要在 out 参数上使用 __autoreleasing 属性:

- (void) barOutString:(NSString * __autoreleasing *)myString {
    NSString* foo = [[NSString alloc] initWithString:@"hello"];
    *myString = foo;
}

预发布文档(由于 NDA,我不允许链接到该文档)将 __autoreleasing 放在两个 '*',但它可能只是用作 (__autoreleasing NSString **)

您也不能像原始代码中那样使用间接双指针 (b)。您必须使用这种形式:

- (void) foo {
    NSString* a = nil;
    [self barOutString:&a];
    NSLog(@"%@", a);
}

您还直接在对象上调用dealloc,这是完全错误的。我建议您阅读内存管理指南。

You need to use the __autoreleasing attribute on the out parameter:

- (void) barOutString:(NSString * __autoreleasing *)myString {
    NSString* foo = [[NSString alloc] initWithString:@"hello"];
    *myString = foo;
}

The prerelease documentation (which I'm not allowed to link to due to NDA) puts the __autoreleasing in the middle of the two '*'s, but it might just work as (__autoreleasing NSString **)

You also cannot use an indirect double pointer (b) as in your original code. You must use this form:

- (void) foo {
    NSString* a = nil;
    [self barOutString:&a];
    NSLog(@"%@", a);
}

You are also calling dealloc directly on an object which is completely wrong. I suggest you read the memory management guidelines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文