5

I'm a little bit confused about the syntax of NSLog. For example,

    NSString *nameString = @"Name";
    NSLog(@"nameString is: %@", nameString);
    
If my understanding is correct (which it very well may not be), then nameString is defined to be a pointer to a String. I thought then that this would print the memory address that nameString holds, not the value of that address. So, if that is true, then in the NSLog statement, to get the value of the pointer, shouldn't we need to use the asterisk notation to access what nameString points to like this:
    NSLog(@"nameString is: %@", *nameString);
    
? It has been a little while since programming in C, but since Objective-C is a superset of C I thought they would behave similarly.

An explanation would be greatly appreciated! Thanks!

Brett Johnson
  • 67
  • 1
  • 7

3 Answers3

16

The command %@ is like "shortcut" that calls the method -description on the receiver. For an NSString it simply display the string itself, since is inherited from NSObject you can override it, very usefull if you create for own class. In that case the default behaviur is print the value of the pointer.
If you want to print the address of the pointer in the string just replace with :

NSLog(@"nameString is: %p", nameString)
Andrea
  • 26,120
  • 10
  • 85
  • 131
  • Thanks for the answer! But isn't the receiver (nameString) in this case just an integer (because it is a pointer). To me it seems like there is something else going on here where the variable nameString is being followed to memory location and the value there is being grabbed. – Brett Johnson Apr 14 '13 at 09:08
  • First nameString is not an int, but a "pointer". A pointer represents an location in the memory usually is displayed as an HEX number (can be casted to an int) but the concept is far away from a number. Second is a part on how methods are dispatched in objC. To send a message you need a pointer to an object and a message, when you send it there is a method dispatcher that start to check in your object class hierarchy who is implementing that method and it execute it (if found). By the way all methods at runtime are "converted" in C functions. – Andrea Apr 14 '13 at 09:18
  • okay, so to send a message like this:
     [nameString description] 
    , nameString has to be a pointer? Then the object that nameString points to has the method invoked on it?
    – Brett Johnson Apr 14 '13 at 09:24
  • Yes, nameString is a pointer because you declared like that or it retuned like that. [nameString description] calls description method on nameString and returns the description string, for instance [nameString length] will return the number of character in that object. – Andrea Apr 14 '13 at 09:29
0

I think that you use an asterisk only to declare a pointer. Then, you only use the name you decided. For example:

NSString *foo = [[NSString alloc] initWithString:@"Hello"];

NSLog(@"%@", foo);

Correct me if I am wrong :)

BalestraPatrick
  • 9,944
  • 4
  • 30
  • 43
0

It's an object and NSLog is a function that uses its format specifiers to determine what to do with the argument. In this case the specifier is %@ which tells NSLog to call a method on an object. Normally this will call the method "description" which returns an NSString but it probably does respondsToMethod first and falls through to some other string methods.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55