Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSNotificationCenter的职位导致;EXC“不良访问”;例外_Ios_Objective C_Iphone_Exc Bad Access_Nsnotificationcenter - Fatal编程技术网

Ios NSNotificationCenter的职位导致;EXC“不良访问”;例外

Ios NSNotificationCenter的职位导致;EXC“不良访问”;例外,ios,objective-c,iphone,exc-bad-access,nsnotificationcenter,Ios,Objective C,Iphone,Exc Bad Access,Nsnotificationcenter,UIViewController将自身添加到默认中心: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editFood) name:@"editFood" object:nil]; 然后一个UITableView委托NSObject发布一个NSNotification: [[NSNotificationCenter defaultCenter] postNotificationNam

UIViewController
将自身添加到默认中心:

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(editFood)
 name:@"editFood"
 object:nil];
然后一个
UITableView
委托NSObject发布一个
NSNotification

[[NSNotificationCenter defaultCenter]
 postNotificationName:@"editFood"
 object:self];
在运行时,它会得到一个异常


defaultCenter
是否在某处发布?当我从UIViewController向UIViewController发布通知时,同样的概念也适用,但这不重要,对吧?

您的一个订户已被解除分配。确保在您的dealloc中调用
[[NSNotificationCenter defaultCenter]removeObserver:self]

即使在验证dealloc是否存在后,也可能发生错误访问

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self]
}
上述方法在大多数情况下都能解决问题,但显然我的原因是我间接添加了一个
选择器:
设置为
nil
,如下所示:

[NSNotificationCenter.defaultCenter addObserver:self
                                         selector:nil
                                             name:notificationName
                                           object:nil];
…因此,当我发布带有
通知名的内容时,
出现了错误访问


解决方案是发送一个选择器,它实际上指向某个对象。

我在Swift中遇到了同样的问题。问题是函数目标有一个带有默认值的
closure
参数:

@objc func performFoo(completion: (() -> Void)? = nil) {
   ...
}
在我将
关闭
参数替换为
通知
参数后,它工作了:

@objc func performFoo(notification: Notification) {
    ...
}

我必须进行一些重构以使其以正确的方式工作。

它到底在哪里崩溃?在场景中向
[[NSNotificationCenter defaultCenter]removeObserver:self]
添加
-(void)dealloc{}
方法可能会为您解决这个问题。它为我工作;我和你有同样的问题。祝你好运谢谢,我刚刚意识到我的错误(在看了这个并研究了四个小时之后)。调用释放后我试图引用的对象。调试器只是让它看起来像是抛出EXC_BAD_访问异常的地方。@Paul:僵尸工具在调试此类问题时非常有用。@Sven谢谢,我很感激。事实上我试过一次,但不知道怎么用。我在项目plist中添加了一个环境变量,但这不起作用。我在发出名为“我得到了EXC_BAD_ACCESS”的通知后释放该对象。我如何解决这个问题?EXC_BAD_ACCESS不是一个例外,它是无效的内存访问p,只是我自己发现的,我将
NULL
作为选择器,打算将其作为临时的,比如,哦,让我去创建那个方法,然后我忘了稍后更新选择器参数(DOH),是的,EXC_BAD_访问结果。