[英]Creating Strings in Objective-C
I originally learned Swift.我最初是学 Swift 的。 Now, I am trying to learn Objective-C, but I am having some difficulty.现在,我正在尝试学习 Objective-C,但我遇到了一些困难。 They seem a lot more different than I originally thought.它们看起来比我原先想象的要大得多。 Here, I am simply trying to display "Hello World".在这里,我只是想显示“Hello World”。 This is in the .m file.这是在 .m 文件中。
- (void)viewDidLoad {
[super viewDidLoad];
name = *"World";
NSString word = @"Hello";
self.label.text = [NSString stringWithFormat:@"%@, %@", word, name];
}
In my .h file I have:在我的 .h 文件中,我有:
@interface ViewController : UIViewController {
NSString *name;
} }
In Objective-C variables are declared VariableType variableName = Value;
在 Objective-C 中,变量被声明为VariableType variableName = Value;
for example例如
NSInteger number = 10;
If VariableType
is an object like NSString
you have to add an asterisk and unlike Swift a literal string must start with a leading @
.如果VariableType
是像NSString
这样的对象,您必须添加一个星号,并且与 Swift 不同的是,文字字符串必须以@
开头。
NSString *name = @"World";
NSString *word = @"Hello";
self.label.text = [NSString stringWithFormat:@"%@ %@", word, name];
You are encouraged to read Apple's language guide Working with Objective-C鼓励您阅读 Apple 的语言指南Working with Objective-C
Yeah, Objective-C has some extra symbol stuff going on when compared to Swift.是的,与 Swift 相比,Objective-C 有一些额外的符号内容。 I recommend reading or watching some tutorials on the basics.我建议阅读或观看一些有关基础知识的教程。 Also, repetition always helps when learning.此外,重复总是有助于学习。 The code below should work for you.下面的代码应该适合你。 Be sure to pay attention to the details.一定要注意细节。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
name = @"World";
NSString *word = @"Hello";
self.label.text = [NSString stringWithFormat:@"%@ %@", word, name];
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.