如何在一个已排序的NSArray中搜索某一特定字符串?答案是使用CFArray自带的搜索功能:
NSMutableArray *sortedArray = [NSMutableArrayarrayWithObjects:@"Alice",@"Beth",@"Carol",@"Ellen",nil];
//Where is "Beth"?
unsignedindex = (unsigned)CFArrayBSearchValues((CFArrayRef)sortedArray,
CFRangeMake(0,CFArrayGetCount((CFArrayRef)sortedArray)),
(CFStringRef)@"Beth",
(CFComparatorFunction)CFStringCompare,
NULL);
if(index < [sortedArraycount])
{
NSLog(@"Beth was found at index %u", index);
}else{
NSLog(@"Beth was not found (index is beyond the bounds of sortedArray)");
}
//Where should we insert "Debra"?
unsignedinsertIndex = (unsigned)CFArrayBSearchValues((CFArrayRef)sortedArray,
CFRangeMake(0,CFArrayGetCount((CFArrayRef)sortedArray)),
(CFStringRef)@"Debra",
(CFComparatorFunction)CFStringCompare,
NULL);
[sortedArrayinsertObject:@"Debra"atIndex:insertIndex];
NSLog([sortedArraydescription]);
NSMutableArray *sortedArray = [NSMutableArrayarrayWithObjects:@"Alice",@"Beth",@"Carol",@"Ellen",nil];
//Where is "Beth"?
unsignedindex = (unsigned)CFArrayBSearchValues((CFArrayRef)sortedArray,
CFRangeMake(0,CFArrayGetCount((CFArrayRef)sortedArray)),
(CFStringRef)@"Beth",
(CFComparatorFunction)CFStringCompare,
NULL);
if(index < [sortedArraycount])
{
NSLog(@"Beth was found at index %u", index);
}else{
NSLog(@"Beth was not found (index is beyond the bounds of sortedArray)");
}
//Where should we insert "Debra"?
unsignedinsertIndex = (unsigned)CFArrayBSearchValues((CFArrayRef)sortedArray,
CFRangeMake(0,CFArrayGetCount((CFArrayRef)sortedArray)),
(CFStringRef)@"Debra",
(CFComparatorFunction)CFStringCompare,
NULL);
[sortedArrayinsertObject:@"Debra"atIndex:insertIndex];
NSLog([sortedArraydescription]);
分享到:
相关推荐
Object C 字符串和数组NSString NSArray操作
NSArray排序demo
写了一个简单的例子,从第一个汉字开始截取字符串。下面贴上代码: #import ViewController.h @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; ...
Swift-NSArray数组的使用
OC-NSArray数组的使用Demol
将Emoji表情用NSString类型保存在plist文件,通过 [[NSArray alloc] initWithContentsOfFile:bundlePath]获取表情(字符串类型)数组
NSPredicate应用,包括以下几个部分: 1、字符串,正则表达式的匹配; 2、字符串数组NSArray和NSMutableArray的快捷过滤方式; 3、自定义对象数组NSArray和NSMutableArray的快捷过滤方式;
官方文档,数组类的最权威文档。各种数组方法的详细说明。英文的。
NSArray *arr = [[NSArray alloc] initWithObjects:如果要初始化1000项,可以这样:当然换成swift就超级简单了
1. 数组的常用处理方式 //--------------------不可变数组 //1....NSString *s1 = @zhangsan; NSString *s2 = @lisi; NSString *s3 = @wangwu;...//(2)使用类方法创建 NSArray *array2 = [NSArray arrayWithOb
比较完美的解决了数组越界的问题, 在debug模式下会崩溃 , 在release不会崩溃 , 开发过程中 , 方便调试 , 正式环境中 减少崩溃.
NSString NSArray NSDictionary 方法名释义
每个返回NSArray或NSSet都会创建一个新对象,而不会改变原始对象。 如果您使用的是 ARC,则需要为这些文件关闭它,直到它们支持 ARC。 在 Target Settings -> Build Phases -> Compile Sources 下,找到 NSArray+...
使用数据结构的排序算法,对NSArray进行排序,包括快速、选择、插入、希尔等四种排序算法
Iphone数组一些基础操作 NSArray/NSMutableArrayIphone
方法bjl_shuffledArray - ( NSArray *)bjl_shuffledArray 返回值返回一个数组,其中所有元素都被随机排列。 bjl_shuffledArrayWithItemLimit: - ( NSArray *)bjl_shuffledArrayWithItemLimit:( NSUInteger )...
● 使用字符串、数字与对象集合 ● 使用NSArray、NSString、NSData等Foundation类 ● 使用线程、多核处理与异步处理 ● 构建使用了日期、定时器与内存管理的应用 ● 如何在其他平台上使用Objective-C 凭借深入的...
从 NSString 中提取所有或仅唯一单词的超级简单方法 提取唯一词如下所示: NSString *string = @" first_name last_name " ; NSSet *uniqueWords = [string hyp_uniqueWords ]; 会产生 uniqueWords: {( "first...
有时候我们写app的时候不想使用默认的字体,我们会用到UI给我们设计好的字体,或者别的字体,那么我们改如何设置呢. 首先先看一下我自己导入的字体和苹果给我们自带的字体都有哪些吧. 看一下样式 首先说一下.我们如何...
Objective-C的数组比C++,Java的数组强大在于,NSArray保存的对象可以是不同的对象。但只能保存对象,int ,char,double等基本数据类型不能直接保存,需要通过转换成对象才能加入数组。