OGeek|极客世界-中国程序员成长平台

标题: ios - 如何用字符串数组中的 %20 填充空字符串 ios [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 03:19
标题: ios - 如何用字符串数组中的 %20 填充空字符串 ios

我有一个从 JSON 对象中找到的字符串数组。目前是一个包含 4 张图像的图像数组。我不知道将来会得到多少张图像。 我的问题是图像 URL 没有显示在我的 ImageView 中,因为它包含一些空白空间。我已经检查了这个空间是否被替换为 %20,然后我可以在我的 ImageView 中愉快地使用它。

谁能建议我如何替换此图像数组中所有图像字符串 URL 中的所有空格?

请找到我的相同代码:-----

    NSArray* latest = [json objectForKey"BANNER"];

    NSMutableArray *data=[NSMutableArray new];

      if (!latest) {

        NSLog(@"Error parsing JSON: %@",error);
    }
       else{

        for(int i=0; i<latest.count ; i++)
        {
            NSDictionary *dict = [latest objectAtIndex:i];

            [data addObject:[dict objectForKey"url"]];
        }
        NSLog(@"data :%@",data);
        }

# Pragma Mark: 我找到了以下格式的图像数组

data 
    "https://xyz.in/mobile/1508139616-Food Offer Banner App (1).jpg",
    "https://xyz.in/mobile/1508163055-Serving  Banner app.jpg",
    "https://xyz.in/mobile/1508746128-Food App Banner Deal10.jpg",
    "https://xyz.in/mobile/1508746172-Food Banner Features.jpg"
)

Pragma Mark:我想像这样转换“数据”

data 
    "https://xyz.in/mobile/1508139616-Food%20Offer%20Banner%20App%20(1).jpg",
    "https://xyz.in/mobile/1508163055-Serving%20Banne%20app.jpg",
    "https://xyz.in/mobile/1508746128-Food%20App%20Banner%20Deal10.jpg",
    "https://xyz.in/mobile/1508746172-Food%20Banner%20Features.jpg"
)

我知道这个问题已经存在,但仅针对单个字符串。但我的问题是我想转换完整的字符串 URL 数组。 请帮助我已经用了 3-4 天。



Best Answer-推荐答案


让我们使用 stringByReplacingOccurrencesOfString。它可以帮助你用 %20 替换所有空格。

yourImageUrlString = [yourImageUrlString stringByReplacingOccurrencesOfString" " withString"%20"];

为您的代码

NSArray* latest = [json objectForKey"BANNER"];

NSMutableArray *data=[NSMutableArray new];

if (!latest) {

  NSLog(@"Error parsing JSON: %@",error);
}
else{

  for(int i=0; i<latest.count ; i++)
  {
    NSDictionary *dict = [latest objectAtIndex:i];
    NSString *url = [dict objectForKey"url"];
    url = [url stringByReplacingOccurrencesOfString" " withString"%20"];
    [data addObject:url];
  }
  NSLog(@"data :%@",data);
}

关于ios - 如何用字符串数组中的 %20 填充空字符串 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47345014/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4