Re: Simple scope check.
Re: Simple scope check.
- Subject: Re: Simple scope check.
- From: Hank Heijink <email@hidden>
- Date: Sat, 9 Dec 2006 19:46:49 -0500
There's a couple of misunderstandings here. Cocoa isn't a language,
for starters. It uses Objective-C, which is a superset of Ansi C, and
so behaves exactly like you would expect other languages to behave. I
suggest you read the cocoa memory management guide (search for that
on Google and you'll find it).
Your function (which, incidentally, I would declare to be an instance
method (preceded by -) instead of a class method (preceded by +) has
some problems too. You're calling a class method on an instance. The
class method returns an autoreleased instance of NSString, which you
are autoreleasing again.
Try this (keeping your function calls intact):
- (NSString *)numberFizzle:(NSNumber *)input
{
NSString *intToStr = [NSString stringWithFormat:@"%d",[input
intValue]];
return [NSString stringWithFormat:@"i%d:%d", [intToStr length],
[input intValue]];
}
You're now returning an autoreleased instance of NSString, so no
worries.
Good luck!
Hank
On Dec 9, 2006, at 7:14 PM, Sandro Noel wrote:
Greetings,
i was just wondering if Cocoa behaved like other languages
concerning variable scope.
i did search the documentation for variable scope but all i get
back is the references for Apple Scripts ...
so... in this function.
+ (NSString *)numberFizzle: (NSNumber *) input{
NSString *intToStr = [ [ [NSString alloc] stringWithFormat:@"%d",
[input intValue]] autorelease];
NSString *tempStr = [ [ [NSString alloc] stringWithFormat:@"i%d:%
d", [intToStr length], [input intValue]] autorelease];
NSLog(tempStr);
return tempStr;
}
so if Objective-C (cocoa) behaves like i think. there is no need
for me to autorealease anything that does not exit my function.
so fo example here, the intToStr variable will be automatically
killed at the end of the function
so i would not have to autorelease it... BUT tempStr will exit the
function therefor i should autorelease it just in case it is not
released by the user...
is that right ?
Thank you for your assistance !
Sandro Noel.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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
Hank Heijink
www.hankheijink.com
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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