在Objective-C中有没有(stringByAppendingString:)字符串连接的快捷方式,或者一般使用NSString的快捷方式?

例如,我想做:

NSString *myString = @"This";
NSString *test = [myString stringByAppendingString:@" is just a test"];

更像是:

string myString = "This";
string test = myString + " is just a test";

当前回答

这样使用stringByAppendingString:

NSString *string1, *string2, *result;

string1 = @"This is ";
string2 = @"my string.";

result = [result stringByAppendingString:string1];
result = [result stringByAppendingString:string2];

OR

result = [result stringByAppendingString:@"This is "];
result = [result stringByAppendingString:@"my string."];

其他回答

NSString *myString = @"This";
NSString *test = [myString stringByAppendingString:@" is just a test"];

在使用Objective C几年之后,我认为这是使用Objective C实现你想要实现的目标的最佳方式。

开始在你的Xcode应用中输入“N”,它自动完成为“NSString”。 key in "str",它自动补全为"stringByAppendingString"。所以按键是非常有限的。

一旦你掌握了敲击“@”键和标签的窍门,编写可读代码的过程就不再是问题了。这只是一个适应的问题。

宏:

// stringConcat(...)
//     A shortcut for concatenating strings (or objects' string representations).
//     Input: Any number of non-nil NSObjects.
//     Output: All arguments concatenated together into a single NSString.

#define stringConcat(...) \
    [@[__VA_ARGS__] componentsJoinedByString:@""]

测试用例:

- (void)testStringConcat {
    NSString *actual;

    actual = stringConcat(); //might not make sense, but it's still a valid expression.
    STAssertEqualObjects(@"", actual, @"stringConcat");

    actual = stringConcat(@"A");
    STAssertEqualObjects(@"A", actual, @"stringConcat");

    actual = stringConcat(@"A", @"B");
    STAssertEqualObjects(@"AB", actual, @"stringConcat");

    actual = stringConcat(@"A", @"B", @"C");
    STAssertEqualObjects(@"ABC", actual, @"stringConcat");

    // works on all NSObjects (not just strings):
    actual = stringConcat(@1, @" ", @2, @" ", @3);
    STAssertEqualObjects(@"1 2 3", actual, @"stringConcat");
}

备用宏:(如果你想强制一个最小数量的参数)

// stringConcat(...)
//     A shortcut for concatenating strings (or objects' string representations).
//     Input: Two or more non-nil NSObjects.
//     Output: All arguments concatenated together into a single NSString.

#define stringConcat(str1, str2, ...) \
    [@[ str1, str2, ##__VA_ARGS__] componentsJoinedByString:@""];

如何缩短stringByAppendingString和使用#define:

#define and stringByAppendingString

因此你可以使用:

NSString* myString = [@"Hello " and @"world"];

问题是它只适用于两个字符串,你需要包装额外的括号更多的追加:

NSString* myString = [[@"Hello" and: @" world"] and: @" again"];

我能想到两个答案……这两种方法都没有使用连接运算符那么令人愉快。

首先,使用一个NSMutableString,它有一个appendString方法,消除了对额外临时字符串的一些需求。

其次,使用NSArray通过componentsJoinedByString方法进行连接。

当为web服务构建请求时,我发现像下面这样做非常容易,并且在Xcode中使连接可读:

NSString* postBody = {
    @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    @"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
    @" <soap:Body>"
    @"  <WebServiceMethod xmlns=\"\">"
    @"   <parameter>test</parameter>"
    @"  </WebServiceMethod>"
    @" </soap:Body>"
    @"</soap:Envelope>"
};