在iPhone SDK中使用字符串和变量

lab*_*b12 2 string iphone variables cocoa-touch

我在这里有一个字符串:

textbox.text =@"Your name is" 
Run Code Online (Sandbox Code Playgroud)

然后我想在"你的名字是"一个显示文本的变量之后添加.

所以在Visual Basic中我学到了这样:

textbox.text =@"Your name is" & variable1.
Run Code Online (Sandbox Code Playgroud)

但现在我可以看到它在Cocoa中不起作用.

ben*_*ado 5

textbox.text = [NSString stringWithFormat:@"Your name is %@", variable1];
Run Code Online (Sandbox Code Playgroud)

阅读文档stringWithFormat:以了解字符串格式说明符.基本上,您有一个包含代码的格式字符串%@,并且以下参数代替这些转义码.

它具有与旧C风格printf()函数相同的语法.Cocoa的日志功能,NSLog()也以同样的方式工作.

如果你需要将很多字符串组合在一起,请尝试阅读NSMutableString.

你也可以这样做:

textbox.text = [@"Your name is " stringByAppendingString:variable1];
Run Code Online (Sandbox Code Playgroud)

但是如果你必须连接两个以上的东西,那就stringWithFormat:更简洁了.