iOS开发之-Sort Descriptors

本文详细介绍了如何使用NSSortDescriptor对Objective-C中的对象进行排序,包括指定排序方式、应用多个排序属性以及自定义比较方法。通过示例代码展示了在不同场景下实现排序的方法。

Creating and Using Sort Descriptors


It is important to remember that NSSortDescriptor does not sort objects. It provides the description of how to sort objects. The actual sorting is done by other classes, often NSArray or NSMutableArray.


Specifying Sorts Using NSSortDescriptor


ageDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"age"

                                           ascending:YES] autorelease];

sortDescriptors = [NSArray arrayWithObject:ageDescriptor];

sortedArray = [employeesArray sortedArrayUsingDescriptors:sortDescriptors];

Each of the sort descriptors are applied in sequence, providing a means of sorting on multiple property keys.



ageDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"age"

                                                    ascending:YES] autorelease];

hireDateDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"hireDate"

                                                     ascending:YES] autorelease];

sortDescriptors = [NSArray arrayWithObjects:ageDescriptor, hireDateDescriptor, nil];

sortedArray = [employeesArray sortedArrayUsingDescriptors:sortDescriptors];


In each of these cases, the default comparison method, compare:, is used. 



Specifying Custom Comparisons


lastNameDescriptor = [[[NSSortDescriptor alloc]

              initWithKey:@"lastName"

              ascending:YES

              selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];

 firstNameDescriptor = [[[NSSortDescriptor alloc]

              initWithKey:@"firstName"

              ascending:YES

              selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];

 sortDescriptors = [NSArray arrayWithObjects:lastNameDescriptor,

              firstNameDescriptor, nil];

 sortedArray = [peopleArray sortedArrayUsingDescriptors:sortDescriptors];


The Foundation classes that have methods that can be used with sort descriptors are listed in Table 1.




Sample Code


+ (NSArray *)knownRegions
{
	NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];
	NSMutableArray *regions = [[NSMutableArray alloc] initWithCapacity:timeZoneNames.count];
	
	for (NSString *timeZoneName in timeZoneNames){
		NSArray *nameComponents = [timeZoneName componentsSeparatedByString:@"/"];
		NSString *regionName = [nameComponents objectAtIndex:0];
		
		Region *region = nil;
		for (Region *aRegion in regions){
			if ([aRegion.name isEqualToString:regionName])
				region = aRegion;
		}
		
		if (region == nil){
			region = [[Region alloc] initWithRegionName:regionName];
			[regions addObject:region];
			[region release];
		}
		
		NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:timeZoneName];
		TimeZoneWrapper *timeZoneWrapper = [[TimeZoneWrapper alloc] initWithTimeZone:timeZone nameComponents:nameComponents];
		[region.timeZoneWrappers addObject:timeZoneWrapper];
		[timeZoneWrapper release];
	}
	
	NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
	NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
	[regions sortUsingDescriptors:sortDescriptors];
	[sortDescriptors release];
	[sortDescriptor release];
	
	for (Region *aRegion in regions){
		[aRegion sortTimeZoneWrappers];
	}
	
	return [regions autorelease];
}


基于哈希的描述符匹配(Hash-based Descriptors Matching)是一种在计算机视觉领域用于快速且高效地匹配图像局部特征的技术。 ### 基本原理 在图像特征匹配中,为了找到不同图像中对应的特征点,需要对特征描述符进行比较。传统的匹配方法可能需要计算描述符之间的距离,这在处理大规模数据时计算量较大。而基于哈希的方法通过将高维的特征描述符映射到低维的哈希码,从而大大减少了匹配时的计算量。 ### 匹配标准 在该技术中,二值特征可作为特征匹配的第二重校验。当满足两个标准时,一对局部特征可认为是匹配的:一是它们属于同一个视觉词汇;二是它们的 HE(可能是某种哈希编码方式)哈希值距离很小 [^1]。 ### 相关参数 在使用基于哈希的描述符匹配时,有一些重要的参数需要设置: - **table_number**:指定实际使用的哈希 table 的数目,通常情况赋值为 10,不过 10 到 30 都是可以接受的。 - **key_size**:哈希 key 的大小(比特数),通常赋值大于 10 小于 20。 - **multi_probe_level**:控制相邻桶的搜索方式,其区分了 multiprobe 算法与之前的 LSH 算法。通常赋值为 2,如果被赋值为 0,那么算法将退化为 non - multiprobe LSH [^2]。 ### 扩展方法 HE 的扩展方法通过指数函数估计特征和与 Hamming 距离的匹配强度,这有助于更准确地判断特征之间的匹配程度 [^1]。 ### 代码示例 以下是一个简单的使用 OpenCV 进行基于哈希的描述符匹配的伪代码示例: ```python import cv2 # 读取图像 img1 = cv2.imread('image1.jpg', cv2.IMREAD_GRAYSCALE) img2 = cv2.imread('image2.jpg', cv2.IMREAD_GRAYSCALE) # 初始化特征检测器和描述符提取器 detector = cv2.ORB_create() keypoints1, descriptors1 = detector.detectAndCompute(img1, None) keypoints2, descriptors2 = detector.detectAndCompute(img2, None) # 初始化基于哈希的匹配器 table_number = 10 key_size = 15 multi_probe_level = 2 matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) # 进行匹配 matches = matcher.match(descriptors1, descriptors2) # 绘制匹配结果 img_matches = cv2.drawMatches(img1, keypoints1, img2, keypoints2, matches[:10], None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) # 显示结果 cv2.imshow('Matches', img_matches) cv2.waitKey(0) cv2.destroyAllWindows() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值