[英]Create a Map with “Struct” in NSDictionary in Objective-c?
我想创建一个NSMutableDictionary,其中有一个整数映射到strucuter(struct)。 例:
int nVar = 1;
typedef struct
{
NSString *pstrName;
}sSampleStruct;
sSampleStruct *sObject = {@"test"};
NSMutableDictioary *pSampleMap = [[NSMutableDictioary allo] init];
[pSampleMap setObject:sObject forKey:[[nsnumber alloc] initwithint:nVar];
这就是我要做什么? 但是由于struct不是对象,因此它会发出警告吗? 我可以用任何方法创建带有结构的字典。 还是通过其他方式创建具有结构的地图?
请尽快回复.....
谢谢Pradeep。
看一下NSValue :
[pSampleMap setObject:[NSValue value:&sObject withObjCType:@encode(sSampleStruct)] forKey:[NSNumber numberWithInt:nInt]];
另外,您在[[NSNumber alloc] initWithInt:nVar]
中发生内存泄漏,并且由于Objective-C区分大小写,因此您的代码甚至无法编译。
您说对了,因为NSDictionary中的键和值都必须是对象。 您正在寻找的是NSValue
。
从文档 :
NSValue对象是单个C或Objective-C数据项的简单容器。 它可以容纳任何标量类型,例如int,float和char,以及指针,结构和对象id。 此类的目的是允许将此类数据类型的项目添加到集合中,例如NSArray和NSSet实例,它们要求它们的元素成为对象。 NSValue对象始终是不可变的。
有点像(稍微清理一下代码):
typedef struct
{
NSString *pstrName;
} sSampleStruct;
// Changed sObject to non-pointer so it can be initialized with a literal
sSampleStruct sObject = {@"test"};
// Corrected spelling of NSMutableDictionary
NSMutableDictionary *pSampleMap = [[NSMutableDictionary alloc] init];
// Changed to [NSNumber numberWithInt:] to avoid leaking memory
// Corrected spelling and capitalization of NSNumber
[pSampleMap setObject:[NSValue valueWithPointer:&sObject]
forKey:[NSNumber numberWithInt:nVar]];
您可以使用NSData来包装您的结构。 以下是我的解决方案,它对我的工作:
// this is complex structure SIO2Object. Use you custom initialization instead of this
SIO2object *el = ( SIO2object * )sio2ResourceGetObject(sio2->_SIO2resource, @"User/11/..." );
NSMutableDictionary *elements = [NSMutableDictionary dictionary];
NSData *elData = [NSData dataWithBytes:&el length:sizeof(el)];
[elements setObject:elData forKey:@"first"];
SIO2object *newEl = nil;
NSData *newData = [elements objectForKey:@"first"];
[newData getBytes:&newEl];
//there newEl is equal to el
您可以使用NSDictionary,但是这些功能仅可通过CF接口使用:
/* define our custom key callbacks */
CFDictionaryKeyCallBacks keyCallbacks;
keyCallbacks.version = 0; /* default */
keyCallbacks.retain = 0; /* no retain performed */
keyCallbacks.release = 0; /* no release performed */
keyCallbacks.copyDescription = 0; /* potentially applicable */
keyCallbacks.equal = 0; /* potentially applicable */
keyCallbacks.hash = 0; /* potentially applicable */
/* note: if you prefer to use NSNumber for keys,
then you should use the default key callbacks.
this illustrates what you could do if you wanted to use int keys.
*/
/* define our custom value callbacks */
CFDictionaryValueCallBacks valueCallbacks;
/* see per-field comments at keyCallbacks */
valueCallbacks.version = 0;
valueCallbacks.retain = 0;
valueCallbacks.release = 0;
valueCallbacks.copyDescription = 0;
valueCallbacks.equal = 0;
CFAllocatorRef allocator = kCFAllocatorDefault;
CFIndex capacity = 0; /* no hint in this example - resizes as needed */
NSMutableDictionary * myNewDictionary = (NSMutableDictionary*)
CFDictionaryCreateMutable(allocator, capacity, &keyCallbacks, &valueCallbacks);
/*
for insertions/values/keys:
- use a pointer to the key/value if the value is larger than a pointer.
this may require dynamic memory allocation and use of reference counting
since the keys/values must live in memory as long as the dictionary exists
*/
或者如果您熟悉c ++:
std::map<sSampleStruct,int> myDictionary;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.