user1242574
user1242574

Reputation: 1267

Hello world app

I just dived in the world of programming on the iOS platform and I have made my first Hello World app.

I want to add extra functions to the app to learn more programming in Xcode.

In short, the apps adds the input you gave in the text field @ hallo and if you insert nothing it says 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!" 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.

This is the code I'm talking about in the 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'?

Upvotes: 1

Views: 572

Answers (3)

Jim
Jim

Reputation: 73966

if (String == "Kim") {

There are several problems with this line.

String literals in Objective-C are of the format @"foo" - you are missing the @.

You seem to want to compare nameString to @"Kim", but you are simply using 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. In order to compare strings, you should use the isEqualToString: method of one of the strings. For instance:

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

Upvotes: 1

James Webster
James Webster

Reputation: 32066

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. You probably just meant nameString.

Strings need to be compared differently to using == so use isEqualToString instead.

== 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. isEqualToString is just like this, but even more explicit.


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.

Upvotes: 3

jacekmigacz
jacekmigacz

Reputation: 789

String == "Kim"

Do you have String variable definition somewhere?

Upvotes: 0

Related Questions