Help! Message sends


#1

Hi all,

I do not understand message sends although i’m sure they are very simple once understood.
Please look at the code below:

Please could someone explain how this works.

Thanks,
Freddie


#2

If you understand function calls, message sends are not much different - the only difference is the syntax.

When you send a message to an object, you are basically invoking a function, passing the object as an argument to the function.

For example, the message send statement:

NSDate *now = [NSDate date];

Can be explained in terms of the function call statement:

NSDate *now = NSDate_date (NSDate);

Here, the function NSDate_date does not exist; I have just made it up for the example.

When you see a message send statement, you can safely imagine that it can be transformed into a function call statement similar to the one shown here.

Furthermore, it is important to know that the name NSDate denotes not only a class name, but it also denotes a unique object.

[Accelerate your learning and become a competent programmer: pretty-function.org]


#3

Thanks for the reply.

So does that mean that [NSDate date] is passing a function address which can be pointed to by the ‘now’ pointer.

Also, in [NSDate date], is NSDate acting as a type for ‘date’ to be stored in or is all of ‘NSDate date’ a function call where ‘date’ is like a sub-function of ‘NSDate’.

Thanks,
Freddie


#4

[quote]So does that mean that [NSDate date] is passing a function address which can be pointed to by the ‘now’ pointer.
[/quote]No.

NSDate *now = [NSDate date];

[NSDate date] returns an object (of type NSDate), which is then stored in ‘now’ pointer variable.

[quote]Also, in [NSDate date], is NSDate acting as a type for ‘date’ to be stored in or is all of ‘NSDate date’ a function call where ‘date’ is like a sub-function of ‘NSDate’.[/quote]No.

Given:

NSDate *now = [NSDate date];

We can read it as follows.

Here, NSDate is a class type, from which NSDate objects can be created:

NSDate *now = ...

Whereas here, NSDate is an object, a class object:

... = [NSDate date];

Class objects are created by Objective-C runtime machinery; a class object has exactly the same name as the name of class type.

I strongly recommend that you read Apple’s Programming with Objective-C resource.


#5

compare less as you can ‘OOP’ with Procedural programming.

Review the concepts and keep coding as the best understanding method :wink:


#6

Very useful to help comprehending.

[quote=“ibex10”]If you understand function calls, message sends are not much different - the only difference is the syntax.

When you send a message to an object, you are basically invoking a function, passing the object as an argument to the function.

For example, the message send statement:

NSDate *now = [NSDate date];

Can be explained in terms of the function call statement:

NSDate *now = NSDate_date (NSDate);

Here, the function NSDate_date does not exist; I have just made it up for the example.

When you see a message send statement, you can safely imagine that it can be transformed into a function call statement similar to the one shown here.

Furthermore, it is important to know that the name NSDate denotes not only a class name, but it also denotes a unique object.

[Accelerate your learning and become a competent programmer: pretty-function.org][/quote]