ObjC argument typing (was: Experimenting with Timers)
ObjC argument typing (was: Experimenting with Timers)
- Subject: ObjC argument typing (was: Experimenting with Timers)
- From: Ondra Cada <email@hidden>
- Date: Mon, 22 Oct 2001 13:05:56 +0100
Isaac,
>
>>>>> Isaac Sherman (IS) wrote at Sun, 21 Oct 2001 21:40:37 -0400:
IS> >1. You don't need to put the (NSTimeInterval), (SEL), (id), etc.
IS> >next to the arguments when you call the method.
IS>
IS> Thank God!! I thought they were mandatory...
Just like plain C functions: they are mandatory when you declare/define
message/method, so as compiler knew the types.
If you send the message, though, the compiler uses this knowledge to convert
arguments properly for you:
-aMethod:(float)f; // compiler knows that the argument of aMethod: is a float,...
...
[o aMethod:1]; // ...so converts here the int constant to a float properly.
You can use explicit casts in cases you do something not quite right, but
you happen to know that in this very case it _is_ right and want to get rid
of the compiler warning:
-aMethod:(NSMutableArray*)a;
...
NSArray *a=...;
// for some reason or another, I am _sure_ that (i) a is an NSMutableArray,
(ii) it is harmless here to change its contents. The reason should be very
explicitly expressed in this comment so as you will know what happens here
the next year when upgrading the code!
[o aMethod:(NSMutableArray*)a]; // without cast, the compiuler would warn
Note that such situations generally indicate bad design, and so should be
pretty rare.
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
2K Development: email@hidden
http://www.2kdevelopment.cz
private email@hidden
http://www.ocs.cz/oc