Amazon.com Widgets Exercise 7.2
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 24, 2014, 12:42:46 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
|-+ Old Stuff
| |-+ Answers to Exercises
| | |-+ Chapter 7
| | | |-+ Exercise 7.2
Pages: 1 [2] 3 Go Down
Print
Author Topic: Exercise 7.2 (Read 8713 times)
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #15 on: February 28, 2010, 09:51:35 AM

Jack,

The declaration of toReduce in the interface section should also match the declaration in the implementation section. When you write your method like this:

Code: (Objective-C)
-(void) print: (BOOL) toReduce
{
     ...
      if (toReduce == YES)
         ...
}
the reference to toReduce in the method refers to the argument passed into the method.  When you write

Code: (Objective-C)
[resultFraction print: YES];

in main, the argument to the method, the value YES, is effectively copied into the variable named toReduce when the method starts executing.  That's the name you gave to the argument (also known as the formal parameter name) in the first line of your method definition.  

The variable toReduce that you declared in main is local variable, and is not needed per your example.  It can only be referenced by name inside the main function.  This concept of variable scope is explained in detail in a later chapter.

Cheers,

Steve Kochan

Last Edit: March 01, 2010, 05:39:00 PM by skochan Logged
jocko757
Newbie
*
Posts: 12


Email




Reply #16 on: March 01, 2010, 04:01:38 PM

Thanks Steve, that clears it up.
Logged
marks
Newbie
*
Posts: 12






Reply #17 on: April 19, 2010, 10:17:46 AM

After reading the post from this thread , I did some modification it ran but have 2 warnings, I'd spent a long time still don't know where went wrong.. Hope someone can help me, thanks a lot!

I got this 2 warnings.
Quote
! Incomplete Implementation of class 'Fraction'

! Method definition for '-print' not found


Fraction.h (Interface)
Code: (Objective-C)
#import <Foundation/Foundation.h>

// The Fraction class

@interface Fraction : NSObject {
int numerator;
int denominator;
}

@property int numerator, denominator;

-(void) print: (BOOL) simplfy;
-(double) convertToNum;
-(void) setTo: (int) n over: (int) d;
-(void) reduce;

// Add argument to recevier
-(Fraction *) add: (Fraction *) f;

// Subtract argument from receiver
-(Fraction *) subtract: (Fraction *) f;

// Multiply receiver by argument
-(Fraction *) multiply: (Fraction *) f;

// Divide receiver by argument
-(Fraction *) divide: (Fraction *) f;

@end


Fraction.m (Implementation)
Code: (Objective-C)
#import "Fraction.h"


@implementation Fraction

@synthesize numerator, denominator;


-(void) print: (BOOL) simplfy
{
if (simplfy == YES) {
Fraction *result = [[Fraction alloc] init];
[result setTo: numerator over: denominator];
[result reduce];
NSLog (@"Reduced %i/%i", result.numerator, result.denominator);
[result release];
} else {
NSLog (@"%i/%i", numerator, denominator);
}
}

-(double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return 1.0;
}

-(void) setTo: (int) n over: (int) d
{
numerator = n;
denominator = d;
}

-(void) reduce
{
int u = numerator;
int v = denominator;
int temp;

while (v != 0) {
temp = u % v;
u = v;
v = temp;
}

numerator /= u;
denominator /= u;
}

// add b Fraction to a Fraction to resultFraction
-(Fraction *) add: (Fraction *) f
{
// To add two fractions:
// a/b + c/d = ((a*d) + (c*d)) / (b * d)

// result will store the result of the addition
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = numerator * f.denominator + denominator * f.numerator;
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];
//[result reduce];

return result;
}

// Subtract argument from receiver
-(Fraction *) subtract: (Fraction *) f
{
// To subtract two fractions:
// a/b - c/d = ((a*d) - (c*d)) / (b * d)

// result will store the result of the addition
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = numerator * f.denominator - denominator * f.numerator;
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];
//[result reduce];

return result;
}

