【问题标题】:NSMetadataQueryDidUpdateNotification Not WorkingNSMetadataQueryDidUpdateNotification 不工作
【发布时间】:2013-04-30 16:56:36
【问题描述】:

谁能告诉我以下我改编自 iOS 5 Developer's Cookbook 的代码有什么问题

- (void) startMonitoringUbiquitousDocumentsFolder
{
    // Remove any existing query – stored in local instance variable
    if (alertQuery) [alertQuery stopQuery];

    // Search for all file names
    alertQuery.predicate = [NSPredicate predicateWithFormat: @"NSMetadataItemFSNameKey == '*'"];
    alertQuery.searchScopes = [NSArray arrayWithObject:
   NSMetadataQueryUbiquitousDocumentsScope];

    // Subscribe to query updates

    [[NSNotificationCenter defaultCenter]
             addObserver:self
             selector:@selector(alertUserToUpdates:)
             name:NSMetadataQueryDidUpdateNotification
             object:nil];

    [alertQuery startQuery];
}

- (void) alertUserToUpdates
{
    NSLog(@"Contents changed in ubiquitous documents folder");

    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Home Monitor"
                              message:@"Something's going on."
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:@"Check Recording", nil];
    [alertView show];
    [self stopMonitoringUbiquitousDocumentsFolder];
}

我收到一个异常,提示选择器 alertUserToUpdates: 无法识别。为什么它不能识别明明就在它旁边的方法?

2013-04-29 18:07:22.458 eyeFun[8231:907] -[RGPViewController alertUserToUpdates:]: unrecognized selector sent to instance 0x1fd506a0
2013-04-29 18:07:22.462 eyeFun[8231:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RGPViewController alertUserToUpdates:]: unrecognized selector sent to instance 0x1fd506a0'
*** First throw call stack:
(0x337eb3e7 0x3b4e6963 0x337eef31 0x337ed64d 0x33745208 0x3373c349 0x34053b7f 0x340ab327 0x3373c349 0x337b810f 0x37880de9 0x340fa657 0x337c0857 0x337c0503 0x337bf177 0x3373223d 0x337320c9 0x3731133b 0x3564e2b9 0x465b5 0x3b913b20)
libc++abi.dylib: terminate called throwing an exception

【问题讨论】:

    标签: ios icloud


    【解决方案1】:

    因为它不在旁边。

    您告诉它使用名为alertUserToUpdates: 的选择器。您有一个名为alertUserToUpdates 的方法。 : 很重要——它表示该方法接受一个参数,但实际方法不接受参数。

    您的方法应采用NSNotification 类型的参数。

    【讨论】:

    • 也可以使用@selector(alertUserToUpdates) 更新选择器以指向您的方法——通知中心不介意调用不带NSNotification 参数的方法。但大多数情况下,接受参数是一个更好的主意。
    • 感谢两位回复。感谢您的帮助。
    猜你喜欢
    • 2016-02-04
    • 2014-02-22
    • 2013-10-20
    • 2013-06-12
    • 2014-04-10
    • 1970-01-01
    • 2021-01-10
    • 2012-12-12
    • 1970-01-01
    相关资源
    最近更新 更多