您的位置:首页 > 编程语言 > C语言/C++

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

2015-07-26 19:14 686 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: