DragonXDoom
DragonXDoom

Reputation: 151

iOS - Storing a reference to an object inside a variable

How would I go about making a reference to a UILabel inside a variable?

For example;

NSObject *instance;
switch (option) {
    case 0: 
    {
        NSObject *instance = self.player1;
    }
    case 1:
    {
        NSObject *instance = self.player2;
    }
}
instance.text = @"Hello"

I then want to change the text of the label that is in NSObject, but that doesn't appear to work. So how would I go about doing this?

By the way, I'm new to Objective C, if you can't tell.

Upvotes: 0

Views: 310

Answers (1)

Kurt Revis
Kurt Revis

Reputation: 27984

NSObject *instance;
switch (option) {
    case 0: 
        instance = self.player1;  // Don't redeclare the variable, just set it.
        break;   // If we don't do this, we'll continue on to the next line!

    case 1:
        instance = self.player2;
        break;

    default:
        // If option isn't 0 or 1, we should try to handle it.
        instance = nil;
        break;
}

I highly suggest going through some Objective-C and/or C tutorials -- if you are having problems with the basics of the syntax, you will find it hard to make much progress. Stack Overflow isn't the best way to learn that kind of thing.

Upvotes: 2

Related Questions