Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
May 22, 2018, 04:20:25 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
Author Topic: Program 3.4 - why does my error give this result?  (Read 1570 times)
Posts: 27

« on: April 02, 2012, 07:58:50 PM »

Here's the interface section as it should be:

@interface Fraction: NSObject

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


But if I replace (int) with (void) in only the two spots above (and not make that same mistake later in the program), like so:

@interface Fraction: NSObject

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


I get the following output
The value of myFraction is:  181475088/3

The denominator is correct, but the numerator is some random number that changes every time I run the program (the program builds successfully, but gives me a couple of warning messages).
Example output:
The value of myFraction is:  154222880/3
The value of myFraction is:  200360224/3
The value of myFraction is:  51462512/3

This really isn't all that important, I was just thinking that understanding the output will help me better understand some of the nuances of the language, or how the compiler functions.

Here's a screen shot of the "mistake" program:

« Last Edit: April 02, 2012, 08:05:49 PM by Bunchadna » Logged
Full Member
Posts: 155

« Reply #1 on: April 10, 2012, 02:19:42 AM »

I sort of cracked up when I saw what you did as I used to do the same type of stuff on purpose when I was studying C.  My main reason was to try and 'break stuff' or 'break the rules' in order to understand how things work.  I noticed that 'deconstructing' stuff really helps in understanding how something works.  This applies to other disciplines as well--for instance music theory.

I'm probably not going to be able to give you a fully coherent answer as I'm a beginner, and I know just enough C to be dangerous.  But I think that this is a C issue having to do with data types; and I've seen this before when I was studying C.  When you fail to put (int) as the return type when declaring the method (or specifically using (void) which specifies nothing is returned), and then you use that method later (calling for an int to be returned) , the stuff you get back is just random crap floating around in memory as the compiler hasn't been told the correct info.  As far as why the numerator gets affected by this but not the denominator, I have no idea and am now curious myself.  Smiley
Full Member
Posts: 177

« Reply #2 on: April 10, 2012, 04:46:03 AM »

How are you printing the values to the console? if you are using the print method, it should still work as expected.

Now, i see you only changed the declaration to an void, and not the implementation section to void, yes? So they still say they return an int?

I still can't give you a good answer though, but i think it's the same concept as other "rule-breaking" stuff. It simply gives you unreliable and unexpected outputs. As to why the denominator is o.k i don't know, but maybe it has to do with some compiler stuff that after messing the numerator up, automatically changes the denominator method to return an int in the .h file.

Best Regards -
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube:

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
Jr. Member
Posts: 53

« Reply #3 on: April 21, 2012, 10:19:15 PM »

Hello, still newbie here but I've watched/tried some IOS programming by following iTunes U courses.

My understanding is "set" and "get" are the norm/convention to use when setting/getting variables in objects.

The following code works:

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

// Start of @interface section
@interface Fraction: NSObject
    int numerator;
    int denominator;

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

@end // @interface

// Start of @implementation section
@implementation Fraction
- (void) print {

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

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

- (int) getNumerator {
    return numerator;

- (int) getDenominator {
    return denominator;

@end // @implementation

// Start of main program section

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

    @autoreleasepool {
        Fraction *myFraction;       // create object variable myFraction
        int num, den;
        // Create an instance of the object Fraction
        myFraction = [Fraction alloc];
        // myFraction = [Fraction init];        -- no need to init if we're using Xcode 4.3.2
        // assign value to fraction
        [myFraction setNumerator: 1];
        [myFraction setDenominator:3];
        // Display fraction object
        [myFraction print];
        num = [myFraction getNumerator];
        den = [myFraction getDenominator];
        NSLog(@"The numerator is %i, and denominator is %i",num,den);
        // free up object
        [myFraction release];
        // [pool drain];   -- no need to pool drain if we're using Xcode 4.3.2

    return 0;
Pages: [1]   Go Up
Jump to:  

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 ゥ 2009 All rights reserved.