连接NSString和int的最简单方法

Div*_*ero 26 string cocoa cocoa-touch objective-c nsstring

Objective-C中是否有通用功能,我可以插入到我的项目中以简化连接NSStringints?

Gen*_*ich 34

[NSString stringWithFormat:@"THIS IS A STRING WITH AN INT: %d", myInt];
Run Code Online (Sandbox Code Playgroud)

这通常是我如何做到的.


Rog*_*Rog 29

两个答案都是正确的.如果要连接多个字符串和整数,请使用NSMutableString的appendFormat.

NSMutableString* aString = [NSMutableString stringWithFormat:@"String with one int %d", myInt];    // does not need to be released. Needs to be retained if you need to keep use it after the current function.
[aString appendFormat:@"... now has another int: %d", myInt];
Run Code Online (Sandbox Code Playgroud)