我对Objective-C很新,并且创建了这个基本程序。它给我错误@interface
部分,有没有简单的解释,你可以给初学者如何建立@interface
和@implementation
部分?下面的程序有什么问题?您如何正确撰写@interface部分?
#import <Foundation/Foundation.h>
@interface Rectangle : NSObject {
//declare methods
- (void) setWidth: (int) a;
- (void) setHieght: (int) b;
- (double) perimeter;
- (double) area;
}
@end
@implementation Rectangle
{
double area;
double perimeter;
int width;
int height;
}
- (void) setWidth: (int) a
{
width = a;
}
- (void) setHieght: (int) b
{
hieght = b;
}
@end
int main (int argc, const char * argv[])
{
NSAutoreleasePool * Rectangle = [[NSAutoreleasePool alloc] init];
int a = 4
int b = 5
int area = a * b;
int perimeter = 2 * (a +b);
NSLog(@"With width of %i and Hieght of %i", a, b);
NSLog(@"The perimeter is %i", perimeter);
NSLog(@"The Area is %i", area);
[pool drain];
return 0;
}
请更新您的问题,列出正在生成的错误并指出错误引用的行。只是说“给我错误”并不是很好。我会注意到,从你的例子中,你很可能会从旧的源代码。 NSAutoreleasePool,[pool drain]等来自旧式的手动内存管理。通常,您的应用程序现在会使用ARC(自动引用计数),这会消除大部分代码,并会在您尝试使用时产生错误。 –
摆脱'@interface'块中的大括号,一切都会很好。 – rmaddy
是的,问题通过在@interface后删除{}来解决,谢谢。 – user2624497