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