iOS可变参数va_list

栏目: IOS · 发布时间: 6年前

内容简介:也曾考虑过在封装的代码中通过
+ (NSString *)getGoodsRuleWithCarLength:(NSString *)carLength andCarType:(NSString *)carType andSpecialRequire:(NSString *)specialRequire andEntrance:(TYTGoodsRuleEntrance)entrance{
    NSString *goodsRule = nil;
    if(carLength.length > 0){
        if(carType.length > 0){
            if(specialRequire.length > 0){
                goodsRule = [NSString stringWithFormat:@"%@/%@/%@",carLength,carType,specialRequire];
            }else{
                goodsRule = [NSString stringWithFormat:@"%@/%@",carLength,carType];
            }
        }else{
            if(specialRequire.length > 0){
                goodsRule = [NSString stringWithFormat:@"%@/%@",carLength,specialRequire];
            }else{
                goodsRule = [NSString stringWithFormat:@"%@",carLength];
            }
        }
    }else{
        if(carType.length > 0){
            if(specialRequire.length > 0){
                goodsRule = [NSString stringWithFormat:@"%@/%@",carType,specialRequire];
            }else{
                goodsRule = [NSString stringWithFormat:@"%@",carType];
            }
        }else{
            if(specialRequire.length > 0){
                goodsRule = [NSString stringWithFormat:@"%@",specialRequire];
            }else{
                goodsRule = @"";
            }
        }
    }
    return goodsRule;
}
复制代码

二、可变参数的方式

//创建NSString的分类
/**
 *  用斜线拼接参数
 *  count为参数个数,其它参数必须是NSString类型(可为nil)
 *  eg.需要拼接三个字符串 长、宽、高为 "长/宽/高"  方法调用方式为[NSString splicWithSlantParamsCount:3, @"长", @"宽", @"高"];
 */
+ (NSString *)stringSplicWithSlantParamsCount:(int)count,...{
    if(count < 1){
        return @"";
    }
    int index = 0;
    va_list args;
    va_start(args, count);
    //拼接后的字符串
    NSString *paramSum = @"";
    NSString *otherParam;
    //循环到结尾就停止循环
    while(index<count){
        index ++;
        otherParam = va_arg(args, NSString *);
        if(paramSum.length > 0){
            if(otherParam.length > 0){
                paramSum = [NSString stringWithFormat:@"%@/%@",paramSum,otherParam];
            }
        }else{
            if(otherParam.length > 0){
                paramSum = otherParam;
            }
        }
    }
    va_end(args);
    return paramSum;
}

复制代码
+ (NSString *)getGoodsRuleWithCarLength:(NSString *)carLength andCarType:(NSString *)carType andSpecialRequire:(NSString *)specialRequire andEntrance:(TYTGoodsRuleEntrance)entrance{
    //用"/"拼接字符串
    NSString *goodsRule = [NSString stringSplicWithSlantParamsCount:3, carLength, carType, specialRequire];
    return goodsRule;
}
复制代码

很明显,第二中方法扩展性更好,逼格更好

也曾考虑过在封装的代码中通过 vsnprintf 计算可变参数的长度,但是参数有可能为空,就会导致崩溃,所以最后决定直接传进来参数的个数


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

谷歌和亚马逊如何做产品

谷歌和亚马逊如何做产品

梅 (Chris Vander Mey) / 刘亦舟 / 人民邮电出版社 / 2014-6-1 / CNY 49.00

软件在交付之前,面临产品、方案、项目和工程管理等诸多挑战,如何做到游刃有余并打造出极致产品?本书作者曾任谷歌和亚马逊高级产品经理、现任Facebook产品经理,他将自己在达特茅斯学院钻研的理论知识和在领先的互联网公司十年的工作经验尽数总结在此,从定义产品开始,一步步指导你完成管理项目、迭代、发布、市场推广等交付流程,让你身临其境地体验到极致产品如何取得成功。 本书主要内容: 如何清晰定......一起来看看 《谷歌和亚马逊如何做产品》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具