5

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?

8
  • 1
    What exactly is your problem? What compiler errors do you get? Oct 12, 2011 at 14:04
  • I've updated the code sample above with your suggestions, and included the set of compiler errors. Thanks.
    – Ben
    Oct 12, 2011 at 16:02
  • See my updated answer. As the compiler says, you cannot use an indirect pointer like that, instead you must pass in &a directly. Oct 12, 2011 at 16:07
  • Thanks for your help @MikeWeller, the code above compiles now - is it technically correct?
    – Ben
    Oct 12, 2011 at 16:15
  • You don't need the b variable, just pass in &a as the argument. You also don't need to use __autoreleasing on the a and b variables unless you need to access the initial value in barOutString: which isn't very common. Oct 12, 2011 at 16:17

1 Answer 1

9

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.

2
  • 1
    There's a small error in the first method. *myString = &foo; should be changed to *myString = foo;. Aug 1, 2013 at 15:40
  • 1
    Thanks, fixed. Not sure how that got in there. Aug 2, 2013 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.