ios - NSNotificationCenter 发布 C 结构并从通知回调函数中检索它

标签 ios objective-c struct nsnotificationcenter nsnotification

我有一个复杂的C结构,很难将这个结构一一转换为NSDictionary。 如何通过 NSNotificationCenter 发布此 C 结构并从 NSNotificationCenter 回调函数中检索它? 任何建议将不胜感激,谢谢!

最佳答案

以 NSValue 形式存储和检索

商店:

CustomStruct instanceOfCustomStruct = ...;
NSValue * valueOfStruct = [NSValue valueWithBytes:&instanceOfCustomStruct objCType:@encode(CustomStruct)];

[[NSNotificationCenter defaultCenter] postNotificationName:@"YourNotification" object:self userInfo:@{@"CustomStructValue" : valueOfStruct}];

检索:

NSValue * valueOfStruct = note.userInfo[@"CustomStructValue"];
CustomStruct instanceOfCustomStruct;
[valueOfStruct getValue:&instanceOfCustomStruct];

关于ios - NSNotificationCenter 发布 C 结构并从通知回调函数中检索它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23283823/

相关文章:

ios - 让 UIScrollView 放弃 UITouch

ios - 尝试了解 iOS 8 中的 UIScrollView 自动布局

ios - 在通知中心检测特定应用推送通知

ios - POST 请求未考虑 NSMutableURLRequest 超时间隔

iphone - AVAssetExportSession在iPad上有效,在iPhone上无音频

c++ - 在 C++ 中使用 #define 定义位标志

c - 指向结构内部结构的指针;如何访问不出错?

C++:将未知 union 传递给函数作为参数

ios - 虚线边框 UIImageView swift

iphone - 使用 [ALAssetsLibrary enumerateGroupsWithTypes :] 时的 Cocoa 线程同步