将C++的标识符转成OC的标识符

3.将C++的标识符转成OC的标识符
 C++的标识符和OC一样由数字字母下划线组成。打头的不是数字。当标识符超过一个单词,
 C++採用全字母小写。单词间用下划线连接的书写规范,如:
 bei_jing
 OC採用除第一个单词外,其余单词首字母大写的书写规范。如:
beiJing
 
 //传入C++标识符,返回OC标识符
 
 */
+ (NSString *)objcIdentifierFromCppIdentifier:(NSString *)idf
//{
//    //首先获取第0个字母
//    NSMutableString * str1 = [NSMutableString stringWithFormat:@"%c",[idf characterAtIndex:0]];
//    // 从第一个字母往后遍历。遇到‘_’符号后,取出‘_’后的字符将小写转换为大写,让i+1。否则,直接拼接
//    for (NSInteger i = 1; i < idf.length; i++) {
//        unichar ch = [idf characterAtIndex:i];
//        if (ch == '_') {
//            ch = [idf characterAtIndex:i+1];
//            [str1 appendFormat:@"%c",ch-32];
//            i++;
//        } else {
//            [str1 appendFormat:@"%c",ch];
//        }
//    }
//    return str1;
//}
//{
//    NSMutableString * str = [[NSMutableString alloc]init];
//    for (NSInteger i = 0; i < idf.length; i++) {
//        unichar ch = [idf characterAtIndex:i];
//        if (ch == '_') {
//            unichar ch = [idf characterAtIndex:i+1];
//            [str appendFormat:@"%c",ch-32];
//            i++;
//        }
//        else
//        {
//            [str appendFormat:@"%c",ch];
//        }
//    }
//    return  str;
//}
{
    NSMutableString * str = [ NSMutableString stringWithFormat:@"%c",[idf characterAtIndex:0]];
                      for(NSInteger i = 1;i < idf.length;i++)
                      {
                          unichar ch = [idf characterAtIndex:i];
                          if(ch == '_')
                          {
                              unichar ch = [idf characterAtIndex:i+1];
                              [str appendFormat:@"%c",ch-32];
                              i++;
                          }
                          else
                          {
                              [str appendFormat:@"%c",ch];
                          }
                      }
    return str;
}

原文地址:https://www.cnblogs.com/yangykaifa/p/6879934.html