BSD

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • self

    9 answers - 276 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Am I right in thinking that self is a local (stack based) variable?
    for example the code:
    -(id) init {
    self = [self initSpecial];
    return self;
    }
    should alter everything in exactly the same way as:
    -(id) init {
    return [self initSpecial];
    }
  • No.1 | | 594 bytes | |

    6/8/05, Theodore H. Smith <delete (AT) elfdata (DOT) comwrote:

    Am I right in thinking that self is a local (stack based) variable?

    Yes and no. Self is actually a hidden function parameter. For
    instance, the method:
    -(id)foo
    {
    }

    Is really, under the hood, a function that looks something like:

    id foo(id self, SEL _cmd)
    {

    }

    for example the code:
    -(id) init {
    self = [self initSpecial];
    return self;
    }

    should alter everything in exactly the same way as:
    -(id) init {
    return [self initSpecial];
    }

    Yes.
  • No.2 | | 786 bytes | |

    Thats what I meant. Params are (theoretically) stored on the stack in
    C I don't know about in Cocoa.

    8 Jun 2005, at 16:13, Clark Cox wrote:

    6/8/05, Theodore H. Smith <delete (AT) elfdata (DOT) comwrote:
    >
    >>

    >Am I right in thinking that self is a local (stack based) variable?
    >>

    Yes and no. Self is actually a hidden function parameter. For
    instance, the method:

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.3 | | 370 bytes | |

    Le 8 juin 05 16:59, Theodore H. Smith a :

    Am I right in thinking that self is a local (stack based) variable?

    Yes, "self" is a local variable.

    for example the code:

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

    should alter everything in exactly the same way as:

    -(id) init {
    return [self initSpecial];
    }
  • No.4 | | 942 bytes | |

    6/8/05, Theodore H. Smith <delete (AT) elfdata (DOT) comwrote:

    Am I right in thinking that self is a local (stack based) variable?

    for example the code:
    -(id) init {
    self = [self initSpecial];
    return self;
    }

    should alter everything in exactly the same way as:
    -(id) init {
    return [self initSpecial];
    }

    The short answer is Yes, kind of.

    self is the hidden first parameter to the method and will be stored in
    a register on PPC. As with any parameter or other local variable
    assigning to it will have no effect outside the scope of the method
    and the two pieces of code are therefore equivalent.

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.5 | | 1308 bytes | |

    Jun 8, 2005, at 10:59 AM, Theodore H. Smith wrote:
    Am I right in thinking that self is a local (stack based) variable?

    It is an implicit argument to every method, so yes it is local (i.e.,
    its scope is the method) and yes it is stack-based. Technically you
    can treat it like any other method argument, including setting it to
    whatever value you want. You should almost never do this outside of
    an init method, because it can make your code confusing and error-
    prone to have "self" not be the original receiver of the method.
    Some people feel init methods are a special case because of their
    unique semantics -- hence this idiom:
    - (id)init {
    if ((self = [super init]))
    {
    //
    }

    return self;
    }

    for example the code:

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

    should alter everything in exactly the same way as:

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

    Yes, those two code examples have identical effects.

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.6 | | 1252 bytes | |

    Theodore,

    8.6.2005, at 16:59, Theodore H. Smith wrote:

    Am I right in thinking that self is a local

    Completely.

    (stack based) variable?

    Depends on how the compiler feels at the moment :) -- it might keep
    it in the register it comes in (for it actually is the first
    argument, $r3).

    At least, for today, whilst we still run on a decent architecture

    for example the code:

    -(id) init {
    self = [self initSpecial];
    return self;
    }
    should alter everything in exactly the same way as:
    -(id) init {
    return [self initSpecial];
    }

    Exactly. Note also that the former is *needed* in case of more
    complex initialization:
    -init {
    self=[self initSpecial]; // also may need to check for nil
    depending on the following code
    [self whatever];
    return self;
    }

    ada
    CSoftware: ocs (AT) ocs (DOT) cz http://www.ocs.cz
    private ondra (AT) ocs (DOT) cz http://www.ocs.cz/oc

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.7 | | 1077 bytes | |

    Jun 8, 2005, at 8:13 AM, Clark Cox wrote:

    >
    >for example the code:
    >>

    >-(id) init {
    >self = [self initSpecial];
    >return self;
    >}
    >>

    >should alter everything in exactly the same way as:
    >>

    >-(id) init {
    >return [self initSpecial];
    >}
    >>

    >

    Yes.

    Maybe.

    If initSpecial calls [super init], some supers discard the original
    object and return a new one.

    I am fairly certain; I haven't tested it so YMMV, but the
    NSDictionary returned by initWAndKeys is not the one you
    alloc'ed.
    -Mark
    Duck, duck, duck, duck, GSE!

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.8 | | 1346 bytes | |

    Jun 8, 2005, at 11:39 AM, Mark Lively wrote:
    Jun 8, 2005, at 8:13 AM, Clark Cox wrote:
    for example the code:

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

    should alter everything in exactly the same way as:

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


    >>

    >Yes.
    >>

    >

    Maybe.

    If initSpecial calls [super init], some supers discard the original
    object and return a new one.

    I think you're confusing this with a different init pitfall. The
    above two methods are in fact identical. In the first, *whatever* -
    initSpecial returns (even if it's not the original receiver) will be
    assigned to the local variable self, and that value will be
    returned. In the second, *whatever* -initSpecial returns will be
    returned immediately.

    You might be thinking of the mistake some people make when they
    separate the call to +alloc from the call to -init.

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.9 | | 1042 bytes | |

    6/8/05, Mark Lively <lively (AT) accuwx (DOT) comwrote:

    Jun 8, 2005, at 8:13 AM, Clark Cox wrote:

    >
    >for example the code:
    >>

    >-(id) init {
    >self = [self initSpecial];
    >return self;
    >}
    >>

    >should alter everything in exactly the same way as:
    >>

    >-(id) init {
    >return [self initSpecial];
    >}
    >>

    >

    Yes.

    Maybe.

    No, it is a definite YES

    If initSpecial calls [super init], some supers discard the original
    object and return a new one.

    I am fairly certain; I haven't tested it so YMMV, but the
    NSDictionary returned by initWAndKeys is not the one you
    alloc'ed.

    While this is correct, it is also irrelevant in this case. In both
    cases, whatever value is returned by initSpecial (whether it be the
    same object or not) is returned by init.

Re: self


max 4000 letters.
Your nickname that display:
In order to stop the spam: 6 + 5 =
QUESTION ON "BSD"

EMSDN.COM