阻止NSDictionary对其项目进行排序

use*_*568 5 sorting cocoa objective-c nsdictionary

我有这个代码

NSDictionary *tempDict = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:@"authorId","authorName","authorDescription",@"image",nil] forKeys:
[NSArray arrayWithObjects:@"id",@"name",@"desc",@"image",nil]];
NSLog(@"%@",[tempDict description]);
Run Code Online (Sandbox Code Playgroud)

而输出是

desc = authorDescription;

id = authorId;

image = image;

name = authorName;
Run Code Online (Sandbox Code Playgroud)

您会看到字典按字母顺序排序,出于某种原因.这对我来说不好,因为我需要将这个字典添加到plist中,而这个plist已经有一些带有未排序键的字典.那我怎么能避免这种排序呢?

Rob*_*ier 6

排序是由于[NSDictionary description],使用NSLog.这不是一个基本特征NSDictionary.如果您通过快速枚举访问密钥,或者[dictionary allKeys]您将找不到它.

但是对于你的基本问题,如果你想要的是"未分类"(随机),那么"已排序"只是可能的随机序列之一.如果你真的想要未分类,那么排序应该无关紧要,因为它是一个子集.

如果排序很重要,那么你的意思并不是"未排序",而是指"其他一些排序顺序,例如插入顺序".如果需要NSDictionary以某种方式进行排序,则需要将其转换为NSArray.