• 得到汉字首字母在表中的顺序位置


    根据英文字母排序很简单,但是通过汉字首字母排序就比较费劲,网友用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])];
        }
    }
  • 相关阅读:
    自己总结的Java归并排序代码
    SpringDataJpa
    多态
    向上转型向下转型
    python面向对象装饰器
    Apache
    git
    μWSGI
    虚拟环境
    软件仓库(持续更新中)
  • 原文地址:https://www.cnblogs.com/neworiginou/p/2852964.html
Copyright © 2020-2023  润新知