开发者

Objective C: "EXC_BAD_ACCESS" error

I am a newbie, writing a simple program and while there are no warnings/errors during compilation. I am getting a "EXC_BAD_ACCESS" error. Will appreciate any help with this:

    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;
}

Here is the moviesdatabase class and movies class which it is inheriting:

@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

Including the implementation of 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


Your printAll method uses %@ instead of %i for int variables.

This:

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

Should look like this:

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

In NSLog(), %@ is used for NSObjects, and standard printf format strings are used for other C primitives, e.g. %i for int, %u for unsigned int, %f for float.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