objective-c - chrome快捷鍵設定 - chrome畫面旋轉




Objective-C中用於連接NSStrings的快捷鍵 (20)

一個選項:

[NSString stringWithFormat:@"%@/%@/%@", one, two, three];

另外一個選擇:

我猜你對多個追加(a + b + c + d)不滿意,在這種情況下你可以這樣做:

NSLog(@"%@", [Util append:one, @" ", two, nil]); // "one two"
NSLog(@"%@", [Util append:three, @"/", two, @"/", one, nil]); // three/two/one

使用類似的東西

+ (NSString *) append:(id) first, ...
{
    NSString * result = @"";
    id eachArg;
    va_list alist;
    if(first)
    {
        result = [result stringByAppendingString:first];
        va_start(alist, first);
        while (eachArg = va_arg(alist, id)) 
        result = [result stringByAppendingString:eachArg];
        va_end(alist);
    }
    return result;
}

在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";

宏:

// 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:@""];

使c = [a stringByAppendingString: b]更短的唯一方法是在st點附近使用自動完成。 +運算符是C的一部分,它不知道Objective-C對象。


使用這種方式:

NSString *string1, *string2, *result;

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

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

要么

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

lldb窗格中嘗試以下內容

[NSString stringWithFormat:@"%@/%@/%@", three, two, one];

哪些錯誤。

改為使用alloc和initWithFormat方法:

[[NSString alloc] initWithFormat:@"%@/%@/%@", @"three", @"two", @"one"];

Swift中

let str1 = "This"
let str2 = "is just a test"

var appendStr1 = "\(str1) \(str2)" // appendStr1 would be "This is just a test"
var appendStr2 = str1 + str2 // // appendStr2 would be "This is just a test"

此外,您可以使用+=運算符與以下相同...

var str3 = "Some String" 
str3 += str2            // str3 would be "Some String is just a test"

在處理字符串時,我經常發現使源文件ObjC ++更容易,然後我可以使用問題中顯示的第二種方法連接std :: strings。

std::string stdstr = [nsstr UTF8String];

//easier to read and more portable string manipulation goes here...

NSString* nsstr = [NSString stringWithUTF8String:stdstr.c_str()];

如何縮短stringByAppendingString並使用#define

#define and stringByAppendingString

因此你可以使用:

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

問題是,它只適用於兩個字符串,您需要包裝更多的括號以獲得更多附加信息:

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

對於在UI-Test中需要此功能的所有Objective C愛好者:

-(void) clearTextField:(XCUIElement*) textField{

    NSString* currentInput = (NSString*) textField.value;
    NSMutableString* deleteString = [NSMutableString new];

    for(int i = 0; i < currentInput.length; ++i) {
        [deleteString appendString: [NSString stringWithFormat:@"%c", 8]];
    }
    [textField typeText:deleteString];
}

我不斷回到這篇文章,並最終總結出答案,找到這個簡單的解決方案,可以根據需要使用盡可能多的變量:

[NSString stringWithFormat:@"%@/%@/%@", three, two, one];

例如:

NSString *urlForHttpGet = [NSString stringWithFormat:@"http://example.com/login/username/%@/userid/%i", userName, userId];

我的首選方法是這樣的:

NSString *firstString = @"foo";
NSString *secondString = @"bar";
NSString *thirdString = @"baz";

NSString *joinedString = [@[firstString, secondString, thirdString] join];

您可以通過將聯接方法添加到具有類別的NSArray來實現它:

#import "NSArray+Join.h"
@implementation NSArray (Join)
-(NSString *)join
{
    return [self componentsJoinedByString:@""];
}
@end

@[]這是NSArray的簡短定義,我認為這是連接字符串的最快方法。

如果您不想使用該類別,請直接使用componentsJoinedByString:方法:

NSString *joinedString = [@[firstString, secondString, thirdString] componentsJoinedByString:@""];

我試過這個代碼。 它為我工作。

NSMutableString * myString=[[NSMutableString alloc]init];
myString=[myString stringByAppendingString:@"first value"];
myString=[myString stringByAppendingString:@"second string"];

試試stringWithFormat:

NSString *myString = [NSString stringWithFormat:@"%@ %@ %@ %d", "The", "Answer", "Is", 42];

讓我們想像你不知道那裡有多少弦。

NSMutableArray *arrForStrings = [[NSMutableArray alloc] init];
for (int i=0; i<[allMyStrings count]; i++) {
    NSString *str = [allMyStrings objectAtIndex:i];
    [arrForStrings addObject:str];
}
NSString *readyString = [[arrForStrings mutableCopy] componentsJoinedByString:@", "];

這是為了更好的日誌記錄和日誌記錄 - 基於dicius優秀的多重參數方法。 我定義了一個Logger類,並且像這樣調用它:

[Logger log: @"foobar ", @" asdads ", theString, nil];

除了必須以“無”結束var參數外,幾乎是好的,但我想Objective-C中沒有辦法解決這個問題。

Logger.h

@interface Logger : NSObject {
}
+ (void) log: (id) first, ...;
@end

Logger.m

@implementation Logger

+ (void) log: (id) first, ...
{
    // TODO: make efficient; handle arguments other than strings
    // thanks to @diciu http://.com/questions/510269/how-do-i-concatenate-strings-in-objective-c
    NSString * result = @"";
    id eachArg;
    va_list alist;
    if(first)
    {
        result = [result stringByAppendingString:first];
        va_start(alist, first);
        while (eachArg = va_arg(alist, id)) 
        {
            result = [result stringByAppendingString:eachArg];
        }
        va_end(alist);
    }
    NSLog(@"%@", result);
}

@end 

為了只接收字符串,我會在NSString上定義一個類別,並為其添加一個靜態(+)連接方法,它看起來與上面的日誌方法非常相似,只不過它返回字符串。 它在NSString上,因為它是一個字符串方法,並且它是靜態的,因為您想從1-N個字符串創建一個新字符串,而不是將其稱為附加部分的任何一個字符串。


通過創建AppendString(AS)宏來創建快捷方式...

#define AS(A,B)    [(A) stringByAppendingString:(B)]
NSString *myString = @"This"; NSString *test = AS(myString,@" is just a test");

注意:

如果使用宏,當然只是用可變參數來實現,請參閱EthanB的答案。


NSNumber *lat = [NSNumber numberWithDouble:destinationMapView.camera.target.latitude];
NSNumber *lon = [NSNumber numberWithDouble:destinationMapView.camera.target.longitude];
NSString *DesconCatenated = [NSString stringWithFormat:@"%@|%@",lat,lon];

NSString *label1 = @"Process Name: ";
NSString *label2 = @"Process Id: ";
NSString *processName = [[NSProcessInfo processInfo] processName];
NSString *processID = [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]];
NSString *testConcat = [NSString stringWithFormat:@"%@ %@ %@ %@", label1, processName, label2, processID];

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

在Objective Objective CI幾年後,認為這是與Objective C一起工作的最佳方式,以實現您嘗試實現的目標。

在Xcode應用程序中開始鍵入“N”,並自動完成“NSString”。 鍵入“str”,並自動完成“stringByAppendingString”。 所以擊鍵相當有限。

一旦你獲得了敲擊“@”鍵並掛鉤編寫可讀代碼的過程不再成為問題。 這只是一個適應問題。


NSString *result=[NSString stringWithFormat:@"%@ %@", @"Hello", @"World"];




string-concatenation