简体   繁体   English

一个简单的代码,不知道如何调试

[英]A simple code, don't know how to debug

In my BNRItem.m , I have implemented an class method: 在我的BNRItem.m中,我实现了一个类方法:

#import "BNRItem.h"

@implementation BNRItem

+ (instancetype) randomItem {

    // Create an immutable array of three adjectives
    NSArray *randomAdjectiveList = @[@"Fluffy", @"Rusty", @"Shiny"];

    //three nouns
    NSArray *randomNounList = @[@"Bear", @"Spork", @"Mac"];

    //random number for 0 to 2
    //NOTE: NSInteger is not an object, but a type definition for "long"
    NSInteger adjectiveIndex = arc4random() % [randomAdjectiveList count];
    NSInteger nounIndex = arc4random() % [randomNounList count];

    NSString *randomName = [NSString stringWithFormat:@"%@ %@",
                            randomAdjectiveList[adjectiveIndex],
                            randomNounList[nounIndex]];

    int randomValue = arc4random() % 100;

    NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                    '0' + arc4random() % 10,
                                    'A' + arc4random() % 26,
                                    '0' + arc4random() % 10,
                                    'A' + arc4random() % 26,
                                    '0' + arc4random() % 10];


    BNRItem *newItem = [[self alloc] initWithItenName:randomName
                                          valueInDollars:randomValue
                                            serialNumber:randomSerialNumber];

    return newItem;

}

@end

In main.m, I try to call the class method to create new instance of BNRItem: 在main.m中,我尝试调用class方法来创建BNRItem的新实例:

#import <Foundation/Foundation.h>
#import "BNRItem.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSMutableArray *items = [[NSMutableArray alloc] init];

        for (int i = 0; i < 10; i++) {
            //Thread 1: breakpoint 1.1
            BNRItem *item = [BNRItem randomItem];
            [items addObject:item];
        }

        for (BNRItem *item in items) {
            NSLog(@"%@", item);
        }

        //destroy the mutable array object
        items = nil;
    }
    return 0;
}

But, when I run my application, I ended up with the following thing which I don't understand how to debug, there is no error message, it just shows a breakpoint in code: 但是,当我运行我的应用程序时,遇到了以下我不理解如何调试的事情,没有错误消息,它只是在代码中显示了一个断点:

在此处输入图片说明

So, what is the problem in my code? 那么,我的代码有什么问题呢? How can I debug with this kind of situation? 我该如何调试这种情况? I mean, I am not only seeking for how to make my current code running, but also want to know how to debug with this situation? 我的意思是,我不仅在寻找如何使当前代码运行的方法,还想知道如何在这种情况下进行调试? because the console doesn't tell much info about what is wrong in code. 因为控制台不会提供太多有关代码错误的信息。

Your application is running with breakpoint turned on. 您的应用程序在breakpoint打开的情况下运行。 All you need to do is to click on the blue symbol (shown in below attached picture) and run it again. 您所需要做的就是单击蓝色符号(如下图所示),然后再次运行它。 That symbol should turn white. 该符号应变为白色。 You should then be good to go! 那么您应该很好走!

在此处输入图片说明

As others have said, you are hitting a breakpoint in your code. 正如其他人所说,您正在代码中遇到断点。 The blue arrow on line 17 in your screenshot is a breakpoint, and the fact that there is tick mark next to it and text in the right margin that says "Thread 1: breapoint..." tells you that you have hit a breakpoint. 屏幕快照中第17行上的蓝色箭头是一个断点,并且它旁边有刻度线,并且右边距中的文字为“线程1:breapoint ...”的事实表明您已经击中了一个断点。

This is a very useful debugging tool. 这是一个非常有用的调试工具。 You should learn how to use it. 您应该学习如何使用它。 Clicking on the same spot on another line adds a breakpoint at that line. 单击另一行上的同一点会在该行上添加一个断点。 When you run your program, if that line of code is executed the debugger stops execution before the line is run and shows you the current value of the variables it things you need to see (variables that just changed, plus local variables.) 在运行程序时,如果执行了该行代码,调试器将在运行该行之前停止执行,并向您显示需要查看的变量的当前值(刚刚更改的变量以及局部变量)。

If you grab a blue breakpoint and drag away, it removes it. 如果抓住一个蓝色的断点并将其拖走,它将删除它。 If you click on it it toggles between active and disabled. 如果单击它,它将在活动和禁用之间切换。

There is also a debugger command line that lets you enter debugger commands to display the values of variables (And even execute code.) 还有一个调试器命令行,可让您输入调试器命令以显示变量的值(甚至执行代码)。

For now, just drag the breakpoint away to delete it, but take the time to learn how to use breakpoints and the other features of the debugger. 现在,只需将断点拖到远处即可将其删除,但请花一些时间来学习如何使用断点和调试器的其他功能。 Truly, this is one of the most powerful features of an IDE and something you must learn how to do if you are going to progress beyond the beginner stage as a developer. 的确,这是IDE最强大的功能之一,如果您要超越开发人员的初级阶段就必须学习如何做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM