Accessor methods pointer question


#1

This has continued to confuse me and I’ve gone through the book several times.

In the beginning of chapter 20, the example is given regarding creating a property that points to an object:

@property (nonatomic) NSDate *hireDate;

Then it says: “recognize that you have declared a property named hireDate that is a pointer to an NSDate.” That is clear.

From there: “…by default you get an instance variable. This variable is named _hireDate and is a pointer to an NSDate. The compiler also synthesizes two accessor methods:”

- (void)setHireDate:(NSDate *)d;
- (NSDate *)hireDate;

I understand why these methods are created, what is confusing is the syntax - namely the (NSDate *) part. What does the pointer point to within the parentheses? When should I use that syntax? Can someone describe it in more human language terms?

While these methods are synthesized behind the scenes, there are other examples later in the book where we must explicitly use this syntax and it is barely glossed over with no explanation. Any help would be appreciated.


#2

[quote]what is confusing is the syntax - namely the (NSDate *) part. What does the pointer point to within the parentheses? When should I use that syntax? Can someone describe it in more human language terms?
[/quote]
NSDate * in (NSDate *) does not point to anything. However, (NSDate *) as a whole means the value of what follows is the address of an NSDate object.

This syntax may at times look strange and confusing. However, it is quite logical and getting used to it is easy.

In C, for example, the function declarations:

Shape * rotateShape (const Shape *, double);
Shape * rotateShape (const Shape * shape, double degrees);

mean rotateShape is a function that takes an argument of type const Shape * and an argument of type double and returns a value of type Shape * (a pointer to the new rotated shape.)

Also in C, the expression:

(Rectangle *)shape

This is a cast expression and b[/b] is the cast operator (where T is Rectangle *.)
This expression means cast the value of shape to a value of type Rectangle *: that is, pretend that the value of shape is the address of a Rectangle.

Now, a similar function declaration in Objective-C:

+ (Shape *)rotateShape:(const Shape *)shape by:(double)degrees;

means rotateShape:by: is a function that takes an argument of type const Shape * and an argument of type double and returns a value of type Shape * (a pointer to the new rotated shape.)

There are three cast-expression lookalikes in there:

+ (Shape *)rotateShape... 
// the return value is the address of a Shape object

(const Shape *)shape
// the value of shape is the address of a Shape object

(double)degrees
// the value of degrees is the angle of rotation which is of type double

[Become a competent programmer faster than you can imagine: pretty-function.org]


#3

Thanks ibex10, this is helpful.

Just to see if I have my head around it, in the example I gave:

- (void)setHireDate:(NSDate *)d;

This means that once set (or created), assign the value of d to the address of an NSDate object. Is that correct?

[quote=“ibex10”]Also in C, the expression:
(Rectangle *)shape

This is a cast expression and (T) is the cast operator (where T is Rectangle *.)
This expression means cast the value of shape to a value of type Rectangle *: that is, pretend that the value of shape is the address of a Rectangle.[/quote]

Both of those explanations are really clear - thank you.


#4

[quote]Just to see if I have my head around it, in the example I gave:

This means that once set (or created), assign the value of d to the address of an NSDate object. Is that correct?[/quote]
No, it just means d is to contain the address of an NSDate object.

Also, it does not make sense to say “assign the value of d to the address of an NSDate object,” but it makes sense to say “assign d the address of an NSDate object,” or “assign the address of an NSDate object to d.”


#5

This makes perfect sense…thanks again.