Ios 使用导致SIGABRT的选择器进行分拣

Ios 使用导致SIGABRT的选择器进行分拣,ios,objective-c,xcode,ios7,nsarray,Ios,Objective C,Xcode,Ios7,Nsarray,我正在使用SorterDarrayUsingSelector对我拥有的数组进行排序 我叫它: NSArray *sortedArray; SEL sel = @selector(intSortWithNum1:withNum2:withContext:); sortedArray = [_myObjs sortedArrayUsingSelector:sel]; 以下是定义: - (NSInteger) intSortWithNum1:(id)num1 withNum2:(id)num2 wi

我正在使用SorterDarrayUsingSelector对我拥有的数组进行排序

我叫它:

NSArray *sortedArray;
SEL sel = @selector(intSortWithNum1:withNum2:withContext:);
sortedArray = [_myObjs sortedArrayUsingSelector:sel];
以下是定义:

- (NSInteger) intSortWithNum1:(id)num1 withNum2:(id)num2 withContext:(void *)context {
    CLLocationCoordinate2D c1 = CLLocationCoordinate2DMake([((myObj *)num1) getLat], [((myObj *)num1) getLong]);
    CLLocationCoordinate2D c2 = CLLocationCoordinate2DMake([((myObj *)num2) getLat], [((myObj *)num2) getLong]);

    NSUInteger v1 = [self distanceFromCurrentLocation:(c1)];
    NSUInteger v2 = [self distanceFromCurrentLocation:(c2)];
    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}

它没有修复任何问题。

选择器应该由被比较的对象实现,并且应该只接受一个参数,该参数是相同类型的另一个对象

例如,在中,有一个使用caseInsensitiveCompare比较字符串的示例。这是因为NSString实现了不区分大小写的比较

如果你想一想。。。sortedArrayUsingSelector如何知道将什么作为参数传递给示例中的函数

编辑: 这意味着用作“排序选择器”的函数必须是由数组中的对象定义的函数。假设数组中包含人员,则数组必须按如下方式排序:

sortedArray = [_myObjs sortedArrayUsingSelector:@selector(comparePerson:)];
comparePerson消息将被发送到数组中的对象(Persons),因此在Person类中必须具有名为comparePerson的函数:

- (NSComparisonResult)comparePerson:(Person *)person
{
    if (self.age == person.age)
        return NSOrderedSame;
}

在本例中,comparePerson将自身(自我)与论点(人)进行比较,如果两个人年龄相同,则认为他们相等。如您所见,如果您编写了正确的逻辑代码,这种比较和排序的方法可能非常强大。

您可以添加堆栈跟踪吗?thread1 SIGABRT说的不多。。(至少对我来说)如何在XCode中获取堆栈跟踪?在应用程序崩溃后的控制台输出中,“comparator消息被发送到数组中的每个对象,并将数组中的另一个对象作为其单个参数。”(from)您所说的对象必须实现选择器是什么意思?从字面上说,myObj应该是myObj?谢谢,我会试一试的!
- (NSComparisonResult)comparePerson:(Person *)person
{
    if (self.age == person.age)
        return NSOrderedSame;
}