Amazon.com Widgets Create a new object instead of using self?
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 18, 2014, 10:13:44 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 10 - More on Variables and Data Types
| | | |-+ Create a new object instead of using self?
Pages: [1] Go Down
Print
Author Topic: Create a new object instead of using self? (Read 934 times)
akiakic
Newbie
*
Posts: 1


Email




on: July 27, 2011, 11:00:16 AM

This is a great book but I am still having trouble understanding "self". If anyone could answer this it would be greatly appreciated.

Is this:

Code: (Objective-C)
- (Fraction *) initWith: (int) n: (int) d
{
    Fraction *abc; //self is same as abc object created here?
   
    abc = [super init];
    if (abc)
    {
        [abc setX: n andY: d];
    }
   
    return abc;
}
the same as this:

Code: (Objective-C)
- (Fraction *) initWith: (int) n: (int) d
{
   
    self = [super init];
    if (self)
    {
        [self setX: n andY: d];
    }
   
    return self;
}
Logged
dharr19
Full Member
***
Posts: 175


Email




Reply #1 on: August 07, 2011, 09:07:45 PM

Hi,

Hi,

This is the init and initWith methods I used in the Fraction class


Code: (Objective-C)
-(id) init
{
    return [self initWith:0 over:0];
}

-(id) initWith: (int) n over: (int) d;
{
    self = [super init];
    
    if ([self isKindOfClass: [NSObject class]] == YES)
        NSLog(@"self is a kind of NSObject class");
    
    if ([self isMemberOfClass:[Fraction class]] == YES)
        NSLog(@"self is a member of Fraction class");
    
    if (self)
        [self setTo:n over:d];
    
    return self;
}

output


2011-08-08 00:07:14.673 FractionTest[1147:707] self is a kind of NSObject class
2011-08-08 00:07:15.269 FractionTest[1147:707] self is a member of Fraction class
Last Edit: August 07, 2011, 09:38:59 PM by dharr19 Logged
seerex
Full Member
***
Posts: 177


Email




Reply #2 on: September 02, 2011, 01:15:46 PM

An old threat i know, but i thought i'd answer the question if anyone like me stumbled upon it.

No, the object you create with abc is NOT the same as self. self is the receiver of the message, the message invoker, the message caller.  For instance, consider this in a main routine:
Code: (Objective-C)
[myObj callMyMethod];

Here, myObj is the receiver / message invoker, meaning, that if you wrote "self" in the callMyMethod declaration / implementation, you would refer to myObj.

Hope it makes sense.
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.