0

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.

7
  • 2
    Because in ClassC, you create a new object classA. So is not myVariable is not set for this one. Commented Sep 12, 2014 at 13:31
  • You didn't call the setMyVariable method, only the getMyVariable, so the variable was never given a value, hence it's nil. Commented Sep 12, 2014 at 13:35
  • Larme, You right. Now i see that. May you support me and say how i may get that variable without alloc new instance? Commented Sep 12, 2014 at 13:37
  • Bedford, i call getMyvariable at ClassB.m and want to get value from ClassC.m Commented Sep 12, 2014 at 13:39
  • 1
    It probably doesn't work because this is just crazy code. Commented Sep 12, 2014 at 14:12

2 Answers 2

4

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

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

2 Comments

For all you question i may answer: because in my stupid books say use that. I only start learn Objective C. Today is 4'th day since i first install xcode. So now i stupid idiot and that is normal for this time. I will be better. And you answer take me more information, keywords for learn very more. Very thanks for you. All work perfect now. I learn how it works and go next. Thanks!
With Singleton Pattern you open me new world. Second thanks! I remove a lot of garbage code with new pattern. It made me happy
1

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...

7 Comments

I dont need to set value in ClassA. But i may do this. I need to set this value in ClassB and after some time get this from ClassC.
Thanks for getter and setther methods. Did not know about them. Four of my books is outdated.
maybe your question was not very clear to me. i think you need to share the object of class A with class B and C. if you create a new object, it's another instantiation.
You right. I need share myVariable value from all of this classes. And i dont know how i may do that without initiate new instance. In more information: ClassA is controller, ClassB is ViewB controller. When user do some action in them ClassB set value of myVariable. Then ViewB change to ViewC, where i need get value of myVaribale, which already sets from ViewB.
-1 because you don't seem to actually answer the question of being able to access the variable in another class after being set in a different class. Also why have you done getter=myVariable, setter=setMyVariable: what is the point in this, and if you want to do anything with this an not lose it you will need to change from weak to strong otherwise it will be lost.
|

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.