Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rplumii

macrumors newbie
Original poster
Sep 6, 2009
3
0
I had a question regarding method and pointer declarations in Objective C.
Code:
-(NSString *) description
{
return (@"I'm slightly confused");
}

My question about this snippet is whether I am returning a pointer to a NSString or an actual NSString?

Code:
NSString *boolString (BOOL yesNo)
{
if (yesNo == NO)
{
return (@"NO");
}
 else
{
return (@"YES");
}
} // boolString

This relates to my first question. In this I seem to be returning an actual NSString instead of the this ,(NSString *), which if I am to understand is a pointer to an NSString. Please correct me if I'm wrong. My second quesiton about this is that if I am returning a string, why is there an asterisk in front of boolString?
 
You are returning a pointer to an NSString. However, referring to an objective-c object *without* going through a pointer isn't allowed. So we tend to talk about it as just "an NSString".

@"foo" is an NSString* literal, and the [ ] message sending operator implicitly dereferences the pointer.
 
The first thing is that you never pass full objects around, only pointers to them. You only pass full things around when they are a "simple" value, that is they take up only one chunk of memory (so an int, char, float, etc...). Things more complicated than that (strings, objects, etc...) you always pass references to (which, behind the scenes are just ints). Part of this is that in passing things you are copying them, and that would get really messy and prohibitive if you were doing with objects.

Now to make things even weirder, there is a implementation detail in how the compiler deals with static strings like you are using... that is things like @"this". Rather than have a whole bunch of copies for those static strings hanging around in memory, and needing to go through all the trouble of instating them at run-time, the compiler creates a special form of NString for them, and compiles that into a special address. Then any use of the same sequence of characters points at that special address. This creates the weird effect that while the program is running those "static" strings never go away, and never need to be memory managed. So you never have to do [@"this" retain] or [@"this" release], like you would for dynamically created strings.
 
There is no such thing as a "bare" NSString or any object in Objective-C. They all live on the heap and you only refer to them via pointers. Period. The @"" syntax for NSString literals gives you an NSString *. it's special, but it's not a bare NSString.

Just assume any time you're dealing with an object, it's via a pointer. There are no stack objects, ever. There are structs and pointers to them, which can confuse a bit, but don't worry about those now.

http://developer.apple.com/mac/libr...tingStrings.html#//apple_ref/doc/uid/20000148

you can also pass messages to @"" NSString *s like any other NSString object.

-Lee

edit: wow. Too slow. Also, a pointer is not an int. Never tell a beginner that. It's an address in memory. It could be wider than an int.
 
Thank you so much. That made everything a lot clearer. I was really having trouble understanding what I was returning.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.