1

Hello everyone this is my first post.

I am at a loss as to why I'm getting error message in my code. It is supposed to output the area and perimeter of a rectangle. Instead of completely changing the code, I want to narrow in on the line of code involved. Here is the code. I pointed out the lines in question.

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

Does it have to do with how I'm declaring parameters? I listed the error info in the code. I use Xcode and the info on making the code is 2 years old. Maybe some of the code is outdated?

bbum
  • 162,346
  • 23
  • 271
  • 359
Adreamer82
  • 143
  • 2
  • 7

1 Answers1

2

The compiler is pointing out that the method name is written in an ambiguous fashion.

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

It would be easy to assume that the method is setWH:w: instead of what it truly is which is setWH::. That w is ambiguous.

It should be:

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

Or, better yet, no need to abbreviate:

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

Better, still, though is to go the full monty and just use properties:

@property int width;
@property int height;

Even that, though, may be problematic. Since your class is representing a rect (and should be called Recty, not recty -- classes begin with capital letters), then you would probably want to use CGFloat for the width and height. Unless you are doing something like modeling a board game where the width/height are truly integral. Then do something like:

@property int boardWidth;
@property int boardHeight;

Actually, there are some other problems in your code (mostly that it isn't modern, not outright broken). I'd recommend:

@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 does not have named parameters. It interleaves the method names with the arguments. While any part of the method name could be just a bare :, that is very actively discouraged. Even more so by this compiler warning.

Ben Lings
  • 28,823
  • 13
  • 72
  • 81
bbum
  • 162,346
  • 23
  • 271
  • 359
  • incredible insight thanks . pls stay in touch ill be working on this project all day and week.. – Adreamer82 Feb 06 '13 at 16:34
  • i keep getting error on main it says use of undeclared identifier 'r' im using code above u gave. – Adreamer82 Feb 06 '13 at 17:53
  • 1
    I copy/pasted that code from Xcode after I wrote it. If you are getting an error, your code must be different. Edit the question to show your new main(). – bbum Feb 06 '13 at 18:57