• 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: Graham Cox <email@hidden>
  • Date: Wed, 23 Jul 2008 12:07:48 +1000


On 23 Jul 2008, at 11:49 am, 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. It seems that since I'm passing a pointer to an NSString as the argument, it should be fine but it's not.



This is my opinion, so take it FWIW: methods that return one value as a result and another by reference really suck. Sometimes you have no choice, if what you are returning are scalars, C arrays or simple structs, but with objects, there's little excuse for it. What's wrong with returning an array of strings?




Anyway, to your problem:

NSStrings are immutable.

If you *really* want to return a string object by reference, your method would look like this:

- (void) leakLikeABastard:(NSString**) aString
{
    *aString = @"new string";
}


a much better way to do what you are apparently trying to do is:

- (NSArray*) foo
{
    return [NSArray arrayWithObjects:@"Hi there", @"Everyone", nil];
}


But since it's not really too clear what your ultimate aim is, this may not be the best way either.





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


Just hit reply (all) - it worked, your message is in the same thread.


cheers, Graham

_______________________________________________

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: PropertyList -> NSBrowser / NSOutlineView?
  • 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