9
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);
godel9
  • 7,340
  • 1
  • 33
  • 53
user3090658
  • 157
  • 2
  • 9
  • Wait until you get to addresses to pointers like using NSError. My general suggestion is to read K&R (Kernigan and Richie)'s guide to C to get the in depth albeit dry view of pointers. – Dru Freeman Dec 19 '13 at 04:50

2 Answers2

10

The %@ format specifier takes a pointer to an object, so there's no need to dereference the pointer in the parameter list. In general, there's no need to dereference pointers to Objective C objects.

godel9
  • 7,340
  • 1
  • 33
  • 53
  • Do you mean it replaces the asterisk sign of the pointer that we can dereference a pointer using %@ – user3090658 Dec 19 '13 at 04:41
  • When you use `%@`, you just pass the pointer to the object. There's no need to dereference the pointer. Objective-C has special rules for dealing with Objective-C objects, and this is one of them. – godel9 Dec 19 '13 at 04:48
  • 1
    Pointers in objective C should be treated like Opaque Structures. What this means is you're really not meant to dereference them at all. (No looking behind the curtain) so think of (NSString*) as an NSString Object rather than a pointer to an NSString instance. this is why (id) is generic object. The * is necessary because at heart Obj-C is still C and has to follow certain rules. – Dru Freeman Dec 19 '13 at 04:49
  • 1
    @LordAndrei Well said. – godel9 Dec 19 '13 at 04:51
  • @LordAndrei I'm still new to this language though i have basics on pointer on language C but what do you mean Opaque Sturcture and also thinking of (NSString*) as an object? What does(NSString *) mean with the asterisk and bracket sign. – user3090658 Dec 19 '13 at 04:54
  • 1
    Parenthesis around a class is often used for type casting. Meaning I want to tell the compiler that I would rather believe the variable is the type I am telling it. It's also a good way of denoting a class type. The asterisk obviously is for a pointer. So what I'm denoting is that the "Class" is an NSString pointer. Going any deeper in the comments I think sort of gets a bit off track from the original question unless you want to edit it. – Dru Freeman Dec 19 '13 at 05:04
  • As for opaque structures, I again refer out and suggest you look at: http://en.wikipedia.org/wiki/Opaque_pointer – Dru Freeman Dec 19 '13 at 05:05
7

%@ 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.)

Turix
  • 4,470
  • 2
  • 19
  • 27
  • Can you please explain more in a less technical way? I'm new to this even though i have a strong basics on pointer. – user3090658 Dec 19 '13 at 04:40
  • Does it mean we are using %@ to dereference a pointer instead of * sign? – user3090658 Dec 19 '13 at 04:48
  • 2
    @user3090658 sort of. I would prefer to say that we are using %@ to send a message to an object via a pointer. This is the way messages are sent in objective C. However, you are correct that doing so is basically a way of "dereferencing" the pointer (for example, if the message recipient does not point to a valid object, there will be an exception). But in objective-C you do not need to precede a variable name with a "*" in order to send the object it points to a message. – Turix Dec 19 '13 at 04:52
  • Thanks you cleared me up. so does %@ send message via the pointer can it display all types of data like float int and string? – user3090658 Dec 19 '13 at 04:56
  • @user3090658 No, of course not. You can't send Objective-C messages to non Objective-C objects. – JustSid Dec 19 '13 at 04:58
  • 1
    @user3090658 No, you would only pass objects (i.e., via their pointers) to `%@`. – Turix Dec 19 '13 at 04:59
  • @JustSid what do you mean by non objective-C objects? – user3090658 Dec 19 '13 at 05:00
  • @Turix oh so i can use %@ to view the value of the object using the pointer and %@? – user3090658 Dec 19 '13 at 05:01
  • 2
    @user3090658 A float is a scalar type and not an object. A `NSString` on the other hand is an Objective-C object. Long story short, everything that inherits from `NSObject` is an Objective-C object. – JustSid Dec 19 '13 at 05:01
  • @user3090658 Also, to add to what @JustSid said, in objective-C, you can also create C++ objects if you want. You would not pass such objects to `%@` parameters either. – Turix Dec 19 '13 at 05:02
  • 1
    @user3090658 It doesn't show the value. It shows the description of the object. The `%@` format specifier will ask the objects for its description by calling `[object description]` and printing the returned string. – JustSid Dec 19 '13 at 05:02
  • @Turix That's why I explicitly said Objective-C object and not just object. – JustSid Dec 19 '13 at 05:03
  • @Turix OH I Finally understand the concept so %@ just means the 3 lines of code you typed up there just now. And it prints out its description. Correct me if im wrong – user3090658 Dec 19 '13 at 05:08
  • @user3090658 Pretty much. (There are some very slight technical differences, but they are not relevant here.) Also, again, note that an object's description is something that is up to the class to define. – Turix Dec 19 '13 at 05:15