Objective c 这里是否需要使用#import?
在Objective c 这里是否需要使用#import?,objective-c,Objective C,在矩形的实现部分,当我省略时,导入“XYpoint”对我来说仍然是一样的。放置#导入“XYpoint”是良好做法还是会影响程序 #import <Foundation/Foundation.h> @interface XYPoint : NSObject @property int x, y; -(void) setX: (int) xVar andY: (int) yVar; @end Rectangle的实现没有使用XYPoint类的任何细节。它只是将其视为通用指针
矩形
的实现部分,当我省略时,导入“XYpoint”
对我来说仍然是一样的。放置#导入“XYpoint”
是良好做法还是会影响程序
#import <Foundation/Foundation.h>
@interface XYPoint : NSObject
@property int x, y;
-(void) setX: (int) xVar andY: (int) yVar;
@end
Rectangle
的实现没有使用XYPoint
类的任何细节。它只是将其视为通用指针,从不向其发送消息或取消引用。因此,转发声明(接口文件矩形中的@class
语句)就足够了。导入头对编译的程序没有任何影响
您的Rectangle
类很可能最终会演变为关心XYPoint
类的接口。当它这样做时,它将需要导入该接口声明。如果您忽略导入,编译器将警告您
也就是说,没有什么理由不导入它。您的Rectangle
实现没有使用XYPoint
类的任何细节。它只是将其视为通用指针,从不向其发送消息或取消引用。因此,转发声明(接口文件矩形中的@class
语句)就足够了。导入头对编译的程序没有任何影响
您的Rectangle
类很可能最终会演变为关心XYPoint
类的接口。当它这样做时,它将需要导入该接口声明。如果您忽略导入,编译器将警告您
也就是说,没有什么理由不进口它
#import "XYpoint.h"
@implementation XYPoint
@synthesize x, y;
-(void) setX:(int)xVar andY:(int)yVar {
x = xVar;
y = yVar;
}
@end
#import <Foundation/Foundation.h>
@class XYPoint;
@interface Rectangle: NSObject
-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
@end
#import "Rectangle.h"
#import "XYpoint.h"
@implementation Rectangle {
XYPoint *origin;
}
-(void) setOrigin:(XYPoint *)pt {
origin = pt;
}
-(XYPoint *) origin {
return origin;
}
@end
#import "XYpoint.h"
#import "Rectangle.h"
int main (int argc, char * argv[]) {
@autoreleasepool {
Rectangle *rect = [[Rectangle alloc] init];
XYPoint *pointy = [[XYPoint alloc] init];
[pointy setX:5 andY:2];
rect.origin = pointy;
NSLog(@"Origin %i %i", rect.origin.x, rect.origin.y);
}
return 0;
}