1

我目前是一名新开发人员,遇到了这个错误;SIGABRT。我尝试过开发许多应用程序,其中很多都收到此错误。我正在为小孩开发一个声音匹配教育应用程序,他们使用 Xcode 中的一个选择器视图来匹配动物及其声音。我的代码如下:

视图控制器.m

#define componentCount 2
#define animalComponent 0
#define soundComponent 1
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize lastAction;
@synthesize matchResult;

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    NSString *actionMessage;
    NSString *matchMessage;
    int selectedAnimal;
    int selectedSound;
    int matchedSound;

    if (component==animalComponent) {
        actionMessage=[[NSString alloc]
                       initWithFormat:@"You Selected The Animal Named '%@'!",
                       [animalNames objectAtIndex:row]];

    } else {
        actionMessage=[[NSString alloc]
                       initWithFormat:@"You Selected The Animal Sound '%@'!",
                       [animalSounds objectAtIndex:row]];
    }

    selectedAnimal=[pickerView selectedRowInComponent:animalComponent];
    selectedSound=[pickerView selectedRowInComponent:soundComponent];

    matchedSound=([animalSounds count] - 1) -
    [pickerView selectedRowInComponent:soundComponent];

    if (selectedAnimal==matchedSound) {
        matchMessage=[[NSString alloc] initWithFormat:@"Yes, A '%@' Does Go '%@'!"];
        [animalNames objectAtIndex:selectedAnimal],
        [animalSounds objectAtIndex:selectedSound];
    } else {
        matchMessage=[[NSString alloc] initWithFormat:@"No, A '%@' Doesn't Go '%@'!"];
        [animalNames objectAtIndex:selectedAnimal],
        [animalSounds objectAtIndex:selectedSound];
    }

    lastAction.text=actionMessage;
    matchResult.text=matchMessage;

    [matchMessage release];
    [actionMessage release];
}



- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component==animalComponent) {
        return [animalNames objectAtIndex:row];
    } else {
        return [animalSounds objectAtIndex:row];
    }
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component==animalComponent) {
        return [animalNames count];
    } else {
        return [animalSounds count];
    }
}

- (void)dealloc {
    [animalNames release];
    [animalSounds release];
    [lastAction release];
    [matchResult release];
    [super dealloc];
}

- (void)viewDidLoad {
    animalNames=[[NSArray alloc]initWithObjects:
                 @"Cat", @"Dog", @"Snake", @"Cow", @"Horse", @"Pig", @"Duck", @"Sheep", @"Bird",nil];
    animalSounds=[[NSArray alloc]initWithObjects:
                 @"Chirp", @"Baa", @"Quack", @"Oink", @"Nay", @"Moo", @"Hiss", @"Bark", @"Purr",nil];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

视图控制器.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
    NSArray *animalNames;
    NSArray *animalSounds;
    IBOutlet UILabel *lastAction;
    IBOutlet UILabel *matchResult;
}

@property (nonatomic, retain) UILabel *lastAction;
@property (nonatomic, retain) UILabel *matchResult;

@end

SIGABRT 在哪里

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        //at end of row of code above this, there was the error message: signal SIGABRT
    }
}

无论如何,为了摆脱这个 SIGABRT 错误,我绝对需要帮助。谢谢你。

4

3 回答 3

3

您必须确定导致问题的代码行,这是我们无法从代码片段中识别出来的。

您可能希望启用异常断点,因为它们通常可以准确识别导致异常的代码行。当我在开发中遇到异常时,我将简单地在“所有”异常上添加一个异常断点(请参阅Breakpoint Navigator 帮助或下面的屏幕快照)。这样,如果我通过调试器运行程序,当它遇到异常时,它会在有问题的行停止代码,大大简化了识别问题根源的过程。它并不总是完美地工作,但它经常比其他技术更快地找到异常的来源。

所有例外

有关调试应用程序的更广泛讨论(例如,使用调试器单步执行您的代码,请参阅Xcode 用户指南:调试您的应用程序。如果您知道问题出在此方法中,您可能需要逐行执行您的代码线,问题也就不言而喻了。


我还建议您查看Ray Wenderlich 网站上的My App Crashed, Now What 。

于 2012-12-02T22:43:04.663 回答
0

您的匹配消息似乎存在一些格式问题:

matchMessage=[[NSString alloc] initWithFormat:@"Yes, A '%@' Does Go '%@'!"];
[animalNames objectAtIndex:selectedAnimal],
[animalSounds objectAtIndex:selectedSound];

我很惊讶编译器实际上会编译它,但无论如何 - 你需要在方法调用中包含选定的字符串stringWithFormat:

试试下面:

matchMessage = [[NSString alloc] initWithFormat:@"Yes, A '%@' Does Go '%@'!", 
    [animalNames objectAtIndex:selectedAnimal],
    [animalSounds objectAtIndex:selectedSound]
];

你的编译器肯定会给你一个警告。因此,您的第一步应该始终是修复编译器警告。最好在构建设置中打开“将警告视为错误”选项。这意味着编译器对于它将接受和运行的内容非常严格——这意味着您必须修复警告。编译器发出警告真的没有任何借口。

于 2012-12-03T03:31:43.157 回答
0

在第一个调用堆栈之前阅读英文。它告诉你什么?最常见的 sigabrt 形式是在IBOutlet.h 文件中删除 an 并且出口连接仍然存在于情节提要中。要解决这个问题,请删除情节提要中的所有出口连接,重写IBOutlets,然后重新链接它们。

于 2017-08-14T06:59:25.503 回答