// Multiply receiver by argument
-(Fraction *) multiply: (Fraction *) f
{
// To multiply two fractions:
// a/b * c/d = (a * c) / (b * d)

// result will store the result of the addition
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = numerator * f.numerator;
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];
//[result reduce];

return result;
}

// Divide receiver by argument
-(Fraction *) divide: (Fraction *) f
{
// To divide two fractions:
// a/b / c/d = (a * d)) / (b * c)

// result will store the result of the addition
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = numerator * f.denominator;
resultDenom = denominator * f.numerator;

[result setTo: resultNum over: resultDenom];
//[result reduce];

return result;
}

@end


Main
Code: (Objective-C)
#import "Fraction.h"

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

Fraction *aFraction = [[Fraction alloc] init];
Fraction *bFraction = [[Fraction alloc] init];
Fraction *resultFraction;

[aFraction setTo: 5 over: 6];
[bFraction setTo: 6 over: 8];

resultFraction = [aFraction add: bFraction];
[resultFraction print: YES];
[resultFraction release];

resultFraction = [aFraction subtract: bFraction];
[resultFraction print: NO];
[resultFraction release];

resultFraction = [aFraction multiply: bFraction];
[resultFraction print: YES];
[resultFraction release];

resultFraction = [aFraction divide: bFraction];
[resultFraction print: NO];
[resultFraction release];
 
[aFraction release];
[bFraction release];
   
[pool drain];
    return 0;
}

Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #18 on: April 19, 2010, 07:33:09 PM

It seems like it's using an older version of your interface section that referenced a print method.  Try doing a Build > Clean All Targets and rebuilding.  Don't know if that will fix your problem.

Cheers,

Steve Kochan
Logged
marks
Newbie
*
Posts: 12






Reply #19 on: April 20, 2010, 07:00:08 AM

Hi Steve,

After recreate the exercise again, now it worked.

Thank you!
Logged
tadej5553
Full Member
***
Posts: 145


Email




Reply #20 on: April 21, 2010, 09:28:36 AM

The variables that are used as arguments in methods are decared as arguments (when you declare the method)
and don't have to be declared eveywhere else.

Note that this isn't the same variable all the time, a new one is created from the argument's value (you are only passing the value, not the variable as the argument, as you will see later).
Logged
tdilenarda
Newbie
*
Posts: 9

tdilenarda@hotmail.fr tdilenarda@mac.c




Reply #21 on: April 22, 2010, 04:47:37 AM

Here's my version.

It's working, but I'm sure my code could be simplified. Tried a few things but it didn't work anymore after that...

Fraction.h
Code: (Objective-C)
#import <Foundation/Foundation.h>

// Define the Fraction class

@interface Fraction : NSObject

{
int numerator;
int denominator;
}

@property int numerator, denominator;

-(void) print : (BOOL) shouldBeReduced;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
-(Fraction *) add: (Fraction *) f;
-(Fraction *) subtract: (Fraction *) f;
-(Fraction *) multiply: (Fraction *) f;
-(Fraction *) divide: (Fraction *) f;
-(void) reduce;

@end

Fraction.m
Code: (Objective-C)
#import "Fraction.h"

@implementation Fraction

@synthesize numerator, denominator;

-(void) print : (BOOL) shouldBeReduced
{
if (shouldBeReduced)
{
Fraction *tempFraction = [[Fraction alloc] init];
[tempFraction setTo: numerator over: denominator];

[tempFraction reduce];

if (tempFraction.denominator == 1 )
NSLog(@"%i", tempFraction.numerator);
else if (tempFraction.numerator == 0)
NSLog(@"0");
else
NSLog(@"%i / %i", tempFraction.numerator, tempFraction.denominator);
[tempFraction release];
}
else
{
if (numerator == 0)
NSLog(@"0");
else if (denominator == 1 )
NSLog(@"%i", numerator);
else
NSLog(@"%i / %i", numerator, denominator);
}
}

-(void) setTo: (int) n over: (int) d
{
numerator = n;
denominator = d;
}

-(double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return 1.0;
}

-(Fraction *) add: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = numerator * f.denominator + denominator * f.numerator;
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];

return result;
}

