通过c++抽象层实现ARC Objective-C委托
ARC Objective-C delegation through a C++ abstract layer
我正在编程简单的跨平台c++层(dylib),由Objective-C实现。特定于平台的部分通过平台宏包含。
NSUserNotificationCenter需要委托模式来处理特定的操作,例如单击通知。我面临的问题是,只要我执行send
,通知被发送,但实例卸载之后。因此,onclick通知动作永远不会被调用(didActivateNotification),而是因为一个坏的指针而崩溃。我该怎么做呢?
注意:SomeObjectiveC.mm
是一个类位于我的应用程序。AppleUserNotificationManager.m
是由NotificationManager+apple.mm
初始化的,它们都位于我的Dylib中。
SomeObjectiveC.mm
Notification *notification = new Notification;
notification->setTitle("Foo bar notification");
notification->setMessage("Hello world!");
NotificationManager *notificationManager = new NotificationManager;
notificationManager->send(notification);
NotificationManager + apple.mm
#include "notificationManager+apple.hpp"
bool NotificationManager::send(Notification *notification)
{
AppleUserNotificationManager *notificationManager = [[AppleUserNotificationManager alloc] init];
NSString *title = [NSString stringWithUTF8String:notification->getTitle().c_str()];
NSString *message = [NSString stringWithUTF8String:notification->getMessage().c_str()];
if (notification->getSoundName().empty()) {
[notificationManager sendWithTitle:title andMessage:message];
}
NSString *soundName = [NSString stringWithUTF8String:notification->getSoundName().c_str()];
[notificationManager sendWithTitle:title andMessage:message andSound: soundName];
return true;
}
AppleUserNotificationManager.m
#import "AppleUserNotificationManager.h"
@implementation AppleUserNotificationManager
@synthesize userNotification;
- (id)init
{
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate: self];
userNotification = [[NSUserNotification alloc] init];
self = [super init];
return self;
}
/**
* @param NSUserNotificationCenter center
* @param NSUserNotification notification
*
* @return bool
*/
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification{
return YES;
}
/**
* @param NSUserNotificationCenter center
* @param NSUserNotification notification
*/
- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
NSString *notificationText = [notification informativeText];
NSString *urlRegEx = @"(http|https)://((\w)*|([0-9]*)|([-|_])*)+([\.|/]((\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
if ([urlTest evaluateWithObject:notificationText]) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:notificationText]];
}
}
/**
* @param NSString title
* @param NSString message
* @param NSString soundName
*/
- (void)sendWithTitle:(NSString *)title andMessage:(NSString *)message andSound:(NSString *)soundName{
userNotification.title = title;
userNotification.informativeText = message;
userNotification.soundName = soundName;
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification: userNotification];
}
@end
在分配前使用self
指针?
下面的改变能解决问题吗?
- (id)init
{
self = [super init];
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate: self];
userNotification = [[NSUserNotification alloc] init];
return self;
}
相关文章:
- 如何将数组从Objective-C发送到C++函数
- 在 Objective-C++ -> EXC_BAD_ACCESS Error 中使用"new"关键字
- Mac Snow Leopard 上的 Qt 5.3.2:ld:在架构x86_64 [clang、Objective-C
- 创建跨平台 C++ 触摸管理器.在 c++ 中传递 Objective-c 对象涉及代码
- C++对象作为 Objective-C++ 属性
- 将.cpp文件中的数据传递给Objective c ViewController
- 为什么这个来自 Objective-C++ 的dynamic_cast在调试中成功但在发布时失败?
- Objective-C 如何使用向量对?
- 在 Objective-C 和 Swift 之间委托问题
- Objective-C C++ 包装器 类型错误的不完整定义
- EXC_BAD_ACCESS Xcode,来自我的 Objective-C++ 和 swift 桥接标头中的 C++ 包
- ARC 在 Objective-C++ 中不起作用
- Objective-C 集成 C 库并传递字符串值 - ARC 错误
- Objective-C ARC 指针所有权与C++
- 如何在启用ARC的情况下将Objective-C代码转换为C++
- 在ARC中不允许对Objective-C指针进行隐式转换
- Objective-C:如果我在ARC的函数中返回一个c++对象,它会创建一个副本吗?我是否必须手动释放该副本?
- 通过c++抽象层实现ARC Objective-C委托
- Objective-C ARC:使用block作为c++回调是正确的吗?
- 在c++对象中使用ARC和Objective C对象会发生什么?