hmedia1
hmedia1

Reputation: 6180

Making an Objective C Variable accessible outside method, while allowing method to still be called to update the values

Update: From the accepted answer:

As the name of this variable MyFirstVar is the same as the instance declared in the class it hides the instance variable and you have two distinct variables.

This made sense of quite a few things that were not obvious to me, so I'm just putting it at the top in case it's relevant to someone else.


Original Post:

Obviously this won't work:

@interface MyDelegate: NSObject <NSApplicationDelegate>
    {
        NSString * const MyFirstVar;
        NSString * const MySecondVar;
    }


-(void)setSomeVars;

@end

@implementation MyDelegate

    - (void)setSomeVars
    {
        NSString * const MyFirstVar = @"First Var";
        NSString * const MySecondVar = @"Second Var";
        NSLog(@"%@,%@",MyFirstVar,MySecondVar);  //  <--- This logs "First Var,Second Var" 
    }

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    [self setSomeVars];
    NSLog(@"%@,%@",MyFirstVar,MySecondVar);     //   <--- This logs "(null),(null)"

}

@end

See the two comment lines in bold. What surprised me was that the code completion is perfectly happy to use these variables and using default set of warning/error rules, there are none.

If on the other hand I use:

    {
        __block NSString *MyFirstVar;
        __block NSString *MySecondVar;
    }

And:

    {
       MyFirstVar = @"First Var";
       MySecondVar = @"Second Var";
    }

...then it works as desired, and logs "First Var,Second Var", both inside and outside the setSomeVars method.

The objective for me here, as a learning exercise (I don't really want application wide variables), is to be able to reference the most recent and up to date value of the variables; anywhere, any time, and to be able to "update" those variables by calling setSomeVars

The Question:

How do I make these variables accessible from any block of code within the entire application without calling setSomeVars to evaluate them to the local scope?

Upvotes: 0

Views: 94

Answers (1)

CRD
CRD

Reputation: 53000

When you write:

NSString * const MyFirstVar = @"First Var";

inside of setSomeVars you are creating a new local variable, one which lasts until the call to setSomeVars finishes. As the name of this variable MyFirstVar is the same as the instance declared in the class it hides the instance variable and you have two distinct variables.

Simply change the statement to:

MyFirstVar = @"First Var";

and you have an access to the existing instance variable, no two variables, no hiding.

The qualifier __block is related to blocks and should not be used here.

Some other points:

  • Declaring instance variables in the @interface is no longer recommended. Move the declarations and their enclosing braces, { & }', to immediately follow the@implementation`. This hides the variables from outside the class and therefore improves encapsulation which is good for program structure, correctness and maintenance.
  • It is convention to start variable and method names with a lowercase letter, and class names with an uppercase letters. You used uppercase for your variables, and this is why the syntax colouring is wrong.

HTH

Upvotes: 2

Related Questions