A quick one: Passing a reference/pointer to NSString

2008-07-22 Thread Jeff Brown
Hi Guys

What do I need to do in the following code to get theString to take the value 
I'm giving it in foo i.e. Hi there?

- (void) aMethod
{
NSString* theString = @;
[self foo:theString];
}

- (NSString*) foo:(NSString*)aString
{
NSString* stringB = @Hi there;
aString = stringB;
}

Thanks guys.


  Start at the new Yahoo!7 for a better online experience. www.yahoo7.com.au
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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]


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

2008-07-22 Thread Graham Cox
NSString is not mutable, so once created, you can't change its  
contents. You can reassign the pointer to another string, as you are  
doing - though without knowing your intentions it doesn't really look  
right (and could cause a memory leak). Your 'foo' method is prototyped  
to return a string but isn't doing that, so maybe that's why you're  
not seeing what you expect?


What are you trying to do? If you want to change the content of a  
string, you could use NSMutableString, but the code you've posted  
isn't a great pattern for string manipulation so the intention of it  
isn't clear (to me, anyway).


cheers, Graham





On 23 Jul 2008, at 11:33 am, Jeff Brown wrote:


Hi Guys

What do I need to do in the following code to get theString to take  
the value I'm giving it in foo i.e. Hi there?


- (void) aMethod
{
   NSString* theString = @;
   [self foo:theString];
}

- (NSString*) foo:(NSString*)aString
{
   NSString* stringB = @Hi there;
   aString = stringB;
}

Thanks guys.


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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]


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

2008-07-22 Thread Jeff Brown
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. 

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




  Start at the new Yahoo!7 for a better online experience. www.yahoo7.com.au
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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]


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

2008-07-22 Thread Ken Thomases

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

What do I need to do in the following code to get theString to take  
the value I'm giving it in foo i.e. Hi there?


- (void) aMethod
{
   NSString* theString = @;
   [self foo:theString];
}

- (NSString*) foo:(NSString*)aString
{
   NSString* stringB = @Hi there;
   aString = stringB;
}


If you're just inquiring about how to use the language, I think you're  
looking for:


- (void) aMethod
{
	NSString* theString = nil;	// No sense initializing it with a pointer  
to a string, when you're about to reassign it.
	[self foo:theString];	// Don't pass the pointer theString, pass the  
pointer to the pointer theString so that -foo: can write through the  
pointer-to-pointer to modify the pointer.  Makes sense? ;)

}

- (void) foo:(NSString**)aString		// Note the extra asterisk, which  
establishes an additional level of indirection

{
	*aString = @Hi there;	// Use the asterisk to dereference the  
pointer-to-pointer to modify the pointed-to pointer

}


However, that's a pretty uncommon technique when using Cocoa.  More  
common would be for a method to return a string.


Also, keep the memory management guidelines in mind.  They are  
obscured a bit in trivial examples like this, which only involve  
string literals.  The convention is that callers of the -foo: method,  
since they are not invoking +alloc, +new, -copy..., or -mutableCopy...  
need not concern themselves with releasing the returned object.  So, - 
foo: must be implemented to take care of that itself, often by making  
sure the returned object has been autoreleased.


Lastly, the naming conventions for a method like -foo: would be to  
prefix it with get.  Something like getTitle:.  Again, though,  
more common would be just a title method which returned the title  
rather than outputting it via a by-reference parameter.


Cheers,
Ken
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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]


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

2008-07-22 Thread Graham Cox


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 (Cocoa-dev@lists.apple.com)

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]


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

2008-07-22 Thread Ken Thomases

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 (Cocoa-dev@lists.apple.com)

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]


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

2008-07-22 Thread Ken Thomases

On Jul 22, 2008, at 9:07 PM, Graham Cox wrote:


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


There's nothing about returning an object pointer by reference that's  
inherently prone to leaking.  The more likely problem is in the caller  
of such a method.  If the caller supplies the address of a variable  
which already holds a strong reference to an object, then passing that  
variable by reference to this method would cause the caller to lose  
track of its pointer.  It would thus be unable to later release its  
strong reference, causing a leak.


Cheers,
Ken

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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]


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

2008-07-22 Thread Ken Thomases

On Jul 22, 2008, at 9:14 PM, Ken Thomases wrote:


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.


More alternatives that I forgot to list:

*) Have the method return an NSDictionary* with keys for accessing the  
two strings

*) Have separate methods:

-(NSString*) greeting;
-(NSString*) addressee;

*) The usual justification for returning two values from one method is  
that the two are closely interrelated.  It maybe doesn't make sense  
for them to exist in isolation from one another.  (The trivial  
examples discussed don't illustrate this.)  In such a case, that might  
be a design clue that you need a separate class to represent whatever  
single conceptual idea spans the two tidbits of information.  So, you  
might want a custom class which represents the single concept which  
has two (or more) properties.  Then, -foo (no colon) would return an  
instance of that class.


Cheers,
Ken
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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]


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

2008-07-22 Thread Graham Cox
I know, the problem lies with the caller. But this approach is bad  
form in my view, and does require that you take more care to avoid  
leaks.


Graham


On 23 Jul 2008, at 12:19 pm, Ken Thomases wrote:

There's nothing about returning an object pointer by reference  
that's inherently prone to leaking.


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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]


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

2008-07-22 Thread Jeff Brown
Thanks guys for all the advice.

Much appreciated.

Jeff





  Start at the new Yahoo!7 for a better online experience. www.yahoo7.com.au
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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]