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.
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 mails | Author | Date |
|---|---|---|
| James Powell | Nov 12, 22:40 | |
| James Powell | Nov 13, 11:31 | |
| Andreas Mayer | Nov 13, 13:23 | |
| James Powell | Nov 13, 14:09 | |
| Andreas Mayer | Nov 13, 15:49 | |
| Cameron Hayne | Nov 13, 16:35 | |
| Jeff Disher | Nov 13, 16:52 | |
| Andreas Mayer | Nov 13, 17:16 | |
| Andreas Mayer | Nov 13, 17:45 | |
| Andreas Mayer | Nov 13, 17:53 | |
| James Powell | Nov 13, 18:11 | |
| Andy Lee | Nov 13, 20:08 | |
| Andreas Mayer | Nov 14, 06:49 | |
| Andy Lee | Nov 14, 08:30 | |
| Andreas Mayer | Nov 14, 13:17 |






Cocoa mail archive

