Milar
Milar

Reputation: 3

Access to variable value from another class in Objective C

ClassA.h

...
@property (weak, nonatomic) NSString *myVariable;
- (id) setMyVariable:(NSString *)string;
- (id) getMyVariable;

ClassA.m

...
@synthezise myVariable = _myVariable;
... some inits
- (id) setMyVariable:(NSString *)string {
   _myVariable = string;
   NSLog(@"here nslog success return new value: ", _myVariable);
   return _myVariable;
}

- (id) getMyVariable {
   NSLog(@"here nslog return nil", _myVariable);
   return _myVariable;
}

ClassB.m

#import ClassA.h
...
ClassA *classA = [[ClassA alloc] init];
[classA setMyVariable:@"some"];

ClassC.m

#import ClassA.h
...
ClassA *classA = [[ClassA alloc] init];
NSLog(@"here nslog returns nil: @%", [classA getMyVariable]);

Why does [ClassC getMyVariable] return nil? Same result when I try to set value directly without setter and getter. I already read other topics on StackOverflow and Google, but have not idea why it doesn't work.

Upvotes: 0

Views: 2805

Answers (2)

Popeye
Popeye

Reputation: 12113

Your whole code is a bit of a mess really. Why are you using a weak property? Why are you using a @synthezise since this is is automatically done by xcode for you along with the getters and setters so you don't need to create them ever.

The reason why your [classA getMyVariable]; is nil in ClassC is because you create a new instance of it on the line above. By the looks of what you are trying to do is you want to set the variable for instance of a class in one class and access that variable on the same instance in a different class. So one method of doing this is to use a singleton, these are sometimes not liked but I think they work well and don't see a reason why some (not all) developers don't like them.

So lets do some cleaning up and try implementing a singleton

ClassA.h

@interface ClassA : NSObject
@property (nonatomic, strong) NSString *myVariable;
// No need for you to create any getters or setters.

// This is the method we will call to get the shared instance of the class.
+ (id)sharedInstance;
@end

ClassA.m

#import "ClassA.h"

@implementation ClassA
// No need to add a @synthezise as this is automatically done by xcode for you.

+ (id)sharedInstance
{
    static ClassA *sharedClassA = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // If there isn't already an instance created then alloc init one.
        sharedClassA = [[self alloc] init];
    });
    // Return the sharedInstance of our class.
    return sharedClassA;
} 
@end

Right so we have cleaned our ClassA code up and added a method for getting a shared instance of ClassA so now to ClassB

ClassB.m

// Other code in ClassB

// Get the shared instance
ClassA *classA = [ClassA sharedInstance];
// Set the value to the property on our instance.
[classA setMyVariable:@"Some String Value"];

//........

Now that ClassB has set the variable we can go to ClassC now and look at it.

// Other code in ClassC

// We still need to have an instance of classA but we are getting the sharedInstance
// and not creating a new one.
ClassA *classA = [ClassA sharedInstance];    
NSLog(@"My variable on my shared instance = %@", [classA myVariable]);
//........

Might help if you read this and this for help on understanding different design patterns

Upvotes: 4

markusfassbender
markusfassbender

Reputation: 346

because you don't set a value after creating an object. i should be like this:

ClassA *classA = [ClassA alloc] init];
[classA setMyVariable:@"some"];
NSLog(@"not nil anymore: @%", [classA getMyVariable]);

BTW: the @property tag provides two keywords to set getter and setter methods.

@property (weak, nonatomic, getter=myVariable, setter=setMyVariable:) NSString *myVariable;

and apple avoids the word "get" in getter-methods...

Upvotes: 1

Related Questions