小编典典

在Swift中附加字符串

swift

我是iOS新手。我目前正在使用Objective-C和Swift学习iOS。

要在Objective-C中附加字符串,我使用以下代码:

 NSString *string1 = @"This is";
 NSString *string2 = @"Swift Language";
 NSString *appendString=[NSString stringWithFormat:@"%@ %@",string1,string2];
 NSLog(@"APPEND STRING:%@",appendString);

有人请指导我。


阅读 223

收藏
2020-07-07

共1个答案

小编典典

非常简单:

对于ObjC:

     NSString *string1 = @"This is";
     NSString *string2 = @"Swift Language";

ForSwift:

    let string1 = "This is"
    let string2 = "Swift Language"

对于ObjC AppendString:

     NSString *appendString=[NSString stringWithFormat:@"%@ %@",string1,string2];

对于Swift AppendString:

    var appendString1 = "\(string1) \(string2)"
    var appendString2 = string1+string2

结果:

    print("APPEND STRING 1:\(appendString1)")
    print("APPEND STRING 2:\(appendString2)")

在Swift中完成代码:

    let string1 = "This is"
    let string2 = "Swift Language"
    var appendString = "\(string1) \(string2)"
    var appendString1 = string1+string2
    print("APPEND STRING1:\(appendString1)")
    print("APPEND STRING2:\(appendString2)")
2020-07-07