如何將NSArray
轉換爲NSDictionary
,使用數組對象的int字段作爲NSDictionary
的關鍵字?將NSArray轉換爲NSDictionary
41
A
回答
51
- (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array
{
id objectInstance;
NSUInteger indexKey = 0U;
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];
for (objectInstance in array)
[mutableDictionary setObject:objectInstance forKey:[NSNumber numberWithUnsignedInt:indexKey++]];
return (NSDictionary *)[mutableDictionary autorelease];
}
9
這會給NSArray
增加一個類別擴展名。需要C99
模式(這是目前的默認模式,但以防萬一)。
在.h
文件的地方,可以是#import
ED由所有..
@interface NSArray (indexKeyedDictionaryExtension)
- (NSDictionary *)indexKeyedDictionary
@end
在.m
文件..
@implementation NSArray (indexKeyedDictionaryExtension)
- (NSDictionary *)indexKeyedDictionary
{
NSUInteger arrayCount = [self count];
id arrayObjects[arrayCount], objectKeys[arrayCount];
[self getObjects:arrayObjects range:NSMakeRange(0UL, arrayCount)];
for(NSUInteger index = 0UL; index < arrayCount; index++) { objectKeys[index] = [NSNumber numberWithUnsignedInteger:index]; }
return([NSDictionary dictionaryWithObjects:arrayObjects forKeys:objectKeys count:arrayCount]);
}
@end
實施例使用:
NSArray *array = [NSArray arrayWithObjects:@"zero", @"one", @"two", NULL];
NSDictionary *dictionary = [array indexKeyedDictionary];
NSLog(@"dictionary: %@", dictionary);
輸出:
2009-09-12 08:41:53.128 test[66757:903] dictionary: {
0 = zero;
1 = one;
2 = two;
}
-2
我創建了一個簡單的庫,名爲Linq to ObjectiveC,它是使這類問題更容易解決的一組方法。在你的情況,你需要的Linq-to-ObjectiveC toDictionary方法,其中通過按鍵選擇指定你的「廉政」字段:
NSDictionary* dictionary = [sourceArray toDictionaryWithKeySelector:^id(id item) {
return [item intField];
}];
這將返回它的鍵由intField
源數組中給出的字典。
71
試試這個神奇:
NSDictionary *dict = [NSDictionary dictionaryWithObjects:records
forKeys:[records valueForKey:@"intField"]];
FYI這是可能的,因爲該內置功能:
@interface NSArray(NSKeyValueCoding)
/* Return an array containing the results of invoking -valueForKey:
on each of the receiver's elements. The returned array will contain
NSNull elements for each instance of -valueForKey: returning nil.
*/
- (id)valueForKey:(NSString *)key;
+0
這真是一種神奇!優雅和真正方便的解決方案!請注意:也許最好使用[記錄valueForKey:@「otherField」]不只是記錄,因爲它也會包含「intField」。無論如何,即使經過多年的編碼,它對我來說都是新的... – BootMaker 2018-01-19 16:02:39
1
- (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array
{
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];
[array enumerateObjectsUsingBlock:
^(id obj, NSUInteger idx, BOOL *stop){
NSNumber *index = [NSNumber numberWithInteger:idx];
[mutableDictionary setObject:obj forKey:index];
}];
NSDictionary *result = [NSDictionary.alloc initWithDictionary:mutableDictionary];
return result;
}
2
這是從員工列表NSMutableArray
創建NSMutableDictionary
的例子:
NSMutableArray *emloyees = [[NSMutableArray alloc]initWithObjects:@"saman",@"Ruchira",@"Rukshan",@"ishan",@"Harsha",@"Ghihan",@"Lakmali",@"Dasuni", nil];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSString *word in emloyees) {
NSString *firstLetter = [[word substringToIndex:1] uppercaseString];
letterList = [dict objectForKey:firstLetter];
if (!letterList) {
letterList = [NSMutableArray array];
[dict setObject:letterList forKey:firstLetter];
}
[letterList addObject:word];} NSLog(@"dic %@",dict);
相關問題
- 1. 將NSDictionary轉換爲NSArray
- 2. 將NSArray轉換爲NSString包含NSDictionary
- 3. iOS OC,如何將RLMObject轉換爲NSDictionary,NSArray?
- 4. 將包含NSDictionary對象的NSArray轉換爲IOS中的單個NSDictionary
- 5. 將NSDictionary字符串轉換爲NSDictionary
- 6. NSDictionary到NSArray不轉換(信號SIGBRT)
- 7. IOS - 轉換NSArray的NSDictionary的到
- 8. 轉換的NSArray到NSDictionary中的切片
- 9. 轉換NSArray的NSDictionary的到 - 錯誤
- 10. 將NSObject轉換爲NSDictionary
- 11. 將NSDictionary轉換爲XML
- 12. 將NSDictionary轉換爲std :: vector
- 13. 將NSDictionary轉換爲NSString
- 14. 將NSDictionary轉換爲UITableView
- 15. 將NSDictionary轉換爲JSON
- 16. 將對象轉換爲NSDictionary
- 17. 將NSData轉換爲NSDictionary
- 18. 將nsdictionary轉換爲nsdata
- 19. 將NSDictionary值轉換爲NSString
- 20. 將NSString轉換爲NSDictionary
- 21. 將JSON轉換爲NSArray
- 22. 將NSArray值轉換爲int
- 23. 將std:vector轉換爲NSArray
- 24. 將NSArray轉換爲CLLCoordinate
- 25. 將JavaUtilList轉換爲NSArray?
- 26. 將NSArray轉換爲MKPlacemark
- 27. 將PHP JSON轉換爲NSArray
- 28. 將NSString轉換爲NSArray
- 29. 將NSArray轉換爲NSSortDescriptor
- 30. 爲什麼從pList加載一個NSArray將我的數組轉換爲NSDictionary?
我喜歡使用類別,所以我會優先考慮這個解決方案,而不是使用簡單的函數,因爲它直接在對象本身上調用它,就好像它總是知道該方法一樣。 – Woofy 2009-09-19 12:10:05