从NSNotificationCenter获取'无法识别的选择器'

从NSNotificationCenter获取'无法识别的选择器'

问题描述:

我试图在NSNotificationCenter中实现一个观察器。除了使用self作为观察者的我想创建一个小东西,做它:从NSNotificationCenter获取'无法识别的选择器'

typedef void (^ErrorCallback)(NSError*); 
typedef void (^SuccessCallback)(); 
typedef void (^ReplicationChanged) (NSNotification*); 

@interface SyncParams : NSObject 

@property (copy) ErrorCallback errorCallback; 
@property (copy) SuccessCallback successCallback; 
@property (copy) ReplicationChanged replicationChanged;//this used to observe 

- (void)replicationChanged:(NSNotification*)notification; 

@end 

@implementation SyncParams 

@end 

再后来,我创建观察者的实例:

SyncParams* params = [SyncParams alloc]; 
params.replicationChanged = ^(NSNotification* notification) { 
    //do stuff here 
}; 

最后将它添加到NSNotificationCenter:

[[NSNotificationCenter defaultCenter] addObserver: params 
             selector: @selector(replicationChanged:) 
              name: kCBLReplicationChangeNotification 
              object: replicationObject]; 

但我得到这个错误:Exception '-[SyncParams replicationChanged:]: unrecognized selector sent to instance 0x7ff945c049a0' was thrown

我对objective-c非常陌生!任何指针?

+0

'replicationChanged:'的实现看起来像什么? – Avi

+0

我没有看到,“replicationChanged:”已实现。 –

+0

是不是这是什么:'params.replicationChanged = ^(NSNotification * notification){ //在这里做东西 }; '? – pomo

是的,没有,我结束了以下内容:

[[NSNotificationCenter defaultCenter] addObserverForName:kCBLReplicationChangeNotification object:repl queue:nil usingBlock:^(NSNotification *notification) { 

    NSString *status; 
    if (repl.status == kCBLReplicationActive) { 
     NSLog(@"Repication in progress"); 
     status = @"in-progrss"; 
    } else if (repl.status == kCBLReplicationOffline) { 
     NSLog(@"Sync in offline"); 
     status = @"offline"; 
    } else if (repl.status == kCBLReplicationStopped) { 
     NSLog(@"Sync in stopped"); 
     status = @"stopped"; 
    } else if (repl.status == kCBLReplicationIdle) { 
     NSLog(@"Sync in idle"); 
     status = @"idle"; 
    } 

    NSError *error = repl.lastError; 
    if(error) { 
     status = @"error"; 
     NSLog(@"replication error %@", error.code); 
    } 

    NSDictionary *dictionary = @{ 
           @"type": type, 
           @"changesCount": @(repl.changesCount), 
           @"completedChangesCount": @(repl.completedChangesCount), 
           @"running": @(repl.running), 
           @"status": status, 
           @"suspended": @(repl.suspended), 
           }; 

    [self sendEventWithName:@"replicationChanged" body:dictionary]; 

}]; 

我只得到一个回调不过,当它启动。