#import <Foundation/Foundation.h>@interface Coordinate : NSObject-(void) setX:(int) x1;
-(void) setY:(int) y1;
-(int) getX;
-(int) getY;@end
@implementation Coordinate{
    int x;
    int y;
}- (void) setX:(int)x1{
    x=x1;
}-(void) setY:(int)y1 {
    y=y1;
}-(int) getX{
    return x;
}-(int) getY{
    return y;
}@endint main(int argc, const char * argv[])
{    @autoreleasepool {
        
        // insert code here...
        Coordinate *myCoordinate;
        myCoordinate=[Coordinate alloc];
        myCoordinate=[myCoordinate init];
        
        [myCoordinate setX:3];
        [myCoordinate setY:5];
        
        int a=[myCoordinate getX];
        int b=[myCoordinate getY];
        
        NSLog(@"my coordinate is x=%i,y=%i",a,b);
    }
    return 0;
}为什么这段代码在xcode输出(11db)?哪里出了问题啊我有点知道可能是set方法里有问题,但找不出什么问题。求解答谢谢!objective-cxcode

解决方案 »

  1.   

    还有如果可以的话(11db)这个是报错吗?我初学objective-c
      

  2.   

            myCoordinate=[Coordinate alloc];
            myCoordinate=[myCoordinate init];使用如上的代码来实例一个对象,有很大的可能得不到我们想要的对象。官方建议的方式是使用 alloc init链式创建
    myCoordinate=[[Coordinate alloc] init];
    原因是:即便对象无法创建成功,也会返回nil, 最重要的原因是指针指向的对象是由init 后返回,而非alloc
      

  3.   

    NSLog(@"my coordinate is x=%i,y=%i",a,b);
    整形用%d吧
      

  4.   

    你用的是XCODE5吧,改一下AUTO
      

  5.   

    我已经知道错误了,代码没问题,是xcode设置上得问题