-(Fraction *) subtract: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = numerator * f.denominator - denominator * f.numerator;
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];

return result;
}

-(Fraction *) multiply: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = numerator * f.numerator;
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];

return result;
}

-(Fraction *) divide: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = numerator * f.denominator;
resultDenom = denominator * f.numerator;

[result setTo: resultNum over: resultDenom];

return result;
}

-(void) reduce
{
int u = numerator;
int v = denominator;
int temp;

while (v != 0)
{
temp = u % v;
u = v;
v = temp;
}

numerator /= u;
denominator /= u;
}
@end

Main.m
Code: (Objective-C)
#import "Fraction.h"

int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

Fraction *aFraction = [[Fraction alloc] init];
Fraction *bFraction = [[Fraction alloc] init];
Fraction *theResult;

[aFraction setTo: 3 over: 5];
[bFraction setTo: 1 over: 10];

// ADD

theResult = [aFraction add: bFraction];
NSLog(@"Adding");
[aFraction print: NO];
NSLog(@"and");
[bFraction print: NO];
NSLog(@"=");
[theResult print: NO];
NSLog(@"or could be reduced to :");
[theResult print: YES];
[theResult release];

// SUBTRACT

NSLog(@"  ");
theResult = [aFraction subtract: bFraction];
NSLog(@"Subtracting");
[bFraction print: NO];
NSLog(@"from");
[aFraction print: NO];
NSLog(@"=");
[theResult print: NO];
NSLog(@"or could be reduced to :");
[theResult print: YES];
[theResult release];

// MULTIPLY

NSLog(@"  ");
theResult = [aFraction multiply: bFraction];
NSLog(@"Multiplying");
[aFraction print: NO];
NSLog(@"with");
[bFraction print: NO];
NSLog(@"=");
[theResult print: NO];
NSLog(@"or could be reduced to :");
[theResult print: YES];
[theResult release];

// DIVIDING

NSLog(@"  ");
theResult = [aFraction divide: bFraction];
NSLog(@"Dividing");
[aFraction print: NO];
NSLog(@"by");
[bFraction print: NO];
NSLog(@"=");
[theResult print: NO];
NSLog(@"or could be reduced to :");
[theResult print: YES];
[theResult release];

[aFraction release];
[bFraction release];

[pool drain];
return 0;
}

Here's my log :
Quote
run
[Switching to process 2290]
Running…
2010-04-22 13:38:10.146 Chap7_Ex.2[2290:a0f] Adding
2010-04-22 13:38:10.151 Chap7_Ex.2[2290:a0f] 3 / 5
2010-04-22 13:38:10.152 Chap7_Ex.2[2290:a0f] and
2010-04-22 13:38:10.153 Chap7_Ex.2[2290:a0f] 1 / 10
2010-04-22 13:38:10.154 Chap7_Ex.2[2290:a0f] =
2010-04-22 13:38:10.154 Chap7_Ex.2[2290:a0f] 35 / 50
2010-04-22 13:38:10.155 Chap7_Ex.2[2290:a0f] or could be reduced to :
2010-04-22 13:38:10.155 Chap7_Ex.2[2290:a0f] 7 / 10
2010-04-22 13:38:10.156 Chap7_Ex.2[2290:a0f]   
2010-04-22 13:38:10.157 Chap7_Ex.2[2290:a0f] Subtracting
2010-04-22 13:38:10.158 Chap7_Ex.2[2290:a0f] 1 / 10
2010-04-22 13:38:10.158 Chap7_Ex.2[2290:a0f] from
2010-04-22 13:38:10.159 Chap7_Ex.2[2290:a0f] 3 / 5
2010-04-22 13:38:10.159 Chap7_Ex.2[2290:a0f] =
2010-04-22 13:38:10.160 Chap7_Ex.2[2290:a0f] 25 / 50
2010-04-22 13:38:10.160 Chap7_Ex.2[2290:a0f] or could be reduced to :
2010-04-22 13:38:10.161 Chap7_Ex.2[2290:a0f] 1 / 2
2010-04-22 13:38:10.161 Chap7_Ex.2[2290:a0f]   
2010-04-22 13:38:10.162 Chap7_Ex.2[2290:a0f] Multiplying
2010-04-22 13:38:10.162 Chap7_Ex.2[2290:a0f] 3 / 5
2010-04-22 13:38:10.164 Chap7_Ex.2[2290:a0f] with
2010-04-22 13:38:10.164 Chap7_Ex.2[2290:a0f] 1 / 10
2010-04-22 13:38:10.165 Chap7_Ex.2[2290:a0f] =
2010-04-22 13:38:10.165 Chap7_Ex.2[2290:a0f] 3 / 50
2010-04-22 13:38:10.167 Chap7_Ex.2[2290:a0f] or could be reduced to :
2010-04-22 13:38:10.167 Chap7_Ex.2[2290:a0f] 3 / 50
2010-04-22 13:38:10.168 Chap7_Ex.2[2290:a0f]   
2010-04-22 13:38:10.169 Chap7_Ex.2[2290:a0f] Dividing
2010-04-22 13:38:10.169 Chap7_Ex.2[2290:a0f] 3 / 5
2010-04-22 13:38:10.170 Chap7_Ex.2[2290:a0f] by
2010-04-22 13:38:10.170 Chap7_Ex.2[2290:a0f] 1 / 10
2010-04-22 13:38:10.171 Chap7_Ex.2[2290:a0f] =
2010-04-22 13:38:10.171 Chap7_Ex.2[2290:a0f] 30 / 5
2010-04-22 13:38:10.172 Chap7_Ex.2[2290:a0f] or could be reduced to :
2010-04-22 13:38:10.172 Chap7_Ex.2[2290:a0f] 6


