Objective c NSDictionary和CTypedPtrMap目标C
c++-Objective c NSDictionary和CTypedPtrMap目标C,objective-c,Objective C,c++-CTypedPtrMap Mu cAppMap stTimer*是一个包含5个值的结构,字是无符号短的,它是键。 是否可以在NSDictionary中存储结构对象 stTimer*pEvent; NSDictionary*dictionary=[[NSDictionary alloc]init]; [dictionary setObject:pEvent forKey:wTimerId] 警告: 正在传递setobject的参数1:forKey 来自不兼容的指针类型 编辑 在这种情况下,
CTypedPtrMap Mu cAppMap代码>
stTimer*是一个包含5个值的结构,字是无符号短的,它是键。
是否可以在NSDictionary中存储结构对象
stTimer*pEvent;
NSDictionary*dictionary=[[NSDictionary alloc]init];
[dictionary setObject:pEvent forKey:wTimerId]
警告:
正在传递setobject的参数1:forKey
来自不兼容的指针类型
编辑
在这种情况下,传递的键和值应该是对象。但是键(wTimerId)是一个无符号短int,而不是指针或对象
如何将其作为密钥传递
编辑:
警告1./timer/Timers.m:50:0/timer/Timers.m:50:warning:传递参数3
'CFNumberCreate'的函数从整数生成指针,而不进行强制转换
警告2./timer/Timers.m:52:0/timer/Timers.m:52:warning:传递参数1
来自不兼容指针类型的“CFDictionaryGetCount”的
我按照警告中的说明,将其输入(unsigned short*)作为
WTimeRID是无符号的短。它将给我另一个警告:“从不同大小的整数< /强>整数的Casto指针。< / P> < P>不能仅将指针传递给C++结构的简单结构或实例-<代码> NS字典)/代码>期望Obji-C对象符合COCOA标准。
您可以使用在字典中存储NSValue
实例。
或者,您可以直接创建并相应地设置回调-CFDictionary
是免费桥接到NSDictionary
,因此您可以像以前一样在大部分情况下使用它
编辑:
对于同样的问题适用的密钥,您可以使用NSNumber
来包装它们。此外,您不能对值使用默认的字典回调,而是使用以下方法:
CFDictionaryValueCallBacks cbs = {0, NULL, NULL, NULL, NULL};
CFMutableDictionaryRef cfdict =
CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &cbs);
NSMutableDictionary *dict = (NSMutableDictionary *)cfdict;
// now both the Cocoa approach:
[dict setObject:(id)pEvent forKey:[NSNumber numberWithInt:wTimerId]];
// .. and the CoreFoundation approach work:
CFNumberRef timerId = CFNumberCreate(..., &wTimerId);
CFDictionarySetValue(cfdict, timerId, pEvent);
CFRelease(timerId);
如果您不想包装密钥,您还必须调整密钥回调。@Georg Fritzsche:请您举例说明如何搜索并在其中插入元素。@Beata:您有什么特别的地方吗?你开始阅读文档了吗?无意冒犯,但我已经厌倦了人们期望完整的工作代码,而不是至少自己做一些研究。@Georg Fritzche:很抱歉用我的一点知识来麻烦你。我尝试了你在这里解释的示例,并得到了EXC_BAD_访问错误。我已经将该代码作为编辑包含在上面。请查看。谢谢。@Beata:你从未初始化过pEvent
(例如,使用pEvent=new session;
),然后再开始使用它。另外,如果您遇到崩溃,您应该告诉我们在哪里。@Beata:对不起,因为您没有使用Objective-C++,它应该是pEvent=malloc(sizeof(struct session))代码>
Timers.h
--------
#import <Foundation/Foundation.h>
struct session {
int a;
char c;
int b;
};
@interface Timers : NSObject {
unsigned short wTimerId;
}
-(id)init;
-(void)dealloc;
-(void)timer;
@end
Timers.m
--------
#import "Timers.h"
@implementation Timers
-(id)init
{
wTimerId=91;
return self;
}
-(void)dealloc
{
[super dealloc];
}
-(void)timer
{
struct session* pEvent;
pEvent->a=10;
pEvent->c='A';
pEvent->b=20;
CFDictionaryValueCallBacks cbs = {0,NULL,NULL,NULL,NULL};
CFMutableDictionaryRef cfdict = CFDictionaryCreateMutable(NULL,0,&kCFTypeDictionaryKeyCallBacks,&cbs);
NSMutableDictionary* dict = (NSMutableDictionary*)cfdict;
//Now both the coca approach
[dict setObject:(id)pEvent forKey:[NSNumber numberWithInt:wTimerId]];
//..and the CoreFoundation aproach work
CFNumberRef timerId = CFNumberCreate(NULL,kCFNumberShortType,wTimerId);
CFDictionarySetValue(cfdict,timerId,pEvent);
NSLog(@"Dict size:%d\n",(int)((CFIndex)CFDictionaryGetCount(dict)));
CFRelease(timerId);
main.m
------
#import <Foundation/Foundation.h>
#import "Timers.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Timers* time = [[Timers alloc]init];
[time timer];
[pool drain];
return 0;
}
1.CFNumberRef timerId = CFNumberCreate(NULL,kCFNumberShortType,wTimerId);
2.NSLog(@"Dict size:%d\n",(int)((CFIndex)CFDictionaryGetCount(dict)));
CFDictionaryValueCallBacks cbs = {0, NULL, NULL, NULL, NULL};
CFMutableDictionaryRef cfdict =
CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &cbs);
NSMutableDictionary *dict = (NSMutableDictionary *)cfdict;
// now both the Cocoa approach:
[dict setObject:(id)pEvent forKey:[NSNumber numberWithInt:wTimerId]];
// .. and the CoreFoundation approach work:
CFNumberRef timerId = CFNumberCreate(..., &wTimerId);
CFDictionarySetValue(cfdict, timerId, pEvent);
CFRelease(timerId);