0

I have a file Variables.m for storing properties that I can use in all classes within my app. Now I would like to set a value for one of the properties (say username) in class A and I would like it to be available to all the other classes (B,C,D,E..) in my app like a constant. ie. once they initialize a Variables object (say var) in class B, if they issue var.username, they should get the username that I set in class A.

In effect instead of hardcoding a username value, I want to set it programmatically and have all the classes see the value that I just set. Can I achieve this without passing the Variables object around whenever I navigate to a class?

3 Answers 3

2

Alternatively, you can use NSUserDefaults

Set variable in class A:

NSString *usernameToSave = @"John Doe";
[[NSUserDefaults standardUserDefaults] setObject:usernameToSave forKey:@"userName"];

Read variable in any class:

NSString *userName = [[NSUserDefaults standardUserDefaults] objectforKey:@"userName"];

Apart from being simple, this has the advantage (or not) that it's persistent across app restarts.

It has the disadvantage that it's not very secure; for passwords and the like you should use Keychain Services as described here.

Sign up to request clarification or add additional context in comments.

Comments

1

Create a singelton class that has the appropriate getters and setters for the items you want it to store.

You can then just reference that single instance from any other class.

For example in your Variables class create a class method like so:

    +(Variables*)sharedVariables{
        static Variables *myVariables = nil;
        if (myVariables != nil){
             return myVariables;
        }
       myVariables = [[Variables alloc]init];
       return myVariables;
    }

If you want a better implementation then google objective C singleton class implementation.

:)

to use this from your other classed you would just type:

[Variables sharedVariables].whatever

3 Comments

Thanks but I have a couple of question. In your singleton class if I wanted to initialize it with values, say username and password, how do I do this. Also can I have 2 versions of sharedVariables, one that takes in input arguments and allocates the space for the static myVariables object and the other that just returns the myVariables object? If yes where do I put the statement static Variables *myVariables = nil ? Thanks
set those in the init method. Or create a custome one i.e. -(id)initWithMyData:(Type*)data1 andData2:(Type*)data2; if they are going to change otherwise just type them in the standard initialiser for that class.
well this a singleton. So there should only be one. If you want a singe instance that you can reference from other classes use the class method above to create a singleton. if you want another instance if this class then just use the standard init method to create those.
0

You can use Singleton pattern. Like this.

@interface MySingleton : NSObject 
{
    NSString *username;
}
@property (nonatomic, retain) NSString *username;
+ (MySingleton *) sharedInstance;

@end


@implementation MySingleton
@synthesyse username;
static MySingleton *sMySingleton = nil;

+ (MySingleton *) sharedInstance
{
    @synchronized(self)
    {
        if (sMySingleton == nil)
        {
            sMySingleton = [NSAllocateObject([self class], 0, NULL) init];
        }
    }    
    return sMySingleton;
}

@end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.