zoukankan      html  css  js  c++  java
  • 得到汉字首字母在表中的顺序位置

    根据英文字母排序很简单,但是通过汉字首字母排序就比较费劲,网友用C写了一个算法,能将汉字转为英文然后比较;
    项目中用到的是addressbook中的好友名字按姓氏索引显示 
    关键词是:UILocalizedIndexedCollation
    搜索实现中文下的UITableView Index就能搜到关于这点知识

        Person *per = [[Person alloc] init];
        per.name = @"欧库作";
        
        Person *per2 = [[Person alloc] init];
        per2.name = @"比比";
        
        Person *per3 = [[Person alloc] init];
        per3.name = @"阿门";
        
        NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:per,per2,per3, nil];
        UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
        for (id obj in array) {
            int index = [collation sectionForObject:obj collationStringSelector:@selector(getname)];
            NSLog(@"index:%i",index);
        }
    index就是所在的位置,测试时需要引入pinyin.h文件和pinyin.c文件,写一个Person的类,有一个name的属性;

    #import <Foundation/Foundation.h>
    #import "Person.h"
    
    @interface Person : NSObject
    
    @property(nonatomic,retain)NSString* name;
    
    @end
    #import "Person.h"
    #import "pinyin.h"
    
    @implementation Person
    @synthesize name;
    
    - (NSString *)getname{//不是重写get方法
        if ([name canBeConvertedToEncoding:NSASCIIStringEncoding]) {
            return name;
        }else {
            return [NSString stringWithFormat:@"%c",pinyinFirstLetter([name characterAtIndex:0])];
        }
    }
  • 相关阅读:
    宏任务、微任务与Event Loop
    puppteer的使用
    docker的使用 -- windows
    vscode集成eslint
    删除git中无用的大文件
    git 使用
    利用chrome devtool 观察页面占用内存
    JS对象-不可扩展对象、密封对象、冻结对象
    数学
    素数 + 背包
  • 原文地址:https://www.cnblogs.com/neworiginou/p/2852964.html
Copyright © 2011-2022 走看看