我应该如何修复此代码? [英] How should I fix this code?

查看:127
本文介绍了我应该如何修复此代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对一系列人对象进行排序.该类看起来像这样.

I'm trying to sort an array of people objects. The class looks like this.

Person
-------
name
phone

我有一个装有这些对象的NSMutableArray.我想按每个人员对象的名称对该数组进行排序.

I have an NSMutableArray filled with these objects. I want to sort this array by the name of each person object.

这是我尝试的代码.

NSSortDescriptor  *desc = [[NSSortDescriptor alloc] initWithKey: @"name" ascending: YES];
[personArray sortedArrayUsingDescriptors: [NSMutableArray arrayWithObject: desc]];

它不起作用.如何更改才能正常工作?

It doesn't work. How can it be changed to work?

推荐答案

您知道sortedArrayUsingDescriptors:返回一个新的(排序的)数组,对吗?如果要对数组进行原位排序,则需要确保personArrayNSMutableArray并使用其sortUsingDescriptors:方法.

You're aware that sortedArrayUsingDescriptors: returns a new (sorted) array, correct? If you want to sort the array in-place, then you need to make sure that personArray is an NSMutableArray and use its sortUsingDescriptors: method.

修改

也许看看这两种方法会更有益:

Perhaps taking a look at the two methods will be more beneficial:

-[NSArray sortedArrayUsingDescriptors:];
-[NSMutableArray sortUsingDescriptors:];

第一个以形容词和名词开头.形容词描述名词.名词表示它是返回值.由于名词是数组",因此该方法的返回值是NSArray是有意义的.由于形容词是已排序的",因此我们可以假定返回的数组将具有某种顺序(该顺序由使用"描述符"指定).

The first one starts with an adjective and a noun. The adjective describes the noun. The noun indicates that it is the return value. Since the noun is "array", it would make sense that the return value of the method is an NSArray. Since the adjective is "sorted", we can therefore assume that the returned array will be in some sort of order (the order being specified by the "using" the "descriptors").

第二个以动词开头.由于动词是一个动作词,我们可以推断出这实际上是一种行为方法,将修改接收者本身.由于动词是"sort",因此可以假定该方法完成后接收者将处于某种顺序.

The second one starts with a verb. Since a verb is an action word, we can deduce that this is actually a behavioral method that will modify the receiver itself. Since the verb is "sort", we can therefore assume that the receiver will be in some sort of order after the method has completed.

遵循此模式的其他方法对":

Other "method pairs" that follow this pattern:

  • -[NSString stringByAppendingString:]-[NSMutableString appendString:]
  • -[NSArray filteredArrayUsingPredicate:]-[NSMutableArray filterUsingPredicate:]
  • -[NSCharacterSet invertedSet]-[NSMutableCharacterSet invert]
  • ...还有许多其他人
  • -[NSString stringByAppendingString:] and -[NSMutableString appendString:]
  • -[NSArray filteredArrayUsingPredicate:] and -[NSMutableArray filterUsingPredicate:]
  • -[NSCharacterSet invertedSet] and -[NSMutableCharacterSet invert]
  • ...and many others

由于您使用的是第一种方法(sortedArrayUsingDescriptors:),因此必须检索该方法的返回值;否则,您将一无所有地完成所有这些工作.如果要就地"对数组进行排序(即直接修改接收器,并且不给 一个新的NSArray),则personArray 必须 NSMutableArray,并且您必须使用sortUsingDescriptors:方法.

Since you're using the first method (sortedArrayUsingDescriptors:), you must retrieve the return value of the method; otherwise you're doing all this work for nothing. If you want to sort the array "in place" (ie, modify the receiver directly and not be given a new NSArray), then personArray must be an NSMutableArray, and you must use the sortUsingDescriptors: method.

这篇关于我应该如何修复此代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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