简体   繁体   English

Hello world应用程序

[英]Hello world app

I just dived in the world of programming on the iOS platform and I have made my first Hello World app. 我只是潜入iOS平台的编程世界,我已经制作了我的第一个Hello World应用程序。

I want to add extra functions to the app to learn more programming in Xcode. 我想在应用程序中添加额外的功能,以便在Xcode中学习更多编程。

In short, the apps adds the input you gave in the text field @ hallo and if you insert nothing it says Hello World!. 简而言之,应用程序会添加您在文本字段@ hallo中提供的输入,如果您不插入任何内容,则表示Hello World!。 (It adds World!) (它增加了世界!)

Now I want the app to recognize a certain input so if I input a certain name (Kim) it gives not the standard response "hello Kim!" 现在我想要应用程序识别某个输入,所以如果我输入某个名称(Kim),它不会给出标准响应“hello Kim!” but for example hello Girl!. 但是比如你好女孩!

This is the code that adds "world!" 这是添加“世界”的代码。 after "hello" if you let the input empty, so I figured this is the place to add some extra code for the extra feature. 在“hello”之后,如果你让输入为空,那么我认为这是为额外功能添加一些额外代码的地方。

This is the code I'm talking about in the HellowWorldViewController.m 这是我在HellowWorldViewController.m讨论的代码

- (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;
}

This is what I came up with : 这就是我想出的:

- (IBAction)changeGreeting:(id)sender {

    self.userName = self.textField.text;

    NSString *nameString = self.userName;
    if ([nameString length] == 0) {
        nameString = @"World";
    }
    if (String == "Kim") {
        nameString = @"Girl";
    }

    NSString *greeting = [[NSString alloc] initWithFormat:@"Hello, %@!", nameString];
    self.label.text = greeting;
}

I get two errors "Expected expression" and "Use of undeclared identifier 'String'; did you mean 'NSString'? 我得到两个错误“预期表达式”和“使用未声明的标识符'字符串';你的意思是'NSString'?

Change: 更改:

if (String == "Kim") { nameString = @"Girl"; }

to: 至:

if ([nameString isEqualToString:@"Kim"]) { nameString = @"Girl"; }

You have two problems: 你有两个问题:

String didn't mean anything to your program, there is no variable or class called String. String对你的程序没有任何意义,没有变量或类叫做String。 You probably just meant nameString . 你可能只是想要nameString

Strings need to be compared differently to using == so use isEqualToString instead. 字符串需要与使用==进行不同的比较,因此请改用isEqualToString

== checks for identity. ==检查身份。 That is whether the two objects are the same object and point to the same address in memory. 这就是两个对象是否是同一个对象并指向内存中的相同地址。

isEqualTo by default does the same thing, but can be overridden to perform different equality comparisons. 默认情况下, isEqualTo执行相同的操作,但可以重写以执行不同的相等比较。 isEqualToString is just like this, but even more explicit. isEqualToString就是这样,但更加明确。


This might not be the only problem with your code but it's the most obvious one I found. 这可能不是您的代码唯一的问题,但它是我发现的最明显的问题。

@Jim found another problem: Strings in objective-c start with an @ symbol. @Jim发现了另一个问题:objective-c中的字符串以@符号开头。

if (String == "Kim") {

There are several problems with this line. 这条线有几个问题。

String literals in Objective-C are of the format @"foo" - you are missing the @ . Objective-C中的字符串文字格式为@"foo" - 您缺少@

You seem to want to compare nameString to @"Kim" , but you are simply using String . 您似乎想将nameString@"Kim"进行比较,但您只是使用String That's not the name of the variable you are using. 这不是您正在使用的变量的名称。

You can't compare instances of NSString with == - they are objects, and all you are doing is comparing the memory addresses of the pointers. 您无法将NSString实例与==进行比较 - 它们是对象,您所做的只是比较指针的内存地址。 In order to compare strings, you should use the isEqualToString: method of one of the strings. 为了比较字符串,您应该使用其中一个字符串的isEqualToString:方法。 For instance: 例如:

if ([nameString isEqualToString:@"Kim"]) {

String == "Kim" String ==“Kim”

Do you have String variable definition somewhere? 你在某处有String变量定义吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM