【问题标题】:Error with Objective C method argument label [duplicate]Objective C方法参数标签错误[重复]
【发布时间】:2013-02-06 15:45:57
【问题描述】:

大家好,这是我的第一篇文章。

我不知道为什么我的代码中出现错误消息。它应该输出矩形的面积和周长。我不想完全更改代码,而是想缩小所涉及的代码行。这是代码。我指出了有问题的行。

recty.h:

#import <Foundation/Foundation.h>

@interface recty: NSObject {
    int width;
    int height;
}

@property int width, height;
- (int)area;
- (int)perimeter;
- (void)setWH:(int) w:(int)h; // 'w' used as name of previous parameter rather than as part of selector
@end

recty.m:

#import "recty.h"

@implementation recty
@synthesize width, height;

- (void)setWH:(int) w:(int) h {
    //'w' used as name of previous parameter rather than as part of selector
}

- (int)area {
    return width * height;
}
- (int) perimeter {
    return (width + height) * 2;
}
@end

ma​​in.m:

#import <Foundation/Foundation.h>
#import "recty.h"
int main(int argc, const char * argv[]) {
    recty *r = [[recty alloc]init];
    [r setWH:6 :8];
    @autoreleasepool {
        // insert code here...
        NSLog(@"recty is %i by %i", r.width, r.height);
        NSLog(@"Area = %i, Perimeter = %i", [r area], [r perimeter]);
    }
    return 0;
}

这与我声明参数的方式有关吗?我在代码中列出了错误信息。我使用 Xcode,制作代码的信息已有 2 年历史。也许有些代码已经过时了?

【问题讨论】:

  • 你在发布问题之前是否在stackoverflow上搜索过错误信息?

标签: ios objective-c cocoa


【解决方案1】:

编译器指出方法名的写法不明确。

- (void)setWH:(int) w:(int)h; // 'w' used as name of previous parameter rather than as part of selector

很容易假设该方法是setWH:w:,而不是真正的setWH::w 是模棱两可的。

应该是:

- (void) setW:(int)w h:(int)h;

或者,更好的是,不需要缩写:

- (void) setWidth:(int)width height:(int)height;

更好的是,尽管如此,还是全力以赴并只使用属性:

@property int width;
@property int height;

即使这样,也可能有问题。由于您的类代表一个矩形(应该称为Recty,而不是recty - 类以大写字母开头),那么您可能希望使用CGFloat 作为宽度和高度。除非您正在做一些像棋盘游戏这样的事情,其中​​宽度/高度是真正不可或缺的。然后执行以下操作:

@property int boardWidth;
@property int boardHeight;

实际上,您的代码中还有一些其他问题(主要是它不是现代的,不是完全损坏的)。我建议:

@interface Recty:NSObject
- (instancetype) initWithWidth:(CGFloat)width height:(CGFloat)height;

@property CGFloat width;
@property CGFloat height;

- (CGFloat) area;
- (CGFloat) perimeter;
@end

@implementation Recty
- (instancetype) initWithWidth:(CGFloat)width height:(CGFloat)height;
{
    self = [super init];
    if (self) {
      _width = width;
      _height = height;
    }
    return self;
}

- (CGFloat)area {
return self.width * self.height;
}
- (CGFloat) perimeter {
return (self.width + self.height) * 2;
}
@end


int main(int argc, const char * argv[]) {
    @autoreleasepool {
      Recty *r = [[Recty alloc] initWithWidth:6 height:8];
      NSLog(@"recty is %f by %f", r.width, r.height);
      NSLog(@"Area = %f, Perimeter = %f", [r area], [r perimeter]);
    }
    return 0;
}

Objective-C 没有命名参数。它将方法名称与参数交错。虽然方法名称的任何部分都可能只是一个简单的:,但我们非常不鼓励这样做。这个编译器警告更是如此。

【讨论】:

  • 令人难以置信的洞察力谢谢。请保持联系,我将整天和一周都在这个项目上工作..
  • 我在 main 上不断收到错误,它说使用未声明的标识符 'r' im 使用上面你给出的代码。
  • 我在编写后从 Xcode 复制/粘贴了该代码。如果您遇到错误,则您的代码必须不同。编辑问题以显示您的新 main()。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-29
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
  • 2011-03-12
相关资源
最近更新 更多