【问题标题】:Objective-C error EXC_BAD_ACCESS help pleaseObjective-C 错误 EXC_BAD_ACCESS 请帮忙
【发布时间】:2010-08-20 18:18:46
【问题描述】:

我目前正在使用 Objective-C 上的实用截屏视频来帮助我使用 Objective-c 进行编程。我有 Java 和 C++ 的背景,但是我很难适应 Objective 中的所有内容(主要是因为我对语法不满意)。 以下是我收到的所有代码错误。 我也在 movie.m 类中收到警告:Wirtable atomic property 'title' 不能将合成的 setter/getter 与用户定义的 setter/getter 配对

感谢您的帮助。

我收到此错误

Current language:  auto; currently objective-c
warning: Couldn't find class validation function, calling methods on uninitialized objects may deadlock your program.
Program received signal:  “EXC_BAD_ACCESS”.

我通过调试器运行它,下面代码中电影的地址是红色的

main.m

int main (int argc, const char * argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

Movie *movie = [[Movie alloc] initWithTitle:@"iron man"
                                andRating:5
                                    andYear:2008];

[movie play];
NSLog(@"our movie is %@", movie);

[pool drain];
return 0;}

电影.h

    interface Movie : NSObject {
    NSString *title;
    int rating;
    int year;
}
- (id)initWithTitle:(NSString *)newTitle
          andRating:(int)newRating
            andYear:(int) year;

@property(assign) NSString *title;
@property(assign) int rating;
@property(assign) int year;
-(void) play;

@end

电影.m

    #import "Movie.h"


@implementation Movie

@synthesize title;
@synthesize rating;
@synthesize year;
-(id)initWithTitle:(NSString *)newTitle
                    andRating:(int)newRating
           andYear:(int)newYear;
{

    self = [super init];
    if(nil != self){
        self.title = newTitle;
        self.rating = newRating;
        self.year = newYear;
    }
    return self;

}
-(NSString *) description{
    NSString *oldDescription = [super description];

    return [NSString stringWithFormat: @"%@ title =%@, rating =%d year=%@",
            oldDescription, self.title, self.rating, self.year];
}
- (void)setTitle:(NSString *)newTitle {
    title = [newTitle capitalizedString];
}
-(void) play {
    NSLog(@"Playing %@", self);
}

【问题讨论】:

    标签: objective-c exc-bad-access


    【解决方案1】:

    您使用year=%@ 而应该是year=%d

    一些更随意的想法:

    你应该保留或更好地复制标题而不是分配它。

    init方法应该命名为

    -(id)initWithTitle:(NSString *)aTitle
                        rating:(int)aRating
                        year:(int)aYear;
    

    那么不要忘记dealloc 方法。

    【讨论】:

    • 感谢您的帮助。它现在有效,我还没有达到dealloc的目的,但谢谢。我一直在犯这个错误。
    【解决方案2】:

    您的title 属性是一个对象类型,因此通常应该是retaincopy——在NSString 属性的情况下,传统上使用copy 以避免出现问题' 改为通过 NSMutableString

    @property (copy) NSString* title;
    

    由于您明确定义了 setter,因此您需要自己实现此策略,如下所示:

    - (void)setTitle:(NSString *)newTitle
    {
        [title release];
        title = [[newTitle capitalizedString] copy];
    }
    

    您还需要包含一个dealloc 方法来清理:

    - (void) dealloc
    {
        [title release];
        [super dealloc];
    }
    

    【讨论】:

    • 谢谢,我会调查的,相信会改的;我只经历了第一部分,第二部分地址内存管理。
    • @seanb11 您应该单击 Eiko 答案旁边的小箭头轮廓以确认它是正确的。并享受下一部分——希望随着你的继续,上面的内容会更有意义:)
    猜你喜欢
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多