2010-10-10 59 views
0

我是一個新手,編寫簡單的程序,並且在編譯期間沒有警告/錯誤。我收到「EXC_BAD_ACCESS」錯誤。會明白任何這方面的幫助:目標C:「EXC_BAD_ACCESS」錯誤

int main (int argc, const char * argv[]) { 
     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

     moviedatabase *movie1=[[moviedatabase alloc] init]; 

    [movie1 addMovieWithName:@"DDLJ" andyear:1789 andlength:360 andGenre:Horror]; 
    [movie1 printAll]; 
    [movie1 release]; 

    [pool drain]; 

    return 0; 
} 

這裏是moviesdatabase類和電影類爲繼承:

@interface moviedatabase : movies{ 
//no variables in the class 
} 

-(void) addMovieWithName: (NSString *)mname andyear: (int) myear andlength: (int) mlength andGenre: (enum Genre) mgenre; 
-(void) printAll; 

@end 

@interface movies : NSObject { 
NSString *name; 
int year; 
int length; 
enum Genre {Comedy,Drama,Horror,Action} genre; 
} 

@property (nonatomic) NSString *name; 
@property (nonatomic) int year; 
@property (nonatomic) int length; 
@property (nonatomic) enum Genre genre; 

-(id) initWithName: (NSString *)name andyear: (int) year andlength: (int) length andGenre: (enum Genre) genre; 

@end 

包括moviedatabase執行:

#import "moviedatabase.h" 


@implementation moviedatabase 

-(void) addMovieWithName: (NSString *) mname andyear: (int) myear andlength: (int) mlength andGenre: (enum Genre) mgenre 
    { 
     name=mname; 
    year=myear; 
    length=mlength; 
    genre=mgenre; 

    } 

-(void) printAll; 
{ 
    NSLog(@"name=%@, year=%@, length=%@, genre=%@",name,year,length,genre); 
} 

@end 
+2

也許向我們展示'moviedatabase'類可能會有所幫助。此外,當您顯示源代碼時,請突出顯示問題編輯器中的代碼,然後按頂部的010101圖標縮進。 – BoltClock 2010-10-10 07:06:41

+0

作爲@ BoltClock評論的附錄,根據我們在'main'函數中可以看到的,沒有什麼不是有序的,'EXC_BAD_ACCESS'源於'addMovieWithName'或'printAll'方法中的代碼。 – 2010-10-10 07:16:49

+1

@Vatsaf,我們還需要看看'@ implementation'。 – 2010-10-10 07:18:22

回答

4

printAll方法使用%@而不是%iint變量。

此:

- (void)printAll 
{ 
    NSLog(@"name=%@, year=%@, length=%@, genre=%@", name, year, length, genre); 
} 

應該是這樣的:

- (void)printAll 
{ 
    NSLog(@"name=%@, year=%i, length=%i, genre=%i", name, year, length, genre); 
} 

NSLog()%@用於NSObject s和標準printf格式字符串被用於其它的C原語,例如%i對於int,%u對於unsigned int,%f對於float

1

你有沒有看在:EXC_BAD_ACCESS signal received

該文件特別有幫助對我來說:

http://www.cocoadev.com/index.pl?DebuggingAutorelease

+0

是的,我已經搜索並找到它,但仍然不知道:-( – 2010-10-10 07:18:25

+0

我假設你在xcode中這樣做?如果是這樣,我會說你應該把它放在調試模式,讓事情崩潰,然後從GDB獲取堆棧跟蹤(點擊「bt」進行回溯)。 – smtlaissezfaire 2010-10-10 08:21:05