• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: A quick one: Passing a reference/pointer to NSString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A quick one: Passing a reference/pointer to NSString


  • Subject: Re: A quick one: Passing a reference/pointer to NSString
  • From: Ken Thomases <email@hidden>
  • Date: Tue, 22 Jul 2008 21:14:02 -0500

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@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: A quick one: Passing a reference/pointer to NSString
      • From: Ken Thomases <email@hidden>
References: 
 >Re: A quick one: Passing a reference/pointer to NSString (From: Jeff Brown <email@hidden>)

  • Prev by Date: Re: A quick one: Passing a reference/pointer to NSString
  • Next by Date: Re: A quick one: Passing a reference/pointer to NSString
  • Previous by thread: Re: A quick one: Passing a reference/pointer to NSString
  • Next by thread: Re: A quick one: Passing a reference/pointer to NSString
  • Index(es):
    • Date
    • Thread