Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 17, 2018, 08:08:17 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
  Print  
Author Topic: Chapter 3 - Exercise 7  (Read 2478 times)
VioladaGamba
Newbie
*
Posts: 13


« on: June 30, 2013, 11:56:51 PM »

Another beginner to Ob-C. Loving it, but it is slow going!

Is anyone able to assist me in understanding why it is telling me that I have not declared the identifier 'gX'?

Why is it not saying the same for 'gY' when I have (I think) given the same explanation for gX as I have gY in the @implementation section.

Thanks.

C.
Logged
mitchb
Full Member
***
Posts: 155


« Reply #1 on: July 01, 2013, 11:45:15 AM »

I think this will work better.

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

@interface XYPoint : NSObject

- (void)print;
- (void)setX: (int) X;
- (void)setY: (int) Y;
- (int) X;
- (int) Y;

@end

@implementation XYPoint

{
    int x;
    int y;
}

- (void)print
{
    NSLog(@"%i.%i", x, y);
}

- (void)setX:(int)X
{
    x = X;
}

- (void)setY:(int)Y
{
    y = Y;
}

- (int) X
{
    return x;
}

- (int) Y
{
    return y;
}

@end

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

    @autoreleasepool {
        XYPoint *babyXY = [[XYPoint alloc] init];
        XYPoint *babyXY2 = [[XYPoint alloc] init];
       
        [babyXY setX: 5];
        [babyXY setY: 8];
       
        [babyXY2 setX: 9];
        [babyXY2 setY: 12];
       
        NSLog(@"The first co-ordinate is %i.%i", babyXY.X, babyXY.Y);
        NSLog(@"The second co-ordinate is %i.%i", babyXY2.X, babyXY2.Y);
       
            // To use the getters.
       
        NSLog(@"The first co-ordinate is %i.%i", [babyXY X], [babyXY Y]);
        NSLog(@"The second co-ordinate is %i.%i", [babyXY2 X], [babyXY2 Y]);
       
            // To test the print method.
       
        [babyXY print];
        [babyXY2 print];
    }
    return 0;
}

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
VioladaGamba
Newbie
*
Posts: 13


« Reply #2 on: July 01, 2013, 10:10:46 PM »

Mitch,

Thanks very much for this! I will spend the next few hours comparing it to mine and working out where I went wrong. My only reason for using s and g before the x and y, was so that I could try and separate out the setting and getting ideas - but, maybe I just made things too complicated. Anyway, thanks, and also for adding the 'comments' at the end about setters/getters and printing.

Claire
Logged
ceres75
Full Member
***
Posts: 118



Email
« Reply #3 on: July 05, 2013, 02:35:56 AM »

HI violadagamba,

well if in your NSLog statement you write this way [babyXY gx] without comma it should work. or better you can write babyXY.gx

ceres
Logged

Programming is fun!! Wink
Pages: [1]   Go Up
  Print  
 
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 classroomM.com. All rights reserved.