Amazon.com Widgets Chapter 10 - initialize self using super
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 18, 2013, 04:44:24 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
| |-+  Parts 1 and 2
| | |-+  Questions and Discussion
| | | |-+  Chapter 10 - initialize self using super
Pages: [1]   Go Down
Print
Author Topic: Chapter 10 - initialize self using super  (Read 532 times)
seerex
Full Member
***
Posts: 177


Email




« on: June 19, 2011, 08:53:22 AM »

Hello fellow objective-c apprentices Smiley

I just started reading chapter 10, and the first example of a code i stumble upon is the one where you create a Fraction method that initializes and sets the instance of a Fraction. I have a question though.

You create this method
Code: (Objective-C)
-(Fraction *) initWith: (int) n: (int) d {
    self = [super init];
   
    if (self)
        [self setTo: n over: d];
   
    return self;
}

And according to my understandings what it does is that it initializes the parent class(s) and assigns em to self, hence self gets initialized aswell.

After that if it returns a non-zero value meaning the initialization was completed properly, it sets the instance variables of self.

And then returns self.

I have 2 questions regarding this matter.
1. "self" is the message caller / invoker, right? and when using a = [method] a is the one getting the received Fraction object hence of the return of a Fraction object, correct?

2. The book gets you to write this code
Code: (Objective-C)
b = [[Fraction alloc] initWith: 3 : 7];

From my point of view, it simple calls 2 methods right? the inherited alloc method and the newly created init method that sets the values of numerator and denominator. So this is actually 2 methods combined on 1 line, correct? hence the same result would apply if you did
Code: (Objective-C)
b = [Fraction alloc];
b = [initWith: 3 : 7];

Although it does not? and since the initWith is just a plain method like the print and others, why doesnt this apply? i'm assuming it must have something to do with it being initialized and you cant use methods if it isn't initialized, however, thats what you do here before it assigns the values, correct?

and to get a bit more confusing, but i guess this question is answer simultaneously with the above, but i also tried doing this
Code: (Objective-C)
a = [[[Fraction alloc] initWith: 1 : 3]printAndConvert];

obviously doesnt work either Smiley

It is properly explained in the book and maybe i just missed it or simply forgot it, coz english isnt my native language and on top of that its my very first programming language so im struggling both to learn and also to read and understand some of the more "complex" sentences.

Thank you for taking your time to read this long and tedious post, but your effort in helping me out is greatly appreciated.

Best regards
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)
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #1 on: June 19, 2011, 09:30:55 AM »

The two line version would look like this:

Code: (Objective-C)

b = [Fraction alloc];  
b = [b  initWith: 3 : 7];  

since you want to take the result of the alloc and send it the initWith:: message.

For the second case, you don't return a value from your printAndConvert method, so you would write

Code: (Objective-C)
 [[[Fraction alloc] initWith: 1 : 3]printAndConvert];  

Hope this helps.  Note that this example has a memory leak since you don't save the reference to the allocated Fraction object and therefore can't release it.

 Cheers,

Steve
Logged
seerex
Full Member
***
Posts: 177


Email




« Reply #2 on: June 19, 2011, 11:33:51 AM »

Evening sir.

Thanks a lot, it clears it up Smiley i'll get my head back in the book Smiley when i'm done i'll order the third edition so i cant wait. Really good book and great forum!

best regards
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.