Re: changing NSNumber from within another function
Re: changing NSNumber from within another function
- Subject: Re: changing NSNumber from within another function
- From: Alastair Houghton <email@hidden>
- Date: Fri, 28 Mar 2008 16:00:43 +0000
On 28 Mar 2008, at 15:33, Nick Rogers wrote:
Hi,
I have:
- (void)function1
{
NSNumber *moveUp = [NSNumber numberWithBool:NO];
[self function2:moveUp];
//value of moveUp after returning from function2 is the previous
one and not the value thats changed in function2
// so the value [moveUp boolValue] here is still 'NO'
}
- (void)function2:(NSNumber *)value
{
value = [NSNumber numberWithBool:YES];
}
How to change a parameter's value this way from inside a second
method.
Will taking moveUp as global, is the solution. haven't tried that yet.
Hi Nick,
This is a pretty basic question (and really a C question, when it
boils down to it, though you're using ObjC here). I'd advise you to
find yourself a good introductory book on C (or Objective-C); people
here often recommend Steven Kochan's "Programming in Objective-C" <http://www.amazon.com/dp/0672325861
>, though I don't have a copy myself.
I'm not trying to be patronising here---it's just that without
understanding what pointers are about, you're going to have trouble
writing any substantial Objective-C programs, so it's really well
worth getting, reading, and going through the problems/examples in, a
good basic textbook.
To answer your actual question, you might write:
- (void)function2:(NSNumber **)value
{
*value = [NSNumber numberWithBool:YES];
}
but perhaps it would be better to avoid pointers altogether here and
do something like
- (BOOL)function2
{
return YES;
}
then just
moveUp = [NSNumber numberWithBool:[self function2]];
if indeed you actually need it as an NSNumber. You don't have to
store everything in Objective-C objects in object form... there's
nothing wrong with using ordinary types.
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
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