大家好,這是我的第一篇文章。Objective C方法參數標籤的錯誤
我不知道爲什麼我在代碼中收到錯誤消息。它應該輸出矩形的面積和周長。我不想完全改變代碼,而是想要縮小所涉及的代碼行。這是代碼。我指出了有關問題。
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
的main.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;
}
它有沒有用H做我正在聲明參數?我在代碼中列出了錯誤信息。我使用Xcode和使代碼2歲的信息。也許一些代碼已經過時了?
您是否在發佈問題之前搜索了stackoverflow上的錯誤消息? –