Re: New Cocoa Programmer
Re: New Cocoa Programmer
- Subject: Re: New Cocoa Programmer
- From: Marcel Weiher <email@hidden>
- Date: Mon, 12 Nov 2001 09:26:09 +0100
On Sunday, November 11, 2001, at 03:20 PM, Ondra Cada wrote:
John,
John C. Randolph (JCR) wrote at Sun, 11 Nov 2001 05:11:22 -0800:
JCR> I don't quite agree with that. I notice that I don't write many
JCR> switch() statments anymore.
I do, though. Although you always can replace a switch by a number of
ifs,
quite often it makes code pretty unreadable.
JCR> BTW, the most horrific Obj-C code I ever saw used "switch ([self
JCR> class])"
I don't think you saw this, unless you use your time reading code which
would never compile. The problem is that class ids are link-time
constant,
and case: statements allow only compile-time ones. Just try it:
185 /tmp\> cat q.m
#import <Foundation/Foundation.h>
void main() {
switch ([NSString class]) {
case (int)[NSString class]: break; // would fail
}
186 /tmp\> cc -Wall -framework Foundation q.m
q.m: In function `main':
q.m:4: switch quantity not an integer
q.m:5: case label does not reduce to an integer constant
187 /tmp\>
Actually, this could be compiled back when class-names by themselves
could be used
switch ( [myObject class] ) {
case NSString:
break;
}
I don't quite remember when class-names stopped being legal values, but
I think the change was between NextStep and OpenStep.
Marcel