Ios 按字母顺序排序的词典
我将NSDictionary作为参数传递给我的函数。我希望它的键和值在我插入时是有序的。 例如,预期产出为:Ios 按字母顺序排序的词典,ios,objective-c,iphone,nsdictionary,Ios,Objective C,Iphone,Nsdictionary,我将NSDictionary作为参数传递给我的函数。我希望它的键和值在我插入时是有序的。 例如,预期产出为: mutable dict:{ zKey1 = Value1; fKey2 = Value2; aKey3 = Value3; } 我尝试了以下方法来创建和设置关键点的值 NSMutableDictionary *mutableDict = [[NSMutableDictionary al
mutable dict:{
zKey1 = Value1;
fKey2 = Value2;
aKey3 = Value3;
}
我尝试了以下方法来创建和设置关键点的值
NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc]init];
[mutableDict setObject:@"Value1" forKey:@"zKey1"];
[mutableDict setObject:@"Value2" forKey:@"fKey2"];
[mutableDict setObject:@"Value3" forKey:@"aKey3"];
NSMutableDictionary *dic2=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"1004",@"userId",@"cucumber",@"domain",@"168d5c02f ",@"token",@"1004",@"userId1",@"cucumber",@"domain1",@"168d5c02f ",@"token1", nil];
NSDictionary * dict = [NSDictionary
dictionaryWithObjects:@[@"Ravi",@"33",@"India",@"India"]
forKeys:@[@"name",@"age",@"location",@"country"]];
NSArray *sortedKeys = [[dict allKeys] sortedArrayUsingSelector: @selector(compare:)];
NSMutableArray *sortedValues = [NSMutableArray array];
for (NSString *key in sortedKeys) {
[sortedValues addObject: [dict objectForKey: key]];
}
NSString *obj1=@"1004";
NSString *obj2=@"cucumber";
NSString *obj3=@"168d5c02f";
NSString *key1=@" userId";
NSString *key2=@"domain";
NSString *key3=@"token";
NSLog(@"dict %@",dict);
NSDictionary *dict8 =[NSDictionary
dictionaryWithObjects:@[obj1,obj2,obj3]
forKeys:@[key1,key2,key3]];
但是什么都没用,我总是得到输出
mutable dict:{
aKey3 = Value3;
fKey2 = Value2;
zKey1 = Value1;
}
dict8 {
domain = cucumber;
token = 168d5c02f;
userId = 1004;
}
dict {
age = 33;
country = India;
location = India;
name = Ravi;
}
dic= {
domain = cucumber;
domain1 = cucumber;
token = "168d5c02f ";
token1 = "168d5c02f ";
userId = 1004;
userId1 = 1004;
}
它总是根据键的字母顺序对值进行排序。许多人说NSDictionary是一个未分类的容器。但它确实得到了排序。我急需帮助。提前谢谢。默认情况下,NSDictionary未订购。它将永远没有任何秩序。要创建有序字典,需要覆盖数据结构的现有形式。你可以通过阅读来达到目的 总结本教程(因为每个人都讨厌链接,只有答案和链接随时可能消失):
NSDictionary
将其键存储在哈希表中,该哈希表按设计顺序排列。由于缺少顺序是哈希表存储的基础,因此必须执行NSMutableDictionary
的子类化(并因此重新实现存储)
在您的.h文件中
//
// OrderedDictionary.h
// OrderedDictionary
//
// Created by Matt Gallagher on 19/12/08.
// Copyright 2008 Matt Gallagher. All rights reserved.
//
// Permission is given to use this source code file without charge in any
// project, commercial or otherwise, entirely at your risk, with the condition
// that any redistribution (in part or whole) of source code must retain
// this copyright and permission notice. Attribution in compiled projects is
// appreciated but not required.
//
#import <Cocoa/Cocoa.h>
@interface OrderedDictionary : NSMutableDictionary
{
NSMutableDictionary *dictionary;
NSMutableArray *array;
}
- (void)insertObject:(id)anObject forKey:(id)aKey atIndex:(NSUInteger)anIndex;
- (id)keyAtIndex:(NSUInteger)anIndex;
- (NSEnumerator *)reverseKeyEnumerator;
@end
//
//OrderedDictionary.h
//有序词典
//
//马特·加拉赫于2008年12月19日创作。
//版权所有2008马特·加拉赫。版权所有。
//
//允许在任何情况下免费使用此源代码文件
//项目,商业或其他,完全由您承担风险,条件
//源代码的任何重新分发(部分或全部)都必须保留
//本版权及许可公告。编译项目中的属性是
//感激但不是必需的。
//
#进口
@接口OrderedDictionary:NSMutableDictionary
{
NSMutableDictionary*字典;
NSMutableArray*数组;
}
-(void)insertObject:(id)anObject forKey:(id)aKey atIndex:(nsinteger)anIndex;
-(id)keyatinex:(nsu整数)一个索引;
-(N分子*)反向分子;
@结束
在.m文件中:
//
// OrderedDictionary.m
// OrderedDictionary
//
// Created by Matt Gallagher on 19/12/08.
// Copyright 2008 Matt Gallagher. All rights reserved.
//
// Permission is given to use this source code file without charge in any
// project, commercial or otherwise, entirely at your risk, with the condition
// that any redistribution (in part or whole) of source code must retain
// this copyright and permission notice. Attribution in compiled projects is
// appreciated but not required.
//
#import "OrderedDictionary.h"
NSString *DescriptionForObject(NSObject *object, id locale, NSUInteger indent)
{
NSString *objectString;
if ([object isKindOfClass:[NSString class]])
{
objectString = (NSString *)[[object retain] autorelease];
}
else if ([object respondsToSelector:@selector(descriptionWithLocale:indent:)])
{
objectString = [(NSDictionary *)object descriptionWithLocale:locale indent:indent];
}
else if ([object respondsToSelector:@selector(descriptionWithLocale:)])
{
objectString = [(NSSet *)object descriptionWithLocale:locale];
}
else
{
objectString = [object description];
}
return objectString;
}
@implementation OrderedDictionary
- (id)init
{
return [self initWithCapacity:0];
}
- (id)initWithCapacity:(NSUInteger)capacity
{
self = [super init];
if (self != nil)
{
dictionary = [[NSMutableDictionary alloc] initWithCapacity:capacity];
array = [[NSMutableArray alloc] initWithCapacity:capacity];
}
return self;
}
- (void)dealloc
{
//This method is pre-ARC. Manual Release commands don't work now.
//[dictionary release];
//[array release];
//[super dealloc];
}
- (id)copy
{
return [self mutableCopy];
}
- (void)setObject:(id)anObject forKey:(id)aKey
{
if (![dictionary objectForKey:aKey])
{
[array addObject:aKey];
}
[dictionary setObject:anObject forKey:aKey];
}
- (void)removeObjectForKey:(id)aKey
{
[dictionary removeObjectForKey:aKey];
[array removeObject:aKey];
}
- (NSUInteger)count
{
return [dictionary count];
}
- (id)objectForKey:(id)aKey
{
return [dictionary objectForKey:aKey];
}
- (NSEnumerator *)keyEnumerator
{
return [array objectEnumerator];
}
- (NSEnumerator *)reverseKeyEnumerator
{
return [array reverseObjectEnumerator];
}
- (void)insertObject:(id)anObject forKey:(id)aKey atIndex:(NSUInteger)anIndex
{
if (![dictionary objectForKey:aKey])
{
[self removeObjectForKey:aKey];
}
[array insertObject:aKey atIndex:anIndex];
[dictionary setObject:anObject forKey:aKey];
}
- (id)keyAtIndex:(NSUInteger)anIndex
{
return [array objectAtIndex:anIndex];
}
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level
{
NSMutableString *indentString = [NSMutableString string];
NSUInteger i, count = level;
for (i = 0; i < count; i++)
{
[indentString appendFormat:@" "];
}
NSMutableString *description = [NSMutableString string];
[description appendFormat:@"%@{\n", indentString];
for (NSObject *key in self)
{
[description appendFormat:@"%@ %@ = %@;\n",
indentString,
DescriptionForObject(key, locale, level),
DescriptionForObject([self objectForKey:key], locale, level)];
}
[description appendFormat:@"%@}\n", indentString];
return description;
}
@end
//
//OrderedDictionary.m
//有序词典
//
//马特·加拉赫于2008年12月19日创作。
//版权所有2008马特·加拉赫。版权所有。
//
//允许在任何情况下免费使用此源代码文件
//项目,商业或其他,完全由您承担风险,条件
//源代码的任何重新分发(部分或全部)都必须保留
//本版权及许可公告。编译项目中的属性是
//感激但不是必需的。
//
#导入“OrderedDictionary.h”
NSString*DescriptionForObject(NSObject*object,id语言环境,NSU整数缩进)
{
NSString*objectString;
if([object iskindof类:[NSString类]])
{
objectString=(NSString*)[[object retain]autorelease];
}
else if([object respondsToSelector:@selector(descriptionWithLocale:indent:)]))
{
objectString=[(NSDictionary*)对象描述WithLocale:locale缩进:缩进];
}
else if([object respondsToSelector:@selector(descriptionWithLocale:)]))
{
objectString=[(NSSet*)对象描述WithLocale:locale];
}
其他的
{
objectString=[对象描述];
}
返回objectString;
}
@实现顺序字典
-(id)init
{
返回[self initWithCapacity:0];
}
-(id)initWithCapacity:(nsInteger)容量
{
self=[super init];
if(self!=nil)
{
dictionary=[[NSMutableDictionary alloc]initWithCapacity:capacity];
array=[[NSMutableArray alloc]initWithCapacity:capacity];
}
回归自我;
}
-(无效)解除锁定
{
//此方法是预弧。手动释放命令现在不起作用。
//[词典发布];
//[阵列释放];
//[super dealoc];
}
-(id)副本
{
返回[self-mutableCopy];
}
-(void)setObject:(id)anObject forKey:(id)aKey
{
如果(![dictionary objectForKey:aKey])
{
[数组addObject:aKey];
}
[dictionary setObject:anObject forKey:aKey];
}
-(无效)removeObjectForKey:(id)aKey
{
[dictionary removeObjectForKey:aKey];
[数组移除对象:aKey];
}
-(整数)计数
{
返回[字典计数];
}
-(id)objectForKey:(id)aKey
{
return[dictionary objectForKey:aKey];
}
-(N分子*)键枚举器
{
返回[array objectEnumerator];
}
-(N分子*)反向分子
{
返回[array reverseObjectEnumerator];
}
-(void)insertObject:(id)anObject forKey:(id)aKey atIndex:(nsinteger)anIndex
{
如果(![dictionary objectForKey:aKey])
{
[self-removeObjectForKey:aKey];
}
[array insertObject:aKey atIndex:anIndex];
[dictionary setObject:anObject forKey:aKey];
}
-(id)keyatinex:(nsinteger)一个索引
{
返回[array objectAtIndex:anIndex];
}
-(NSString*)描述WithLocale:(id)区域设置缩进:(NSInteger)级别
{
NSMutableString*indentString=[NSMutableString];
整数i,计数=电平;
对于(i=0;i
您可以下载orderedDictionary。
NSDictionary
在默认情况下不按顺序排列。它将永远没有任何秩序。要创建有序的字典,需要覆盖数据结构的现有形式。你可以通过阅读来达到目的。不,这不是一个“按键排序”的词典,即使你按顺序添加它们。如果输出是按顺序的,那就是你运气好。您可以通过已排序的密钥NSArray按所需顺序枚举密钥来打印它们,但不能直接使用NSDictionary本身