【发布时间】:2022-01-10 00:18:53
【问题描述】:
不太习惯Objective C,因此尝试用CFNotificationCenterAddObserver()方法观察C++代码中的NSWorkspaceDidActivateApplicationNotification事件。
#include<CoreFoundation/CFNotificationCenter.h>
#include<iostream>
#include<QGuiApplication>
void Callback (CFNotificationCenterRef center, void* observer, CFNotificationName name,
const void* object,
CFDictionaryRef userInfo)
{
std::cout << "Hello World\n";
//NSLog(@"New application: %@", [[name userInfo] objectForKey:NSWorkspaceApplicationKey]);
}
int main (int argc, char** argv)
{
QGuiApplication application(argc, argv);
std::cout << "Add observer\n";
CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(), nullptr, Callback,
CFStringRef("NSWorkspaceDidActivateApplicationNotification"), nullptr,
CFNotificationSuspensionBehaviorDeliverImmediately);
std::cout << "Added ...\n";
return application.exec();
}
但是它会导致以下崩溃。如果我尝试在调试模式下运行,它有时会显示以下崩溃或有时会卡在该函数本身。
Add observer
2021-12-03 21:44:24.527086+0530 Test[17288:982994] -[__NSTaggedDate length]: unrecognized selector sent to instance 0x100007c65
2021-12-03 21:44:24.527916+0530 Test[17288:982994] [General] An uncaught exception was raised
2021-12-03 21:44:24.527943+0530 Test[17288:982994] [General] -[__NSTaggedDate length]: unrecognized selector sent to instance 0x100007c65
2021-12-03 21:44:24.528044+0530 Test[17288:982994] [General] (
0 CoreFoundation 0x00007fff35570035 __exceptionPreprocess + 256
1 libobjc.A.dylib 0x00007fff5fc80a17 objc_exception_throw + 48
2 CoreFoundation 0x00007fff355e9fce -[NSObject(NSObject) __retain_OA] + 0
3 CoreFoundation 0x00007fff35511e9f ___forwarding___ + 1485
4 CoreFoundation 0x00007fff35511848 _CF_forwarding_prep_0 + 120
5 CoreFoundation 0x00007fff355aca4c __CFXNotificationRegisterObserver + 588
6 CoreFoundation 0x00007fff3548a037 CFNotificationCenterAddObserver + 204
7 Test 0x00000001000066a1 main + 129
8 libdyld.dylib 0x00007fff614503d5 start + 1
)
2021-12-03 21:44:24.528241+0530 Test[17288:982994] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSTaggedDate length]: unrecognized selector sent to instance 0x100007c65'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff35570035 __exceptionPreprocess + 256
1 libobjc.A.dylib 0x00007fff5fc80a17 objc_exception_throw + 48
2 CoreFoundation 0x00007fff355e9fce -[NSObject(NSObject) __retain_OA] + 0
3 CoreFoundation 0x00007fff35511e9f ___forwarding___ + 1485
4 CoreFoundation 0x00007fff35511848 _CF_forwarding_prep_0 + 120
5 CoreFoundation 0x00007fff355aca4c __CFXNotificationRegisterObserver + 588
6 CoreFoundation 0x00007fff3548a037 CFNotificationCenterAddObserver + 204
7 Test 0x00000001000066a1 main + 129
8 libdyld.dylib 0x00007fff614503d5 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
如何解决这个问题?
【问题讨论】:
-
该错误意味着在某些时候您认为您正在使用
NSString对象,并在其上调用length(内部调用它,隐藏Apple 的SDK 代码)方法。但实际上它是一个NSDate(或内部方法说NSTaggedDate)对象,它不知道length方法并崩溃。 -
@Larme,那么应该做些什么来解决这个错误。介意发布答案吗?
-
NSWorkspace有自己的notificationCenter。
标签: c++ objective-c qt nsnotificationcenter core-foundation