sortedArrayUsingSelector它在干什么? [英] sortedArrayUsingSelector what is it doing?

查看:135
本文介绍了sortedArrayUsingSelector它在干什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是新的Objective-C和我试图找出这句话的做法刚好。

  [名称allKeys] sortedArrayUsingSelector:@selector(比较:)]。

我知道 allKeys 从我的字典里让所有的钥匙。我知道, sortedArrayUsingSelector 正在梳理我的数组IM创造。然后即时调用比较方法,就是在那里,我失去了什么这是在干什么?从苹果的文件就可以说:返回NSComparisonResult值,指示接收者是否比大于给定数量大于,等于,或更小。我不明白它是如何排序基于该方法的。


解决方案

 的NSArray *又如sortedKeys = [[名allKeys] sortedArrayUsingSelector:@selector(比较:)]。

以上code。使用你提供的选择返回字典键的排序的数组。选择器实际上是将是你的阵列中被排序的对象上调用的函数。在这种情况下,您的数组包含字符串,所以在实际的NSArray排序code以下会发生的事情,

  // ...
[key1的比较:键2];
// ..

如果您在不同的选择传递可以说@selector(randomFunction :)然后在分拣code以下会发生

  // ..
[键1 randomFunction:键2];
// ..

因为的NSString不给回应选择您randomFunction会得到一个错误。如果你想创建自己的类型比较函数,你需要添加一个类别,该数组包含类(在你的情况下,类别的NSString)。

有一个更好的办法来数组排序是使用块语句。

  ID mySort = ^(* NSString的key1的,的NSString * KEY2){
    返回[key1的比较:键2];
};
NSArray的*又如sortedKeys = [[名allKeys] sortedArrayUsingComparator:mySort];

这是一个更好的办法是排序任何物体的原因是很容易做到的。

  ID mySort = ^(为MyObject * OBJ1,为MyObject * OBJ2){
    返回[obj1.title比较:obj2.title];
};
NSArray的* sortedMyObjects = [myObjects s​​ortedArrayUsingComparator:mySort];

I am still new to objective-c and am trying to figure out what this statement is doing exactly.

[names allKeys] sortedArrayUsingSelector:@selector(compare:)];

I know that allKeys is getting all the keys from my dictionary. I know that sortedArrayUsingSelector is sorting my array im creating. Then im calling the compare method, that is where I am lost what is this doing? From the document on apple it says that "Returns an NSComparisonResult value that indicates whether the receiver is greater than, equal to, or less than a given number." I dont understand how it is sorting based of that method.

解决方案

NSArray * sortedKeys = [[names allKeys] sortedArrayUsingSelector:@selector(compare:)];

The above code returns a sorted array of the dictionary keys using the selector you provide. The selector is actually the function that will be called on the object that is being sorted in your array. In this case your array contains strings so in the actual NSArray sorting code the following would be happening,

//...
[key1 compare:key2];
//..

If you passed in a different selector lets say @selector(randomFunction:) then in the sorting code the following would happen

//..
[key1 randomFunction:key2];
//..

Since NSString does not respond to the selector randomFunction you would get an error. If you wanted to create your own type of comparison function you would need to add a category to the class that the array contains (in your case a category to NSString).

A better way to sort an array is to use a block statement.

id mySort = ^(NSString * key1, NSString * key2){
    return [key1 compare:key2];
};


NSArray * sortedKeys = [[names allKeys] sortedArrayUsingComparator:mySort];

The reason it's a better way is sorting any objects is very easy to do.

id mySort = ^(MyObject * obj1, MyObject * obj2){
    return [obj1.title compare:obj2.title];
};


NSArray * sortedMyObjects = [myObjects sortedArrayUsingComparator:mySort];

这篇关于sortedArrayUsingSelector它在干什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