Debugger stopped.
Program exited with status value:0.
Last Edit: April 22, 2010, 05:38:21 AM by tdilenarda Logged

MBP 17" C2D 2.33. 3 GB RAM - Seagate 500 GB @ 7200 rpm.
iPhone 3GS 32 GB Black @ Orange
tdilenarda
Newbie
*
Posts: 9

tdilenarda@hotmail.fr tdilenarda@mac.c




Reply #22 on: April 22, 2010, 05:34:52 AM

I'd like to add a message if the fraction can't be reduced.

I added another property to Fraction.h :
Code: (Objective-C)
@property BOOL canBeReduced;

and modified the '@synthesize' in Fraction.m
Code: (Objective-C)
@synthesize numerator, denominator, canBeReduced;

I then modified the 'reduce' method:
Code: (Objective-C)
-(void) reduce
{
int u = numerator;
int v = denominator;
int temp;

while (v != 0)
{
temp = u % v;
u = v;
v = temp;
}

if (u != 1)
{
numerator /= u;
denominator /= u;
canBeReduced = YES;
}
else
{
canBeReduced = NO;
NSLog(@"This fraction can't be reduced.");
}
}

And added the condition in the 'print' method :
Code: (Objective-C)
-(void) print : (BOOL) shouldBeReduced
{
if (shouldBeReduced)
{
Fraction *tempFraction = [[Fraction alloc] init];
[tempFraction setTo: numerator over: denominator];

[tempFraction reduce];

if (canBeReduced == YES)
{
if (tempFraction.denominator == 1 )
NSLog(@"%i", tempFraction.numerator);
else if (tempFraction.numerator == 0)
NSLog(@"0");
else
NSLog(@"%i / %i", tempFraction.numerator, tempFraction.denominator);
}

[tempFraction release];
}
else
{
if (numerator == 0)
NSLog(@"0");
else if (denominator == 1 )
NSLog(@"%i", numerator);
else
NSLog(@"%i / %i", numerator, denominator);
}
}

The message displays correctly if the fraction can't be reduced, but if it can, the reduced fraction doesn't display.
I've been trying to find out why for the past hour, but no luck so far... Can someone help ?

