Re: Class Factory Methods?
Re: Class Factory Methods?
- Subject: Re: Class Factory Methods?
- From: "I. Savant" <email@hidden>
- Date: Mon, 22 Jun 2009 15:23:28 -0400
You are confused because you are guessing and not verifying. Let's
examine:
On Jun 22, 2009, at 3:02 PM, Chunk 1978 wrote:
"new", "alloc", initWithFormat" (or "stringWithFormat") are class
factory methods that are declared methods inherited by my custom
classes, i guess somewhere in NSObject?
Computer programming is not "guessing". The documentation tells you
precisely where these methods are declared as well as the lineage of
the classes (which tells you whether a class inherits these methods).
Both -initWithFormat: and +stringWithFormat: are methods (instance
and class, respectively) of NSString. The "new" and "alloc" methods
belong to NSObject. This information is easily verified in the class
reference documentation and even discoverable in a few seconds with
Google.
DO YOUR RESEARCH. DO NOT GUESS.
so it write a stringWIthFormat class method, i guess to override it
for the custome class it's written within:
-=-=-=-=-
+ (NSString *)stringWithFormat
{
return @"newBar";
}
-=-=-=-=-
Okay, so at this point you have a subclass. Of what? NSObject?
NSString? NSRandomObjectWithMysteriousOrigins?
This is directly relevant to your question.
assuming both these methods are written in the same custom class, does
that mean that my first method "(void)myMethod" will not allocate and
initiate a new stringWithFormat of @"bar" and output it to the console
with NSLog(), but will output @"newBar" to the console?
It's difficult to answer your question because things are a bit
muddled here. The outcome of your test depends on a few things you
haven't defined.
First, your -myMethod is calling the *instance* method -
initWithFormat: of an allocated instance of NSString. You're
specifying that method of that particular class, so that's what gets
called.
Second (related to the first), why would you expect
+stringWithFormat of your subclass (of questionable parentage) if
you're not calling that method on that class?
--
I.S.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden