Re: Need clarification on categories
Re: Need clarification on categories
- Subject: Re: Need clarification on categories
- From: Chris Kane <email@hidden>
- Date: Tue, 19 Mar 2002 09:44:16 -0800
On Tuesday, March 19, 2002, at 08:15 AM, Ondra Cada wrote:
I create a category on NSString that implements a new class method,
+(id)stringWithBool:. But I get compiler warnings when I call the
stringWithBool: method. For example, [NSString
stringWithBool:someBooleanValue]. The warnings are "cannot find class
(factory) method" and "return type of 'stringWithBool:' defaults to
id." The
warnings go away only if I import the category or move the declaration
of
the category methods into the client (i.e., make it an informal
protocol)
.
But the application works correctly with or without the import.
Right. Casting, preprocessor, and extern object delarations aside,
headers are just for your conveniency, without them ALL WOULD WORK
PRECISELY THE SAME WAY.
Well, the CAPS is going too far. Except in cases where you thoroughly
understand the default typing/promotion rules of C, and the call is such
that it will be still OK under those rules, the compiler will not
generate correct code at call sites if it hasn't seen a prototype for a
method (the same as for a C function as well).
For example, consider
- (float)floatValue;
float x = [anObj floatValue];
If the compiler doesn't see a declaration for -floatValue, it will
assume that it returns id for a method (int for a function), and x is
going to get some unpreditable/undefined value. The same applies to
arguments to methods, and also the promotion rules for them. Floating
types are just one example of problematic cases.
Chris Kane
Cocoa Frameworks, Apple
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.