Amazon.com Widgets Chapter 3, Programs 3.1 and 3.2 code and tip
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 22, 2013, 10:45:26 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
| |-+  Program Examples
| | |-+  Chapter 3
| | | |-+  Chapter 3, Programs 3.1 and 3.2 code and tip
Pages: [1]   Go Down
Print
Author Topic: Chapter 3, Programs 3.1 and 3.2 code and tip  (Read 5654 times)
EnzoBro
Newbie
*
Posts: 10






« on: May 06, 2009, 04:42:06 PM »

I put this code in here for myself and others to be able to get "good" code to paste into Xcode.  There is no substitute for hand typing in the code.  However, this paste-able code should help you find typos or small syntax errors that may be hard for beginner to find.  (I had this problem)

Program 3.1
Code: (Objective-C)
//simple program to work with fractions
#import <Foundation/Foundation.h>

int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int numerator = 1;
int denominator = 3;
NSLog (@"The fraction is %i/%i", numerator, denominator);

[pool drain];
return 0;
}

This "builds and gos" in Xcode.
OUTPUT is:
(NSLog junk) The fraction is 1/3


Program 3.2
Code: (Objective-C)
// program to work with fractions - class version

#import <Foundation/Foundation.h>

//---- @interface section ----

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

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;

@end

//----@implementation section ----

@implementation Fraction
-(void) print
{
NSLog (@"%i/%i", numerator, denominator);
}

-(void) setNumerator: (int) n
{
numerator = n;
}

-(void) setDenominator: (int) d
{
denominator = d;
}

@end

//---- program section ----

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

Fraction *myFraction;

// Create an instance of a fraction

myFraction = [Fraction alloc];
myFraction = [myFraction init];

// Set fraction to 1/3

[myFraction setNumerator: 1];
[myFraction setDenominator: 3];

// Display the fraction using print method

NSLog (@"The value of myFraction is:");
[myFraction print];
[myFraction release];

[pool drain];
return 0;
}
This "builds and gos" in Xcode.
OUTPUT is:
(NSLog junk) The value of myFraction is:
(NSLog junk) 1/3


Just a TIP:
Xcode has the ability to collapse "paragraphs" of your code.  Look for little black triangles in a column to the left of your code.  Mouse over the triangle and Xcode will show you the "paragraph" that THAT triangle collapses.  I used it to collapse each section (interface, implementation, program) as I completed them to minimize the clutter.
Try it!
« Last Edit: May 07, 2009, 05:33:27 AM by EnzoBro » Logged
My2CentsWorth
Newbie
*
Posts: 49


Mark






« Reply #1 on: March 07, 2010, 11:55:30 PM »

Just a TIP:
Xcode has the ability to collapse "paragraphs" of your code.  Look for little black triangles in a column to the left of your code.  Mouse over the triangle and Xcode will show you the "paragraph" that THAT triangle collapses.  I used it to collapse each section (interface, implementation, program) as I completed them to minimize the clutter.
Try it!

Hey I like that, Thanks for the Tip EnzoBro.
Logged

Donate your unused CPU time to better the world we live in @ http://www.worldcommunitygrid.org/
lordemil
Newbie
*
Posts: 1


Email




« Reply #2 on: March 27, 2011, 03:00:28 PM »

i copied the whole code but still getting the same errors as before. the errors i get is: "use of undeclared identifier 'Fraction'; did you mean 'fraction'? and "use of undeclared identifier 'myFraction'
heres the code:
Program section:
Code: (Objective-C)
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Fraction *myFraction; 
   
        // Create an instance of a fraction 
    myFraction = [Fraction alloc]; 
    myFraction = [myFraction init]; 
   
        // Set fraction to 1/3 
    [myFraction setNumerator: 1]; 
    [myFraction setDenominator: 3]; 
   
        // Display the fraction using print method 
    NSLog (@"The value of myFraction is:"); 
    [myFraction print]; 
    [myFraction release]; 
   
    [pool drain];
    return 0;
}

@interface section:
Code: (Objective-C)
#import <Foundation/Foundation.h>


@interface Fraction: NSObject 

    int numerator; 
    int denominator; 


-(void) print; 
-(void) setNumerator: (int) n; 
-(void) setDenominator: (int) d; 

@end

@implementation section
Code: (Objective-C)
#import "Fraction.h"


@implementation Fraction 

-(void) print 

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


-(void) setNumerator: (int) n 

    numerator = n; 


-(void) setDenominator: (int) d 

    denominator = d; 


@end

please help me with my problem, thanks
Logged
mitchb
Jr. Member
**
Posts: 98






« Reply #3 on: March 27, 2011, 05:06:11 PM »

You need to add:

#import "Fraction.h"

to your program section. You can edit line 1 to import Fraction.h because your interface section already imports Foundation/Foundation.h or just put it on line 2.

Happy programming
Mitch
Logged

If you give a man a program, you will frustrate him for a day;
If you teach him how to program, you will frustrate him for a lifetime;
     - Anonymous
smartalecc5
Newbie
*
Posts: 4


Email




« Reply #4 on: September 09, 2011, 08:18:51 PM »

Wow, I just spent 30 minutes trying to get my typed 3.2 program to work.  It kept saying "stopped at breakpoint 1".  I literally checked each character of code two times and then started pasting in certain parts from the solution file posted above and couldn't find.  I pasted in the entire solution file and I STILL GOT IT.

Did a google search on it and I guess I created a breakpoint in the sidebar and I just had to rightclick delete it.  Wow.  I just had to vent here!  Thanks for the source code help or I'm sure I'd be ripping my hair out figuring out what I typed in wrong lol.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #5 on: September 09, 2011, 08:42:23 PM »

Yeah, everyone who first starts using Xcode gets burned by that.  But the burn is so severe, you  learn that lesson well!

Cheers,

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