• 排序的NSArray中搜索


    在一个已排序的NSArray中搜索某一特定字符串?答案是使用CFArray自带的搜索功能:

     

     
    NSMutableArray *sortedArray = [NSMutableArray arrayWithObjects@"Alice"@"Beth",@"Carol",@"Ellen",nil];
     
    //Where is "Beth"?
    unsigned index = (unsigned)CFArrayBSearchValues((CFArrayRef)sortedArray,
                                                                                        CFRangeMake(0,CFArrayGetCount((CFArrayRef)sortedArray)),
                                                                                        (CFStringRef)@"Beth",
                                                                                        (CFComparatorFunction)CFStringCompare,
                                                                                        NULL);
    if (index < [sortedArray count])
    {
             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"?
    unsigned insertIndex = (unsigned)CFArrayBSearchValues((CFArrayRef)sortedArray,
                                                                                                CFRangeMake(0,CFArrayGetCount((CFArrayRef)sortedArray)),
                                                                                                (CFStringRef)@"Debra",
                                                                                                (CFComparatorFunction)CFStringCompare,
                                                                                                NULL);
    [sortedArray insertObject:@"Debra" atIndex:insertIndex];
    NSLog([sortedArray description]);
  • 相关阅读:
    Python分析44130条用户观影数据,挖掘用户与电影之间的隐藏信息!
    办公利器!用Python快速将任意文件转为PDF
    教你用python搭建一个「生活常识解答」机器人
    办公利器!用Python批量识别发票并录入到Excel表格
    遇到禁止复制该怎么办?幸好我会Python...
    通知:生物信息学云论坛第十五场报告会
    centos7设置SSH安全策略–指定IP登陆
    SpringMVC—RequestMapping注解参数说明
    SpringMVC-方法四种类型返回值总结,你用过几种?
    Window下:自带python编辑器的wxpython项目发布打包exe
  • 原文地址:https://www.cnblogs.com/hopeanCom/p/2789567.html
Copyright © 2020-2023  润新知