NSMutableDictionary 使用如下:
#import "MS_NSMutableDictionary.h"
#import "SortedIndex.h"
@implementation MS_NSMutableDictionary
@synthesize _nsMutableDictionary;
-(void)showMutableDictionary
{
_nsMutableDictionary = [[NSMutableDictionary alloc] initWithCapacity:0];
SortedIndex *one = [[SortedIndex alloc] initWithIndex:1];
SortedIndex *two = [[SortedIndex alloc] initWithIndex:2];
SortedIndex *three = [[SortedIndex alloc] initWithIndex:3];
NSLog(@"Index %i,%i,%i",one.index,two.index,three.index);
//字典增加
[_nsMutableDictionary setObject:@"C" forKey:three];
[_nsMutableDictionary setObject:@"A" forKey:one];
[_nsMutableDictionary setObject:@"B" forKey:two];
NSArray *array1 = [_nsMutableDictionary allKeys];
for(id item in array1)
{
SortedIndex *indexTemp = (SortedIndex *)item;
NSLog(@"Array 1 sorted Before:%i",indexTemp.index);
}
[array1 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
SortedIndex *index1 = (SortedIndex *)obj1;
SortedIndex *index2 = (SortedIndex *)obj2;
if(index1.index > index2.index)
{
return NSOrderedDescending;
}
else if(index1.index < index2.index)
{
return NSOrderedAscending;
}
return NSOrderedSame;
}];
for(id item in array1)
{
SortedIndex *indexTemp = ((SortedIndex *)item);
NSLog(@"Array 1 sorted After :%i",indexTemp.index);
}
for(id item in array1)
{
NSString *stringItem = [_nsMutableDictionary objectForKey:item];
NSLog(@"Values For MutableDictionary %@",stringItem);
}
}
@end