 |
 |
class instances funkiness...
|
 |
|
 |
|
Professional Poster
Join Date: Oct 2001
Status:
Offline
|
|
i'm new to objective-c, and right now it seems REALLY funky to me. for example, NSString. why can i do both?:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
- (void)awakeFromNib
{
NSString *face;
face = <font color = orange>@"foo"</font>;
face = <font color = orange>@"poo"</font>;
[displayHello setStringValue:face];
[face release];
<font color = brown>//that will work just the same as:</font>
NSString *face = [[NSString alloc] init];
face = <font color = orange>@"foo"</font>;
face = <font color = orange>@"poo"</font>;
[displayHello setStringValue:face];
[face release];
}
@end
</font>[/code]
FIRST of all, i thought that NSString was supposed to be written to once or something, and that you needed to use NSMutableString if you wanted to keep assigning something. here everything worked fine, and poo was displayed last in both instances.
so what do i do? what is the difference? does it have to do with memory management or something? i'm so confused.
[ 02-17-2002: Message edited by: itistoday ]
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status:
Offline
|
|
You don't need to use NSMutableString since you aren't working with the same NSString instance. I believe that using the convenience constructor for NSString (something = @"foo") is actually equivalent (something = [NSString stringWithCString:"foo"]) although I am not sure since that theory shouldn't let you explicitly release the object since the class constructor method (ie, one that doesn't use [[alloc] init]) is supposed to return an autoreleased object.
If you print out the memory addresses of the "face" string after each assignment you should find that they are different. This is because you are creating new instances with every assignment, not mutating one of them.
Hope that helps,
Jeff.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
You seem to have a misunderstanding of what @"" is. @"" is a convenient way to create constant NSStrings. At runtime, @"foo" is converted into a special type of NSString object.
NSString * declares a pointer to an NSString object. What you do in the line:
face = @"foo";
is assign the address of the object which contains the string "foo" to your pointer (face). When you do:
face = @"poo";
you are assigning the address of the object which contains the string "poo" to your pointer. you are not changing the object itself, merely changing the address in memory that the pointer points to.
if you don't understand pointers, go back and get a good C book: you'll need it down the line.
[face release] is a NOP, I think, i'm pretty sure constant NSStrings can't be deallocated.
This line is pretty pointless:
NSString *face = [[NSString alloc] init];
as it creates an immutable NSString object in memory containing no data at all. The line:
face = @"foo";
merely changes the pointer to point to a different bit of memory, and the object you just instantiated is left floating somewhere in the ether.
HTH.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
NSString is a subclass of NSObject. NSObject has such instance methods as release, retain, and autorelease. Hence, all our applicable to NSString objects. All the Angus likes pointers, I like saying references. @"foo" and @"poo" are convenenience constructors for NSString objects. So, let's do some explaining by example.
Let's say I have three names, @"Bob" @"Sam", and @"George". Now let's say I have a child, and he has a name. Now, let me assign a name to this child. Magically what happens is a red dotted line gets drawn from the child to the name @"Bob", let's say, and if I change his name to @"Sam", the old magic red line vanishes and a new one goes from the child to @"Sam". Whenever I ask the child what it's name is, it raises it's finger and "points" to the object that the red line is going towards. @"Bob" et all are all autorelease, because they weren't made with an alloc (and all your methods should return autorelease objects, just for standardization). If you want the pointer to exist past the end of the method you're currently end, you'll need to send it a retain message. Otherwise, the magic red line goes poof? No, the pointer is still there, but the object it points to goes poof. And removing the red line does not make the object go poof; it only goes away if its autorelased and you reach the end of the method or if you explicitely release it.
Did that make any sense?
F-bacher
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Feb 2001
Location: zurich, switzerland
Status:
Offline
|
|
Excuse me butting in here but I have a question:
If you do:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
<font color = brown>//Declare a pointer to a string</font>
NSString * face;
<font color = brown>/*and then assign an address to this string
would one not do it with the pointer symbol*/</font>
face = &<font color = orange>@"Foo"</font>;
<font color = brown>/*or does the <font color = orange>@""</font> string object constructor syntax return a pointer implicitly?*/</font></font>[/code]
|
|
weird wabbit
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status:
Offline
|
|
@"" is a convenience constructor for NSString* objects.
The instruction: &@"" would actually return (NSString **) (a pointer to a pointer to a string).
I think what you mean is:
*face = "";
which is generally odd since it would mean take a character array and putting it in the face memory address. I think that you would get some odd crashes if you ever tried to use face after doing something like that since an object pointer isn't just any old pointer, however, it behaves like one until you try to do something like dereference it.
Hope that helps,
Jeff.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Feb 2001
Location: zurich, switzerland
Status:
Offline
|
|
Thanks, you answered my question. I was wondering basically what @"" returned (just too lazy to look it up right then).
As for *face=""; The syntax there would generate a compile time error would it not? I think this would be trying to assign a char array to something of another type as you said. I thought that the pointers to objects were just standard pointers actually, but if they're not would this work?:
*face = *@"foo";
|
|
weird wabbit
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Oct 2001
Status:
Offline
|
|
thank you guys, you really helped me out. I liked Ghoser777's explanation, heh, guess I need to read up on pointers a bit. thanks again.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|