Here's my log :
Quote
run
[Switching to process 2733]
2010-04-22 14:30:44.062 Chap7_Ex.2[2733:a0f] Adding
2010-04-22 14:30:44.069 Chap7_Ex.2[2733:a0f] 3 / 5
2010-04-22 14:30:44.071 Chap7_Ex.2[2733:a0f] and
2010-04-22 14:30:44.073 Chap7_Ex.2[2733:a0f] 1 / 10
2010-04-22 14:30:44.074 Chap7_Ex.2[2733:a0f] =
2010-04-22 14:30:44.074 Chap7_Ex.2[2733:a0f] 35 / 50
2010-04-22 14:30:44.075 Chap7_Ex.2[2733:a0f] or could be reduced to :
2010-04-22 14:30:44.076 Chap7_Ex.2[2733:a0f]   
2010-04-22 14:30:44.077 Chap7_Ex.2[2733:a0f] Subtracting
2010-04-22 14:30:44.078 Chap7_Ex.2[2733:a0f] 1 / 10
2010-04-22 14:30:44.079 Chap7_Ex.2[2733:a0f] from
2010-04-22 14:30:44.079 Chap7_Ex.2[2733:a0f] 3 / 5
2010-04-22 14:30:44.080 Chap7_Ex.2[2733:a0f] =
2010-04-22 14:30:44.081 Chap7_Ex.2[2733:a0f] 25 / 50
2010-04-22 14:30:44.081 Chap7_Ex.2[2733:a0f] or could be reduced to :
2010-04-22 14:30:44.084 Chap7_Ex.2[2733:a0f]   
2010-04-22 14:30:44.085 Chap7_Ex.2[2733:a0f] Multiplying
2010-04-22 14:30:44.086 Chap7_Ex.2[2733:a0f] 3 / 5
2010-04-22 14:30:44.086 Chap7_Ex.2[2733:a0f] with
2010-04-22 14:30:44.087 Chap7_Ex.2[2733:a0f] 1 / 10
2010-04-22 14:30:44.088 Chap7_Ex.2[2733:a0f] =
2010-04-22 14:30:44.088 Chap7_Ex.2[2733:a0f] 3 / 50
2010-04-22 14:30:44.089 Chap7_Ex.2[2733:a0f] or could be reduced to :
2010-04-22 14:30:44.089 Chap7_Ex.2[2733:a0f] This fraction can't be reduced.
2010-04-22 14:30:44.090 Chap7_Ex.2[2733:a0f]   
2010-04-22 14:30:44.091 Chap7_Ex.2[2733:a0f] Dividing
2010-04-22 14:30:44.091 Chap7_Ex.2[2733:a0f] 3 / 5
2010-04-22 14:30:44.093 Chap7_Ex.2[2733:a0f] by
2010-04-22 14:30:44.094 Chap7_Ex.2[2733:a0f] 1 / 10
2010-04-22 14:30:44.094 Chap7_Ex.2[2733:a0f] =
2010-04-22 14:30:44.101 Chap7_Ex.2[2733:a0f] 30 / 5
2010-04-22 14:30:44.101 Chap7_Ex.2[2733:a0f] or could be reduced to :

Running…

Debugger stopped.
Program exited with status value:0.
Last Edit: April 22, 2010, 05:39:49 AM by tdilenarda Logged

MBP 17" C2D 2.33. 3 GB RAM - Seagate 500 GB @ 7200 rpm.
iPhone 3GS 32 GB Black @ Orange
jdelamater
Newbie
*
Posts: 3






Reply #23 on: April 23, 2010, 08:36:57 PM

My goal in this was to complete this assigment as simply as possible. So, here is my solution...

Fraction.h snippet (@interface)
Code: (Objective-C)
...
-(void) print: (BOOL) simplify;
...

Fraction.m snippet (@implementation)
Code: (Objective-C)
...
-(void) print: (BOOL) simplify {
Fraction *tempFrac = [[Fraction alloc] init];
[tempFrac setTo: numerator over: denominator];

if (simplify) { tempFrac.reduce; }

NSLog (@"%i/%i", tempFrac.numerator, tempFrac.denominator);
}
...

FractionTest.m snippet (usage)
Code: (Objective-C)
...
[resultFraction print: 1];
[resultFraction print: 0];
...

