2012-07-18 61 views
5

這裏是我得到NSNotification EXC_BAD_ACCESS

Thread 1:EXC_BAD_ACCESS (code=2, address=0xb7ffffc) 

在此行中

[[NSNotificationCenter defaultCenter] postNotificationName:AsyncImageLoadDidFinish 
                 object:target 
                 userInfo:[[userInfo copy] autorelease]]; 

在AsyncImageView.m文件中的錯誤。

該錯誤會停止代碼,但如果我繼續在調試器中凍結Xcode並將其關閉。我該如何解決這個問題?

+0

你怎麼申報'userInfo'? – Kjuly 2012-07-18 14:24:20

+0

'\t \t的NSMutableDictionary * USERINFO = [的NSMutableDictionary dictionaryWithObjectsAndKeys: \t \t \t \t \t \t \t \t \t \t圖像,AsyncImageImageKey, \t \t \t \t \t \t \t \t \t \t URL,AsyncImageURLKey, \t \t \t \t \t \t \t \t \t \t零]' – BigT 2012-07-18 14:29:27

回答

3

嘗試下面的代碼,它應該沒問題:

NSDictionary * userInfo = [NSDictionary dictionaryWithObjectsAndKeys:..., nil]; 
[[NSNotificationCenter defaultCenter] postNotificationName:AsyncImageLoadDidFinish 
                object:target 
                userInfo:userInfo]; 

或:

NSDictionary * userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:..., nil]; 
[[NSNotificationCenter defaultCenter] postNotificationName:AsyncImageLoadDidFinish 
                object:target 
                userInfo:userInfo]; 
[userInfo release]; 
+0

這一工程,但我也有一個新的AsyncImagView.m而行成了這個'[NSNotificationCenter defaultCenter] postNotificationName:AsyncImageLoadDidFinish \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t object:_target \t \t \t \t \t \t \t \t \t \t \t \t \t \t用戶信息:[USERINFO複製]自動釋放];' – BigT 2012-07-18 15:21:14

+0

@BigT對不起,我不是你said..Have你解決這個問題很清楚? – Kjuly 2012-07-18 15:26:26

+0

是的,我有。這條線的工作,但我更新了AsyncImageView.m,而該行在那裏。兩者都有效。 – BigT 2012-07-18 15:31:32

15

在初始化你需要註冊,並在dealloc中,你需要取消註冊!

-(void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:AsyncImageLoadDidFinish object:nil]; 

OR

- (void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
}