2009-12-13 89 views
1

我想創建一個NSMutableDictionary與整數映射到一個結構(結構)。 例如:在Objective-c的NSDictionary中用「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]; 

這是我想要做的嗎?但是,因爲結構不是一個對象,它會拋出警告? 任何方式,我可以創建一個字典與結構。 還是有其他方法來創建一個結構的地圖?

請回復很快.....

謝謝 普拉迪普。

回答

12

NSValue看看:

[pSampleMap setObject:[NSValue value:&sObject withObjCType:@encode(sSampleStruct)] forKey:[NSNumber numberWithInt:nInt]]; 

此外,你有在[[NSNumber alloc] initWithInt:nVar]內存泄漏和你的代碼甚至不會請編譯以來的Objective-C是區分大小寫的。

+0

嘿謝謝..... tat真的是一個快速的回覆......它的工作原理。 我寫的樣本只是爲了明確我的要求。 感謝您指出內存泄漏。 –

8

你是對的,NSDictionary中的鍵和值都必須是對象。你在找什麼是NSValue

the docs

一個NSValue對象爲單C或Objective-C的數據項的簡單容器。它可以保存任何標量類型,如int,float和char,以及指針,結構和對象標識符。此類的目的是允許將這些數據類型的項添加到集合中,例如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]]; 
+0

嗨,我很滿意你的回答方式... 非常感謝.... 我會需要很多幫助...因爲我需要完成一個tool.by本週結束..希望這個論壇能幫助我。 –

1

可以使用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 
1

可以使用的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 
*/ 
1

,或者如果你熟悉C++ :

std::map<sSampleStruct,int> myDictionary;