Amazon.com Widgets A cleaner way to do this?
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2013, 02:27:44 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
| |-+  Chapter Study
| | |-+  Chapter 7 - More on Classes
| | | |-+  A cleaner way to do this?
Pages: [1]   Go Down
Print
Author Topic: A cleaner way to do this?  (Read 774 times)
JasonWitz
Newbie
*
Posts: 4


Email




« on: March 26, 2010, 02:47:21 AM »

I would like to know a cleaner way to get the output shown below.
Code: (Objective-C)
	// addition
resultFraction = [frac1 add: frac2];
NSLog(@"%i/%i + %i/%i = %i/%i", [frac1 numerator], [frac1 denominator],
[frac2 numerator], [frac2 denominator],
[resultFraction numerator], [resultFraction denominator]);


2010-03-26 02:33:51.245 7x1[5628:10b] Enter a fraction: (number space number)
1 2
2010-03-26 02:33:53.502 7x1[5628:10b] Enter a fraction: (number space number)
3 4
2010-03-26 02:33:54.251 7x1[5628:10b] 1/2 + 3/4 = 5/4
2010-03-26 02:33:54.251 7x1[5628:10b] 1/2 - 3/4 = -1/4
2010-03-26 02:33:54.252 7x1[5628:10b] 1/2 * 3/4 = 3/8
2010-03-26 02:33:54.253 7x1[5628:10b] 1/2 / 3/4 = 2/3

The Debugger has exited with status 0.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3106







« Reply #1 on: March 28, 2010, 11:39:02 AM »

When you get to Chapter 15, there's a brief mention of adding a description method to your class that will get called by NSLog whenever it see the "print object" format characters %@.  Add this to your interface section:

Code: (Objective-C)
-(NSString *) description
{
     return [NSString stringWithFormat: @"%i/%i", numerator, denominator];
}

and then you can write a statement like this in main:

Code: (Objective-C)
NSLog(@"%@ + %@ = %@", frac1, frac2, resultFraction);

Cheers,

Steve Kochan
Logged
JasonWitz
Newbie
*
Posts: 4


Email




« Reply #2 on: March 28, 2010, 10:35:33 PM »

Thanks Steve!

Jason
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.