Amazon.com Widgets Exercise 13.4 Help needed to understand..
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 04:39:38 AM
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
|-+  Programming in Objective-C, 4th edition
| |-+  Exercises
| | |-+  Chapter 13
| | | |-+  Exercise 13.4 Help needed to understand..
Pages: [1]   Go Down
Print
Author Topic: Exercise 13.4 Help needed to understand..  (Read 292 times)
nico75r6
Newbie
*
Posts: 1






« on: June 21, 2012, 06:19:14 AM »

Hello guys,

I found this solution to exercise 13.4:


  
Code: (Objective-C)
  Fraction *add(Fraction *array[], int lenght)    
    {    
        Fraction *result = [[Fraction alloc] init];    
        [result setTo:0 over:1];    
        Fraction *temp;    
          
        for(int i=0; i<15;++i)  
        {    
            temp = [result add: array[i]];    
            [result release];    
            result = temp;    
        }  
            
        return result;    
    }  



It works but...   Huh Huh Huh ... I would like someone please explain to me:

1) why you need to use a variable "temp" instead of incrementing directly "result" ;
2) why "temp" is of the same (Fraction *) type as "result" but you do not have to write the [[Fraction alloc] init] part for it as you do fot the "result". I have a VB/C# background, just started with Objective-c.

NOTE: I am using XCode 4.3 and I have NOT ENABLED ARC because I want to get used to manage the memeory by myself.

In fact the **not right working** code I wrote before I browsed for the solution was:


 
Code: (Objective-C)
   Fraction *add(Fraction *array[], int lenght)    
    {    
        Fraction *result = [[Fraction alloc] init];    
        [result setTo:0 over:1];    
          
        for(int i=0; i<15;++i)  
        {      
            result = [result add: array[i]];    
        }    
            
        return result;    
    }    

This would have worked in C#, without the need of the "temp".

Thanks

Nicola
« Last Edit: June 21, 2012, 11:00:14 AM by nico75r6 » Logged
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.