Dereferencing a pointer when using NSLog in Objective-C
dereference, ios, objective-c, pointers
Solution
`%@` takes a pointer to an object and sends it the `description` message, which returns an `NSString` pointer. (You can override `description` in your classes to customize the string.)
Added in response to comment:
In Objective-C, you to send messages to objects via a pointer using the `[ objectPointer message ]` syntax. So, using your `NSDate` example, you can do:
NSDate * now = [NSDate date];
NSString * dateDescription = [now description]; // Note that "now" points to an object and this line sends it the "description" message
NSLog(dateDescription);
Any instance of a class that inherits from NSObject can be sent the `description` message and thus a pointer to it can be passed to an `%@` format parameter.
(Technical note: if the object supports the `descriptionWithLocale:` message, it will be sent that instead.)
Problem
``` NSDate *now = [NSDate date]; NSLog(@"This NSDate object lives at %p", now); NSLog(@"The date is %@", now); ``` Ok, from this code, I know that `now` is a pointer to an `NSDate` object, but on the code at line 3, how can you dereference a pointer without an asterisk? Why don't we do code like this on the 3rd line: ``` NSLog(@"The date is %@", *now); ```