Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSLog pointer syntax

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!

like image 287
Brett Johnson Avatar asked Apr 14 '13 08:04

Brett Johnson


1 Answers

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)
like image 50
Andrea Avatar answered Nov 02 '22 02:11

Andrea