Output
Code: (Objective-C)
2010-04-23 23:31:24.799 FractionTest[13835:a0f] 1/4
2010-04-23 23:31:24.803 FractionTest[13835:a0f] 2/8
2010-04-23 23:31:24.804 FractionTest[13835:a0f] +
2010-04-23 23:31:24.805 FractionTest[13835:a0f] 1/2
2010-04-23 23:31:24.807 FractionTest[13835:a0f] 2/4
2010-04-23 23:31:24.808 FractionTest[13835:a0f] =
2010-04-23 23:31:24.809 FractionTest[13835:a0f] 3/4
2010-04-23 23:31:24.810 FractionTest[13835:a0f] 24/32
Last Edit: April 23, 2010, 08:39:00 PM by jdelamater Logged
Leik
Newbie
*
Posts: 1






Reply #24 on: May 12, 2010, 04:00:23 PM

Hi, Im trying to do this exercise but asking if the user wants to reduce the fraction or not, but i have problems to read the scanf ("%B", &choice), where choice is a BOOL and im using [print: choice] but the console do not stop to scan, just pass trough it and choice=Yes.

What may i do to solve it?

TYVM
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #25 on: May 12, 2010, 08:38:03 PM

Where does the %B format come from?  And your message syntax [print: choice] is not valid syntax.   You can read in the letter Y or N (into a char variable) to indicate yes or no, for example, and then take the appropriate action.

Cheers,

Steve Kochan
Logged
edelaney05
Newbie
*
Posts: 6






Reply #26 on: May 22, 2010, 10:58:10 PM

I did the exact same as everyone else, but took the quotation "don't make any permanent changes" to mean we could change the fraction as long as we change it back.

After reading everyone else's solutions, I think this may be a bit confusing if you don't follow the scope of the variables very closely.  Correct me if I'm wrong, but oldNumerator & oldDenominator don't even exist outside of the if / else statement.

I'm partial to @jdelamater's solution (make a temp Fraction and either reduce or not based on the BOOL, then make one call to NSLog). One small bug though... I'm pretty sure that tempFrac needs to be released before the end of the method in order not to leak memory. Or does variable scope ensure that it's released when the call completes?

Code: (Objective-C)
-(void) print:(BOOL)reduce {
if (reduce) {
int oldNumerator = numerator, oldDenominator = denominator;
[self reduce];
NSLog(@"%i/%i", numerator, denominator);
numerator = oldNumerator;
denominator = oldDenominator;
} else {
NSLog(@"%i/%i", numerator, denominator);
}
}
Last Edit: May 22, 2010, 11:04:36 PM by edelaney05 Logged
briomusic
Newbie
*
Posts: 15






Reply #27 on: July 02, 2011, 09:24:13 AM

hi, I hope it's ok to post a new question in this topic.

while trying to implement the optional-reduce-print method, I thought I could get away with this:

-(void) print: (BOOL)doReduce{
    Fraction *forPrinting = [[Fraction alloc]init];

//trying to make a carbon copy of the receiver object:
    forPrinting = self;         

    if (denominator!=1) {
        if (doReduce)
            [forPrinting reduce];
        NSLog(@" %i/%i ", [forPrinting numerator], [forPrinting denominator]);}
    else
        NSLog(@" %i", numerator);   
    [forPrinting release];     
//but then I am not allowed to release the temporary object, or main.m will complain!
}


why does the [forPrinting release] seemingly also affect the receiver object, which then is not available more for the remainder of the main routine?
thanks, briomusic
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #28 on: July 02, 2011, 10:41:56 AM

As you read through the chapter and also the stickied post about Programs 7.5 and 7.6, it's important to understand that writing forPrinting = self doesn't make a copy of the Fraction object, it just makes another reference to the same object.

Cheers,

Steve
Logged
MCaEd
Newbie
*
Posts: 49






Reply #29 on: October 08, 2011, 06:42:04 PM

Hi,
Can someone, please, explain amuso's use of self keyword in the first post in his first method? I know self keyword applies to the receiver of the message, but what will it be here?
Thanks
Logged
Pages: 1 [2] 3 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.