您的位置:首页 > 移动开发

iOS字符串拼接 stringByAppendingString:与 stringByAppendingPathComponent:

2017-11-24 14:38 302 查看
         之前的旧项目,把功能模块移植到另一个应用中,结果在图片加载的时候,始终加载不到图片,然后一路追查,查到了拼接的图片地址上面,打印图片地址发下http://协议头少了个“/” , 项目中都是这样使用
stringByAppendingPathComponent:

拼接的网络地址,POST网络请求也没问题。唯独使用SDWebimage 进行图片加载时失败 ,因为协议头少了个 / 。(猜测stringByAppendingPathComponent:方法是会进行以单 / 替换字符串中连在一起的多个 // )


NSString *testStr = @"https://123.21.54.1/health/uiiddd/3323";
NSString *str = [testStr stringByAppendingString:@"/justtry"];
NSString *comStr = [testStr stringByAppendingPathComponent:@"justtry"];
NSLog(@"===%@",str);
NSLog(@"===%@",comStr);

    NSString *comStr2 = [testStr stringByAppendingPathComponent:@"///justtry"];
    NSLog(@"===%@",comStr2);

打印结果:

2017-11-24 14:19:48.607 Switch[59129:2841826] ===https://123.21.54.1/health/uiiddd/3323/justtry

2017-11-24 14:19:48.608 Switch[59129:2841826] ===https:/123.21.54.1/health/uiiddd/3323/justtry

2017-11-24 14:33:38.935 Switch[59409:2855959] ===https:/123.21.54.1/health/uiiddd/3323/justtry
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  NSString