开发者

What is wrong with my code? (Obj-C)

开发者 https://www.devze.com 2023-03-21 11:28 出处:网络
So I am trying to learn how to code in Obj-C as of right now and my current goal is to write a simple command line program to calculate the area and perimeter of a rectangle; however, Xcode keeps thro

So I am trying to learn how to code in Obj-C as of right now and my current goal is to write a simple command line program to calculate the area and perimeter of a rectangle; however, Xcode keeps throwing up errors at me when I try the program. Here is my code:

#import <Foundation/Foundation.h>

@interface Rectangle : NSObject {
@private
    int width;
    int height;
}

-(void) setWidth: (int) w;
-(void) setHeight: (int) h;
-(int)  width;
-(int)  height;
-(int)  area;
-(int)  perimeter;
@end

@implementatio开发者_运维技巧n Rectangle

-(void) setWidth:(int)w
{
    width = w;
}
-(void) setHeight:(int)h
{
    height = h;
}
-(int) width
{
    return width;
}
-(int) height
{
    return height;
}
-(int) area  

It is at this line that i receive the error " Thread 1: stopped at breakpoint 1"

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

int main (int argc, const char * argv []){
    @autoreleasepool {
        Rectangle * shape1 = [Rectangle new];

        [shape1 setHeight: 5];
        [shape1 setWidth: 15];

        NSLog(@"the area of the rectangle is %i and the perimeter is %i", [shape1 area],    [shape1 perimeter]);
    }
    return 0;
}


the code is correct , in xcode u have a little arrow and pose button on the butom left click that or click on the top right where it is written brake point and press play


Your code is absolutely correct. It works for me.


There was a break point button for run button that was clicked on. sometimes this make u think code bad but it just stops at multiple places in the code

0

精彩评论

暂无评论...
验证码 换一张
取 消