GnarGnar
GnarGnar

Reputation: 345

Does anyone know why this isn't working?

I have no idea why this isn't working, I've googled it and found nothing. I'm new to Obj c and xcode. The code works fine if I add something before the -(void) setX: (int) x but not when it is there on it's own... It builds sucessfully, but I do get this, "Thread 1: breakpoint 3.1" at the line in implementation were setX is.

// Program to work with fractions – class version 
#import <Foundation/Foundation.h>
//---- @interface section ----
@interface XYPoint: NSObject

-(void) setX: (int) x;
-(void) setY: (int) y;
-(int) getX;
-(int) getY;

@end

//---- @implementation section ----
@implementation XYPoint 
{
    int xpoint;
    int ypoint;
}

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

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

-(int) getX
{
    return xpoint;
}

-(int) getY
{
    return ypoint;
}
@end

//---- program section ----
int main (int argc, char * argv[])
{
    @autoreleasepool 
    { 
        XYPoint *point = [[XYPoint alloc] init];
        [point setX: 4];
        [point setY: 3];
        NSLog(@"The points are: %i, %i", [point getX], [point getY]);

    return 0; 
    }
}

This doesn't work but this does:

// Program to work with fractions – class version 
#import <Foundation/Foundation.h>
//---- @interface section ----
@interface XYPoint: NSObject

-(void) setX: (int) x;
-(void) setY: (int) y;
-(int) getX;
-(int) getY;

@end

//---- @implementation section ----
@implementation XYPoint 
{
    int xpoint;
    int ypoint;
}
-(void) crap: (int) thing {}
-(void) setX: (int) x
{
    xpoint = x;
}

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

-(int) getX
{
    return xpoint;
}

-(int) getY
{
    return ypoint;
}
@end

//---- program section ----
int main (int argc, char * argv[])
{
    @autoreleasepool 
    { 
        XYPoint *point = [[XYPoint alloc] init];
        [point setX: 4];
        [point setY: 3];
        NSLog(@"The points are: %i, %i", [point getX], [point getY]);

    return 0; 
    }
}

Ok so I just indented it so it would be formated to paste here and when I put it back it works... Does anyone know whats going on?

Upvotes: 0

Views: 2140

Answers (2)

Rudy Velthuis
Rudy Velthuis

Reputation: 28836

Do the following:

@interface XYPoint: NSObject
{
    int xpoint;
    int ypoint;
}

- (void) setX: (int) x;
- (void) setY: (int) y;
- (int) getX;
- (int) getY;

@end

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

etc...

The instance variables are declared in the @interface section. Now it should work. Note that in Objective-C, one does not use getX and setX:, one uses parameterless x and setX: instead. A setX: and x combination can even be used like a property.

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 994351

From your description, it sounds like you have a breakpoint set. A breakpoint breaks into the debugger (with a message like "Thread 1: breakpoint 3.1") when execution reaches that point. This is so that you can check values of variables, step through code, etc.

In Xcode, a breakpoint looks like a blue tag with an arrow pointing toward your source line, in the left hand margin of your code. Try placing your cursor on that line and choosing "Product/Debug/Remove breakpoint at current line" from the menu (or press ⌘\).

Upvotes: 3

Related Questions