2012-03-18 85 views
0

我不知道爲什麼這不起作用,我搜索了它並沒有發現任何東西。我是Obj c和xcode的新手。如果我在 - (void)setX:(int)x之前添加了一些東西,但是它沒有在它自己的地方,那麼代碼就可以正常工作了......它的構建成功了,但我確實得到了這個「線程1:斷點3.1」在執行中的行是setX。有誰知道爲什麼這不起作用?

// 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; 
    } 
} 

這並不工作,但這樣做:

// 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; 
    } 
} 

好了,所以我就縮進它,所以它會被格式化,以粘貼在這裏,當我把它放回它的工作原理...有誰知道發生了什麼事?

回答

3

從您的描述中,聽起來好像你有一個斷點集合。當執行到達該點時,斷點進入調試器(帶有「線程1:斷點3.1」之類的消息)。這是爲了您可以檢查變量的值,逐步執行代碼等。

在Xcode中,斷點看起來像一個藍色標記,箭頭指向源代碼行,位於代碼的左側邊緣。嘗試將光標置於該行並從菜單中選擇「Product/Debug /在當前行刪除斷點」(或按⌘\)。

+0

+1爲了建立在這個正確的答案上,第二個代碼示例沒有停止運行的原因是多餘的空函數落在行上,斷點將'setX:'從斷點移開。所以程序執行永遠不會使它與斷點一致。 – NJones 2012-03-18 04:23:08

+0

是的,這樣做更有意義,謝謝你的迴應。 – GnarGnar 2012-03-18 05:04:27

1

執行以下操作:

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

實例變量在@interface部分聲明。現在它應該工作。請注意,在Objective-C中,不使用getXsetX:,而是使用無參數xsetX:。 A setX:x組合甚至可以像財產一樣使用。

相關問題