On Jul 22, 2008, at 8:49 PM, Jeff Brown wrote:

Sorry - it should have been:

- (void) aMethod
{
  NSString* string1 = @"";
  NSString* string2 = @"";

  string1 = [self foo:string2];
}

- (NSString*) foo:(NSString*)aString
{
  NSString* stringA = @"Hi there";
  NSString* stringB = @"Everyone";
  aString = stringB;

  return stringA;
}

I need to get 2 strings back from foo. I can get foo to return one. I need to pass the other one in by reference.

Alternatives to consider:

*) Have the method return an NSArray* containing the strings
*) Have the method return a struct which has two NSString* fields
*) Have a method such as:

- (void) getGreeting:(NSString**)greeting andAdressee: (NSString**)addressee;

That is, if you're going to write a get-style method, probably better to have it supply both outputs through by-reference parameters than to supply one via by-reference parameter and another via return value.


It seems that since I'm passing a pointer to an NSString as the argument, it should be fine but it's not.

It was doing exactly what you told it to. In your original code, you had a variable aString which was a pointer to an NSString object. Your code then assigned a different value to the pointer, making it point to a different object. However, aString is local to the -foo: method. Changing what it pointed to did not affect the pointer theString in aMethod. (At the point where -foo: was called, the contents of the theString variable was copied to the aString variable in the new context. After that copy the two variables are entirely independent. They both initially pointed to the same object, but that changed when you assigned a new pointer to aString.)


By the way
How do you reply on this mailing list so that the reply remains part of the thread?

I generally just do a Reply All.

Cheers,
Ken
_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to