Weird Apple Sample code

  • Hi,

    I stumpled over weird Apple sample code. It is in the CocoaHTTPServer
    example project (file: HTTPServer.m:7) of an Xcode 3 installation. Can
    someone give an explanation what this is doing?

    @implementation HTTPServer

    - (id)init {
        connClass = [HTTPConnection self];
        return self;
    }

    ....

    Obviously not returning the class you ask for is evil IMHO , but I
    could live with it if I understood the rest of that method. There's
    more (line 56):

    @implementation HTTPConnection

    - (id)init {
        [self dealloc];
        return nil;
    }

    ....

    The sample code doesn't seem to work anyway, so maybe someone just
    committed a broken edit to the repository?

    Another question is: Should Apple really publish example code that
    looks like this?

    Roddi
  • On Dec 13, 2007 4:38 AM, Ruotger Skupin <roddi...> wrote:
    > Hi,
    >
    > I stumpled over weird Apple sample code. It is in the CocoaHTTPServer
    > example project (file: HTTPServer.m:7) of an Xcode 3 installation. Can
    > someone give an explanation what this is doing?
    >
    > @implementation HTTPServer
    >
    > - (id)init {
    > connClass = [HTTPConnection self];
    > return self;
    > }

    It's simply setting the connClass to HTTPConnection, the first time
    any instance of HTTPServer is created. If I were doing this, I'd
    probably do in +initialize instead:

    @implementation HTTPServer

    +(void)initialize {
        connClass = [HTTPConnection self];
    }
    ...

    > Obviously not returning the class you ask for is evil IMHO , but I
    > could live with it if I understood the rest of that method.

    They're not returning anything different than what was asked for.

    > There's more (line 56):
    >
    > @implementation HTTPConnection
    >
    > - (id)init {
    > [self dealloc];
    > return nil;
    > }
    >

    This ensures that HTTPConnection cannot be instantiated via
    [[HTTPConnection alloc] init]. This requires clients of the class to
    call the other initializer
    (-initWithPeerAddress:inputStream:outputStream:forServer:)

    > ....
    >
    > The sample code doesn't seem to work anyway, so maybe someone just
    > committed a broken edit to the repository?
    >
    > Another question is: Should Apple really publish example code that
    > looks like this?

    That said, this code does look a bit sloppy for my tastes. Please file a bug.

    --
    Clark S. Cox III
    <clarkcox3...>
  • Hi,

    after some debugging I understood what goes on. (Teaches me not to
    shoot so fast ;-) )

    The init method should look like this:

    - (id) init {
    if(![super init]) // init my super class
      return nil;

    connClass = [HTTPConnection class]; // get the Class
    return self;
    }

    since -[HTTPServer init] does not return an HTTPConnection object as I
    thought -[HTTPConnection init] refusing to return something else than
    nil makes sense.

    Sorry to bother you, though my last original question still holds.

    Roddi

    P.S.: Before complaining that I don't do self = [super init] read
    this: http://www.wilshipley.com/blog/2005/07/self-stupid-init.html

    Am 13.12.2007 um 13:38 schrieb Ruotger Skupin:

    > Hi,
    >
    > I stumpled over weird Apple sample code. It is in the
    > CocoaHTTPServer example project (file: HTTPServer.m:7) of an Xcode 3
    > installation. Can someone give an explanation what this is doing?
    >
    > @implementation HTTPServer
    >
    > - (id)init {
    > connClass = [HTTPConnection self];
    > return self;
    > }
    >
    > ....
    >
    > Obviously not returning the class you ask for is evil IMHO , but I
    > could live with it if I understood the rest of that method. There's
    > more (line 56):
    >
    > @implementation HTTPConnection
    >
    > - (id)init {
    > [self dealloc];
    > return nil;
    > }
    >
    > ....
    >
    > The sample code doesn't seem to work anyway, so maybe someone just
    > committed a broken edit to the repository?
    >
    > Another question is: Should Apple really publish example code that
    > looks like this?
    >
    > Roddi
  • On Dec 13, 2007 7:38 AM, Ruotger Skupin <roddi...> wrote:

    > Obviously not returning the class you ask for is evil IMHO

    I realize that the code you asked about isn't actually doing this, but I
    just wanted to add that returning a different class from -init is fairly
    common and not at all evil. Class clusters do it all the time - when you
    call [[NSMutableArray alloc] init], what you get back is *not* an instance
    of NSMutableArray, it's an instance of a private subclass of NSArray.

    The "normal" contract in OO programming is that, for a method that is
    documented to return class Foo, what you'll get back may be an instance of
    either Foo or a subclass of Foo. Because of polymorphism, your code doesn't
    need to know or care which is the case.

    sherm--
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today