【问题标题】:What is wrong with my code? (Obj-C)我的代码有什么问题? (对象-C)
【发布时间】:2011-07-20 12:57:31
【问题描述】:

所以我现在正在尝试学习如何在 Obj-C 中编码,我目前的目标是编写一个简单的命令行程序来计算矩形的面积和周长;但是,当我尝试该程序时,Xcode 不断向我抛出错误。 这是我的代码:

#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

@implementation Rectangle

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

在这一行我收到错误“线程 1:在断点 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;
}

【问题讨论】:

  • 您应该复制从编译器/gdb 获得的整个错误消息并将其粘贴到此处。
  • 我遇到过几次这个问题,xcode 在一些我在代码编辑器中看不到的“幽灵”断点处中断,但如果你点击 cmd+alt+B 并查看断点窗口,您可以从那里禁用或删除它们。
  • 这不是断点。你的程序崩溃了。查看调试器窗口以获取错误消息并将其粘贴到此处。

标签: objective-c macos xcode4 area


【解决方案1】:

代码是正确的,在xcode中你有一个小箭头和姿势按钮,左键单击它或单击右上角写有刹车点的地方然后按播放

【讨论】:

    【解决方案2】:

    您的代码绝对正确。它对我有用。

    【讨论】:

    • 嗯,我刚刚重新启动了我的电脑,现在它可以工作了.. 很奇怪。无论如何,谢谢。
    【解决方案3】:

    点击了运行按钮的断点按钮。有时这会让你觉得代码不好,但它只是停在代码中的多个地方

    【讨论】:

      猜你喜欢
      • 2017-05-02
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      相关资源
      最近更新 更多