Re: returning value in a function argument
Re: returning value in a function argument
- Subject: Re: returning value in a function argument
- From: Sherm Pendley <email@hidden>
- Date: Wed, 17 May 2006 21:37:25 -0400
On May 17, 2006, at 8:09 PM, Angelo Chen wrote:
How to return NSString in a function argument?
following code does not work:
void getInfo(NSString *info)
{
info = @"my info";
}
later in the code:
NSString *info;
getInfo(info);
but info has nothing
You need to pass by reference:
// Note that info is *not* an NSString* - it's a pointer to a
NSString*. A
// pointer to a pointer.
void getInfo(NSString **info)
{
// First, you have to check to see if info is NULL
if (NULL != info)
{
// Since info is a NSString**, what we get
// back by dereferencing is a NSString*.
*info = @"my info";
}
}
Later on...
NSString *myInformation;
// Use & to pass the address of myInformation - that is, a pointer to
the pointer
getInfo(& myInformation);
I don't know why so many folks are suggesting you return an array
instead. Return by reference is a perfectly cromulent pattern, and
it's been used for decades to work around C's insistence on a single
return value. Far from discouraging it, Apple is adopting it all over
the place with NSError objects.
sherm--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden