基本的Objective-C的语法:"%@"? [英] Basic Objective-C syntax: "%@"?
问题描述
我正在通过斯坦福iPhone播客和有一些基本的问题。
I'm working through the Stanford iPhone podcasts and have some basic questions.
第一:为什么没有简单的字符串连接? (还是我只是缺少了吗?)
The first: why is there no easy string concatenation? (or am I just missing it?)
我需要与下面的NSLog的帮助,不知道它目前在做什么(在%@一部分)。你刚刚替补那些你需要的地方串联,然后逗号值在最后分开的?
I needed help with the NSLog below, and have no idea what it's currently doing (the %@ part). Do you just substitute those in wherever you need concatenation, and then comma separate the values at the end?
NSString *path = @"~";
NSString *absolutePath = [path stringByExpandingTildeInPath];
NSLog(@"My home folder is at '%@'", absolutePath);
而与任何其他编程语言我会做它是这样的:
whereas with any other programing language I'd have done it like this:
NSLog(@"My home folder is at " + absolutePath);
谢谢! (此外,任何好的导游/有人熟悉Java / C#/等风格的语法转换到Objective-C的?引用)
Thanks! (Additionally, any good guides/references for someone familiar with Java/C#/etc style syntax transitioning to Objective-C?)
推荐答案
%@
是在格式字符串中的占位符,一个NSString的实例。
%@
is a placeholder in a format string, for a NSString instance.
当你做这样的事情:
NSLog(@"My home folder is at '%@'", absolutePath);
您是说的NSLog与absolutePath字符串值替换%@
占位符。
You are saying NSLog to replace %@
placeholder with absolutePath string value.
同样的,如果你把更多的占位符,可以指定多个值,以取代那些占位符是这样的:
Likewise, if you put more placeholders, you can specify more values to replace those placeholders like this:
NSString *absolutePath = @"/home/whatever";
NSLog(@"My home #%d folder is at '%@'", 5, absolutePath);
将会打印:
我家#5是的/ home /不管
My home #5 is at /home/whatever
一个简单的方法做字符串连接:
An easy way to do string concatenation:
NSString *s1 = @"Hello, ";
NSString *s2 = @"world.";
NSString *s = [NSString stringWithFormat:@"%@%@", s1, s2];
// s will be "Hello, world."
您不能 +
符号作为字符串串连运营商,因为没有运行在Objective-C超载。
You can't have +
sign as a string concatenate operator, since there is no operation overloading in Objective-C.
希望它帮助。
这篇关于基本的Objective-C的语法:"%@"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!