【问题标题】:How to print text from array with some other string? [duplicate]如何使用其他字符串从数组中打印文本? [复制]
【发布时间】:2018-02-03 20:51:09
【问题描述】:

我是 Objective-C 的新手,我找不到直接解决我的问题的方法,其中解释了如何在数组值后面或前面添加字符串。 我尝试从数组中获取一些文本并在其后面或前面添加一些其他字符串(将在 tableviewcell 中使用)。在 Android 中,它只使用 + 符号,这就是为什么我不能在这里处理它。 我尝试执行以下操作:

"some text" +[arrayName objectAtIndex:indexPath.row] + "some text"

【问题讨论】:

    标签: objective-c arrays nsstring


    【解决方案1】:

    您需要使用stringWithFormat 来连接这些值,或者您也可以使用NSMutableString

    使用这个

    NSString* finalText = [NSString stringWithFormat:@"some text %@ some text",[arrayName objectAtIndex:indexPath.row]];
    

    另一种方式

    NSMutableString* finalString = [NSMutableString stringWithString:@"test Text"];
    [finalString appendString:[arrayName objectAtIndex:indexPath.row]];
    [finalString appendString:@"test Text 2"];
    

    【讨论】:

      【解决方案2】:

      短:

      [NSString stringWithFormat:@"%@/%@/%@", "some text", [arrayName objectAtIndex:indexPath.row], "some text"];
      

      更多答案请见:https://stackoverflow.com/a/510444/9310455

      【讨论】:

        猜你喜欢
        • 2018-08-07
        • 2019-06-29
        • 1970-01-01
        • 2021-07-24
        • 1970-01-01
        • 2015-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多