re: A question about static typing
re: A question about static typing
- Subject: re: A question about static typing
- From: Enrique Zamudio <email@hidden>
- Date: Mon, 23 Jul 2001 16:02:09 -0500
- Organization: Nasoft
>
How much do folks really use static typing?
As much as possible...
If I open a file and it has static typed outlets, then I don't have to
open the nib where the object resides to check what objects those
outlets are connected to.
Also, whenever you static-type an object variable instead of using id,
the compiler will check that the object really responds to the messages
you send it; when you use id, the compiler only checks to see if a
message you're sending to an id variable exists anywhere (and raises a
warning when it finds more than one method with the same name but
different return values or parameter types).
Also, if you open someone else's code (or even your own) and find a
variable of type id, it might take you longer to figure out what type of
object the variable is pointing to; if you find a static-typed variable,
you already know what it will be (or should be, at least) pointing to.
The advantage of having id, however, is great. You can have an object of
type id and point it to different classes:
id myVar = someDictionary;
myVar = [someDictionary objectForKey:@"array"]; //suppose this returns
an array
myVar = [myVar objectAtIndex:5]; //suppose this returns a string
myVar = [myVar substringToIndex:10]; //this would return another string
myVar = [myVar componentsSeparatedByString:@"."]; //this would return an
array
myVar = [myVar lastObject]; //a string again
the previous code would not generate any warnings and if the dictionary
has the correct data, everything would run ok. With static-typing you
would have to do a lot of casts or have several variables (one NSArray,
one NSDictionary, one NSString)
Hope this helps,
eZL