我只是在学习做“Hello, World”应用程序。但我有一个问题。我想更改代码,使结果显示为“World,Hello”,但无法弄清楚我做错了什么。
这是原始代码:
- (IBAction)changeGreeting:(id)sender {
self.userName = self.textField.text;
NSString *nameString = self.userName;
if ([nameString length] == 0) {
nameString = @"World";
}
NSString *greeting = [[NSString alloc] initWithFormat:@"Hello, %@!", nameString];
self.label.text = greeting;
}
我认为如果我可以将其更改为:
- (IBAction)changeGreeting:(id)sender {
self.userName = self.textField.text;
NSString *nameString = self.userName;
if ([nameString length] == 0) {
nameString = @"World";
}
NSString *greeting = [[NSString alloc] initWithFormat:nameString , @"Hello, %@!"];
self.label.text = greeting;
}
然而,这仍然没有奏效。我会怎么做才能让它发挥作用?