Làm cách nào để chuyển đổi NSArray
thành một số NSDictionary
, sử dụng trường int của đối tượng mảng làm khóa cho NSDictionary
?Chuyển đổi NSArray thành NSDictionary
Trả lời
- (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];
}
Điều này thêm tiện ích mở rộng danh mục vào NSArray
. Cần chế độ C99
(mặc định là những ngày này, nhưng chỉ trong trường hợp).
Trong một tập tin .h
đâu đó rằng có thể #import
ed bởi tất cả ..
@interface NSArray (indexKeyedDictionaryExtension)
- (NSDictionary *)indexKeyedDictionary
@end
Trong một tập tin .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
Ví dụ sử dụng:
NSArray *array = [NSArray arrayWithObjects:@"zero", @"one", @"two", NULL];
NSDictionary *dictionary = [array indexKeyedDictionary];
NSLog(@"dictionary: %@", dictionary);
Đầu ra:
2009-09-12 08:41:53.128 test[66757:903] dictionary: {
0 = zero;
1 = one;
2 = two;
}
Tôi đã tạo một thư viện đơn giản, được gọi là Linq to ObjectiveC, là tập hợp các phương pháp giúp loại sự cố này dễ giải quyết hơn nhiều. Trong trường hợp của bạn, bạn cần phương pháp Linq-to-ObjectiveC toDictionary, nơi trường 'int' của bạn được xác định thông qua một bộ chọn chính:
NSDictionary* dictionary = [sourceArray toDictionaryWithKeySelector:^id(id item) {
return [item intField];
}];
này trả về một từ điển, nơi các phím được đưa ra bởi các intField
trong mảng nguồn.
Hãy thử kỳ diệu này:
NSDictionary *dict = [NSDictionary dictionaryWithObjects:records
forKeys:[records valueForKey:@"intField"]];
FYI này là có thể vì điều này tích hợp tính năng:
@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;
Nó thực sự là một phép thuật! Giải pháp thanh lịch và thực sự tiện dụng! Chỉ cần một lưu ý: Có thể tốt hơn để sử dụng [records valueForKey: @ "otherField"] không chỉ là bản ghi, vì nó sẽ chứa "intField" quá. Dù sao nó mới cho tôi ngay cả sau nhiều năm mã hóa ... – BootMaker
- (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;
}
Đây là một ví dụ về việc tạo ra một NSMutableDictionary
từ danh sách nhân viên NSMutableArray
:
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. chuyển đổi NSDictionary thành NSString
- 2. Chuyển đổi NSArray thành NSMutableArray
- 3. NSDictionary đến NSArray?
- 4. NSDictionary Vs. NSArray
- 5. chuyển đổi std: vector thành NSArray
- 6. Chuyển đổi NSData sang NSDictionary
- 7. Chuyển đổi thức ăn JSON để NSDictionary
- 8. Giải mã JSON để NSArray hoặc NSDictionary
- 9. Làm đẹp NSLog của NSArray & NSDictionary
- 10. Encode NSArray hoặc NSDictionary sử dụng NSCoder
- 11. Chuyển đổi NSArray từ điển thành mảng JSON trong iOS
- 12. Làm thế nào để chuyển đổi NSArray thành NSString
- 13. Array của enums - chuyển đổi sang NSArray
- 14. Chuyển đổi NSArray thành NSSet, các cá thể lớp tùy chỉnh chuyển không nhất quán
- 15. NSDictionary to XML
- 16. Chuyển đổi NSData thành JSON
- 17. Lưu NSDictionary thành plist
- 18. Làm thế nào để chuyển đổi NSArray sang NSData?
- 19. chuyển đổi đối tượng NSDictionary thành đối tượng NSData và ngược lại
- 20. Làm thế nào để chuyển đổi CFArrayRef sang NSArray?
- 21. Làm thế nào để chuyển đổi một chuỗi JSON thành một NSArray?
- 22. Làm thế nào để chuyển đổi NSArray để NSMutableArray
- 23. Chuyển đổi UIImage thành CVImageBufferRef
- 24. Làm thế nào để lưu trữ một NSArray trong một NSDictionary?
- 25. Cách chuyển đổi NSNumber thành NSString
- 26. Có thể sử dụng NSArray, NSDictionary và NSNumber "literals" trong Xcode 4.3 không? (LLVM 4.0)
- 27. Cách tốt nhất để sắp xếp một NSArray của các đối tượng NSDictionary?
- 28. Kiểm tra xem một đối tượng là một NSArray hoặc NSDictionary
- 29. Làm thế nào để sắp xếp một danh sách NSDictionary/NSArray/Property (plist) chứa NSNull
- 30. Thêm NSDictionary vào NSDictionary
Tôi thích sử dụng các loại, vì vậy tôi sẽ sử dụng giải pháp này thay vì sử dụng một hàm đơn giản vì cách gọi trực tiếp trên đối tượng, như thể nó luôn biết phương thức đó. – Woofy