William Sham
William Sham

Reputation: 13239

Explain object variable declaration and assignment

In this snippet:

NSString *testString; 
testString = [[NSString alloc] init]; 

Why, on the second line, do we not have to write *testString = ... in order to access the location where it's actually pointing?

After the first line, what is *testString and what is testString?

Upvotes: 0

Views: 298

Answers (4)

Jesse Black
Jesse Black

Reputation: 7986

All objects are referred to by pointers. The first line

NSString * testString;

declares the instance variable. If your variable type is an object (aside from type id), you need the *

from then on the reference to testString is pointer

If you create 2 strings. 2 physical objects are created (in memory)

NSString * testString = [[NSString alloc] init];
NSString * testString2 = [[NSString alloc] init];

//setting testString to testString2 will lose the pointer to testString for good

testString = testString2;  //<--bad if you still care about testString (and leaks the memory too)

I recommend checking out Apple's guide on Objective-C. Specifically this section

http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW1

Upvotes: 2

jscs
jscs

Reputation: 64002

Why, on the second line, do we not have to write *testString = ... in order to access the location where it's actually pointing?

The init method returns a generic pointer to an object -- its return type is id. testString is a pointer to an NSString, which is an object, so you are assigning a pointer to another pointer. Dereferencing the assigned-to pointer would be a type mismatch.

A variable name is a place (a label for a memory address) in which to put something. The type of the variable is the kind of thing that you can put there. In the case of a pointer, the kind of thing that you put in it is also a memory address. In order to get that address, you dereference the pointer. The kind of thing that you can put at that address is different from the kind that you put in the pointer itself.

After the first line, what is *testString and what is testString?

After the first line, *testString, or the thing at which testString points, is garbage (actually undefined). testString is a pointer (4 or 8 bytes depending on your system) to a address in memory, and it is also undefined.

After the second line, *testString is an NSString object. testString is still a pointer to an address, where there is a valid NSString object.

Upvotes: 1

AliSoftware
AliSoftware

Reputation: 32681

That's simply because we affect the pointer.

[[NSString alloc] init] returns a pointer to an NSString.

In Cocoa every object is dynamically allocated (as in malloc in C) and every NSObject is manipulated thru its pointer/address (in such a point that many ObjC programmer don't even know that they are manipulating pointers and not objects)

Upvotes: 1

Wanged
Wanged

Reputation: 269

The first line you are creating the pointer of NSString type. Pointers in C++ and Objective-C are denoted by the asterisk (*) character when you declare them. The second line you are saying this pointer called "testString" references the memory location of the NSString object that you have allocated in memory.

Upvotes: 2

Related Questions