NSNotification EXC_BAD_ACCESS

Big*_*igT 5 xcode exc-bad-access nsnotificationcenter

这是我得到的错误

Thread 1:EXC_BAD_ACCESS (code=2, address=0xb7ffffc)
Run Code Online (Sandbox Code Playgroud)

在这条线上

[[NSNotificationCenter defaultCenter] postNotificationName:AsyncImageLoadDidFinish
                                                        object:target
                                                      userInfo:[[userInfo copy] autorelease]];
Run Code Online (Sandbox Code Playgroud)

在AsyncImageView.m文件中.

该错误会停止代码,但如果我继续在调试器中冻结Xcode并将其关闭.我该如何解决这个问题?

Gur*_*uru 15

在init中你需要注册,在dealloc你需要注册!

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:AsyncImageLoadDidFinish  object:nil];
Run Code Online (Sandbox Code Playgroud)

要么

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
Run Code Online (Sandbox Code Playgroud)


Kju*_*uly 3

尝试一下下面的代码,应该没问题:

NSDictionary * userInfo = [NSDictionary dictionaryWithObjectsAndKeys:..., nil];
[[NSNotificationCenter defaultCenter] postNotificationName:AsyncImageLoadDidFinish
                                                    object:target
                                                  userInfo:userInfo];
Run Code Online (Sandbox Code Playgroud)

或者:

NSDictionary * userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:..., nil];
[[NSNotificationCenter defaultCenter] postNotificationName:AsyncImageLoadDidFinish
                                                    object:target
                                                  userInfo:userInfo];
[userInfo release];
Run Code Online (Sandbox Code Playgroud)