2012-12-02 72 views
1

我目前是一名新開發人員,遇到此錯誤; SIGABRT。我曾嘗試開發許多應用程序,並且他們中的很多人都會遇到此錯誤。我正在開發一個適合小孩的聲音匹配教育應用,他們在Xcode中使用一個選擇器視圖來匹配動物和他們的聲音。我的代碼如下:SIGABRT Xcode ???需要幫助

ViewController.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 

ViewController.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錯誤,我絕對需要幫助。謝謝。

+7

SIGABRT通常會在運行控制檯中顯示一條消息,它會以相當通俗的英文顯示錯誤。如果錯誤不是不言自明的,請將其作爲對問題的修改。 – Greg

+0

IBOutlets設置是否正確? –

+0

來自'main'的標記行並沒有幫助,因爲這沒有告訴我們什麼。此外,幾乎總是有一個相對容易理解的錯誤信息。用這些信息解決這個問題要快得多。我知道我不打算梳理沒有任何信息的代碼頁。 – Metabble

回答

3

您必須識別導致問題的代碼行,這是我們無法從代碼段中識別出來的代碼。

您可能希望啓用異常斷點,因爲它們通常可以識別導致異常的精確確切代碼行。當我在開發中遇到異常時,我只會在「所有」異常中添加異常斷點(請參見Breakpoint Navigator Help或下面的屏幕快照)。這樣,如果我通過調試器運行程序,遇到異常時,它將停止違規行中的代碼,極大地簡化了識別問題根源的過程。它並不總是完美的工作,但它經常比其他技術更快地發現異常的來源。

all exceptions

有關調試的應用更廣泛的討論(例如,使用調試器來單步通過您的代碼,看到Xcode User Guide: Debug your App。如果你知道問題出在這個方法中,你可能會想step through your code,行線,問題就會變得不言而喻。


我也建議您在雷Wenderlich現場檢查My App Crashed, Now What

+0

這是一個很棒的提示。 +1 – andrewz

0

你似乎有

: - 你需要包括方法調用 stringWithFormat:

內選定的字符串可以嘗試下面

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

我很驚訝,編譯器將實際編譯這一點,但無論如何:與你匹配消息的一些格式問題

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

你的編譯器肯定會給你一個警告。所以你的第一步應該總是修復編譯器警告。在構建設置中打開「將警告視爲錯誤」選項是很好的做法。這意味着編譯器對接受和運行的內容非常嚴格 - 這意味着您必須修復警告。從編譯器獲取警告真的沒有理由。

0

在第一次調用堆棧前閱讀英文。它告訴你什麼?sigabrt最常見的形式是在.h文件中IBOutlet被刪除,並且出口連接仍然在故事板中。要解決這個問題,請刪除故事板中的所有插座連接,重寫IBOutlet,然後重新鏈接它們。