处理程序中未收到ios NSNOTIONG通知

处理程序中未收到ios NSNOTIONG通知,ios,nsnotificationcenter,nsnotifications,Ios,Nsnotificationcenter,Nsnotifications,我用这个作为观察者: NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc addObserver:self selector:@selector(newConnection:) name:NSFileHandleConnectionAcceptedNotification object:nil]; 但当应用程序因此消息崩溃时,会调用此消息 Terminating a

我用这个作为观察者:

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
      selector:@selector(newConnection:)
       name:NSFileHandleConnectionAcceptedNotification
         object:nil];
但当应用程序因此消息崩溃时,会调用此消息

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayerArray newConnection:]: unrecognized selector sent to instance 0x76aed70'
这是当前为空的newConnection处理程序:

- (void)newConnection:(NSNotification*)notification
{
}
它也在.h文件中正确声明。。。

这是调用通知的代码

 socketPort = [[NSSocketPort alloc] initWithTCPPort:portNumber];
    int fd = [socketPort socket];
    fileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fd
                                    closeOnDealloc:YES];
...
[fileHandle acceptConnectionInBackgroundAndNotify];
编辑: 上面的内容都在我的课堂上。 除newConnection之外的所有内容都在此函数中:

- (id)initWithPortNumber:(int)pn delegate:(id)dl
{
if( self = [super init] ) {
...
}
我在viewController中调用了这个文件,如下所示:

    SimpleHTTPServer *server= [[SimpleHTTPServer alloc]initWithPortNumber:80 delegate:self];
解决方案:

问题是:由于arc系统[fileHandle acceptConnectionInBackgroundAndNotify]的原因,视图被解除分配;并不是一个arc检测到的循环,它将视图视为空闲并自动解除分配,所以我只制作了一个小计时器,每秒钟运行一个循环,导致一个空方法。它准确地告诉您错误是什么:
'NSInvalidArgumentException',原因:'-[CALayerArray newConnection:]:未识别的选择器发送到实例0x76aed70'

您需要问的问题是“为什么对象位于
0x76aed70
a
CALayerArray
?”

从基本知识开始。在
addObserver:…
设置断点,确保
self
是您认为的。然后设置该指针的轨迹,以便调试器在其值更改时中断。你应该能很快找到它


我猜您的对象已解除分配,但您没有删除观察者。

问题是:由于arc系统,视图已解除分配
[fileHandle acceptConnectionInBackgroundAndNotify];并不是一个arc检测到的循环,它将视图视为空闲并自动解除分配,所以我只制作了一个小计时器,每秒钟运行一个循环,导致一个空方法。哪一个修复了它。

第一个片段中的
self
是什么?另外,不相关,但避免方法名称中出现
new
,因为它可能会干扰ARC内存管理(不是这种情况,但可能是)。它使当前类成为观察者。。。在我看到的每一个观察者身上,他们都使用了self,它工作得非常完美……是什么让self在观察时保持活着?有多少观察家?只有一个观察家,就是那个观察家,自我是我从最初的观点调用的一个类。我正在编辑代码,以显示调用通知的内容。我发现“CALayerArray”实际上是“self”,我不知道它的意思,尽管我在第一篇文章中更新了大部分代码,也许这会提供线索……找到解决方案。更好的解决方案可能是强烈地保留您的视图,这样ARC就不会取消分配它#只是说……;)