-1

我对 Objective-C 相当陌生,并创建了这个基本程序。它在该@interface部分上给了我错误,是否有任何简单的解释可以让初学者了解如何构建@interface@implementation部分?下面的程序有什么问题?

    #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;
    }
4

2 回答 2

3

您列出了 ivars 应该去的方法。它应该是:

@interface Rectangle : NSObject {

    //instance variables here
}

// declare methods or properties here
- (void) setWidth: (int) a;
- (void) setHieght: (int) b;
- (double) perimeter;
- (double) area;

@end

正如已经指出的那样,您可以简单地删除花括号。

于 2013-07-26T23:16:26.413 回答
0

您的代码中有几个问题,我们稍后会看到,但是作为初学者,您需要了解以下几点:

  • main()用于main.m您项目中的课程,不要在这里和那里搞砸,init()而是使用
  • 你没有在你的 {} 范围内声明方法@implementaion
  • 在什么@end@implementation没有执行之后应该写
  • @implementation不应限制在 {} 范围内,因为它以@end
  • 还有更多,在这里找到它http://www.slideshare.net/musial-bright/objective-c-for-beginners

所以你应该看起来像这样:

#import <Foundation/Foundation.h>

@interface Rectangle : NSObject

//declare methods
- (void) setWidth: (int) a;
- (void) setHieght: (int) b;
- (double) perimeter;
- (double) area;

@end

@implementation Rectangle

    {双区;双周长;整数宽度;整数高度;}

- (void) setWidth: (int) a {
    width = a;
}

- (void) setHieght: (int) b {
    height = b;
}

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
        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);
    }
    return self;
}

@end
于 2013-07-27T06:13:51.647 回答