Can anyone explain me why we aren’t dereferencing a pointer “now” to an NSDate instance, when we actually want to get the data from this instance, not an address.
NSDate *now = [NSDate date];
NSLog(@"The date is %@", now);
The fact why i’m confused is that the previous example of NSLog usage in Aaron Hillegass “Objective-C Programming” book was:
NSDate *now = [NSDate date];
NSLog(@"The new date lives at %p", now);
This code is clear. We want address, we get it. But how do we get the actual date just by changing the specifier when we continue working with the pointer?
The “actual date” is a concept. The “data” is a series of bytes. They are not the same thing.
If you were using good old fashioned C-style strings and printf, you would write this:
This is because the
printffunction needs the address of the thing to work with it. You’re not passing “the actual data” but the address because it is way more efficient to pass the address around. Passing something to a function means copying its value to the stack area of memory, and it’s way faster to copy an address (32 or 64 bits) than to copy a whole string (several bytes, or maybe KB or MB).So in Objective-C we deal with pointers to objects all the time, because pointers are the easiest way to refer to them. The only thing that needs to dereference the pointers (look at the bytes) is the runtime system, for example when it is translating message selectors into function addresses to execute them. Your code just treats the pointer as a pointer, and passes it around without worrying about the precise layout of the data at the other end (the bytes).