Skip navigation.
 
mlRe: numberWithInt (was Re: NSForm validation)
FROM : Andreas Mayer
DATE : Wed Nov 13 17:16:02 2002

Am Donnerstag, 14.11.02 um 01:34 Uhr schrieb Cameron Hayne:

> But what is missing is the documentation. The behaviour seems to be an
> unstated convention rather than something that is necessarily true.


I suppose you are right. The only thing I found was this:

http://developer.apple.com/techpubs/macosx/Cocoa/TasksAndConcepts/
ProgrammingTopics/MemoryMgmt/Tasks/AllocInitObjects.html

"In addition, classes frequently define "factory" methods - convenience 
class methods - that allocate and initialize instances for the 
receiver."

> Even
> using AppKiDo I fail to see anything that says that the method 
> numberWithInt
> that NSDecimalNumber inherits from NSNumber returns an NSDecimalNumber.


Well, I already explained why it does.
You can tell that it is such a 'convenience class method', simply by 
looking at it's name.
Any method of class NSXxx named xxxWithWhatever: is a convenience 
constructor for that class.

> What if I created a class with a method like:
> + (MyWeirdClass *)objectWithInt:(int)value
> {
>    return (MyWeirdClass*) [NSNumber numberWithInt: value];
> }
> then if I created a subclass, MyVeryWeirdClass, would the expression
> [MyVeryWeirdClass objectWithInt:42] somehow magically be of type
> (MyVeryWeirdClass *)  ?


No. You explicitly created an NSNumber object; this will not magically 
be transformed into anything else.
No problem though, since it is not named 'myWeirdWith...:'.

Assuming MyWeirdClass is a subclass of NSNumber (no point in this 
example otherwise), for a convenience constructor you would write:

+ (MyWeirdClass *)myWeirdWithInt:(int)value
{
    return [MyWeirdClass numberWithInt: value];
}

Then, [MyVeryWeirdClass myWeirdWithInt:] would return a 
MyVeryWeirdClass object.

(Of course, that example does nothing useful, since it just provides 
another name for an existing method.)


bye.  Andreas.


Related mailsAuthorDate
mlNSForm validation James Powell Nov 12, 22:40
mlRe: NSForm validation James Powell Nov 13, 11:31
mlRe: NSForm validation Andreas Mayer Nov 13, 13:23
mlRe: NSForm validation James Powell Nov 13, 14:09
mlRe: NSForm validation Andreas Mayer Nov 13, 15:49
mlRe: numberWithInt (was Re: NSForm validation) Cameron Hayne Nov 13, 16:35
mlRe: numberWithInt (was Re: NSForm validation) Jeff Disher Nov 13, 16:52
mlRe: numberWithInt (was Re: NSForm validation) Andreas Mayer Nov 13, 17:16
mlRe: numberWithInt (was Re: NSForm validation) Andreas Mayer Nov 13, 17:45
mlRe: numberWithInt (was Re: NSForm validation) Andreas Mayer Nov 13, 17:53
mlRe: numberWithInt (was Re: NSForm validation) James Powell Nov 13, 18:11
mlRe: numberWithInt (was Re: NSForm validation) Andy Lee Nov 13, 20:08
mlRe: numberWithInt (was Re: NSForm validation) Andreas Mayer Nov 14, 06:49
mlRe: numberWithInt (was Re: NSForm validation) Andy Lee Nov 14, 08:30
mlRe: numberWithInt (was Re: NSForm validation) Andreas Mayer Nov 14, 13:17