본문 바로가기
모바일개발(Mobile Dev)/IOS개발(ObjectC)

xcode @property self variable null

by 테크한스 2015. 10. 2.
반응형

I have two Objective-C classes that communicate data with each other by passing variables through method parameters. For example I might call a method that will pass a variable:

ClassX *x = [[ClassX alloc] init];
[x passThisParameter:i];

The variable is successfully received by ClassX inside of the passThisParameter method. I can confirm this with breakpoints and log output:

- (void)passThisParameter:(id)object {
    self.classVariable = object;
    NSLog(@"Object: %@", self.classVariable); // In the log I can see that classVariable has the same value as object does.
}

However when I try to use classVariable outside of the above scope (ex. in another method, but in the same class) it always shows up as NULLWhy is my classVariable getting reset to NULL? Here is how I am retrieveing the variable later:

- (void)anotherMethodFromClassX {
    NSLog(@"Class Variable: %@", self.classVariable); // This is always NULL even though the variable is never used anywhere else (except when it's set in the method above)
}

I've also tried to setup the variable in a variety of ways in my class definition / header and in the implementation:

  • @property (retain) id classVariable
  • @property (strong) id classVariable
  • @property (assign) id classVariable
  • @property (nonatomic, strong) id classVariable

Any ideas on why this classVariable gets reset to NULL? I can't figure it out and couldn't find much on Google. Please forgive me if some of my programming terms are incorrect.

EDIT: I am using ARC, not MRC.

EDIT: Is it possible that ClassX is reallocated and reinitialized after I set classVariable that it could be reset to NULL? Say it was reloaded in the UI...

EDIT: Here's my ClassX and ClassZ with relevant code available online.

shareimprove this question
   
Without seeing more code, it's hard to answer. The first question that comes to mind is this: are you sure you're looking at globalVariable in the same object? Actually, is it really supposed to be global instead of a member of the class? –  ash Sep 3 '13 at 22:21
   
Why is globalVariable a property? Are you using ARC or MRC? –  Sebastian Sep 3 '13 at 22:23 
   
@ash My apologies, I think it's a class variable, not global. I might be using incorrect terminology here. It might help to know that the variables are declared as @property in the ClassX header file. And yes, I am using ARC. Please see my edits. –  Sam Sep 3 '13 at 22:30 
2 
There's nothing wrong with the code you posted. Can you post a short, self-contained, correct, compilable example which demonstrates your problem? –  Adam Rosenfield Sep 3 '13 at 22:30
   
What you show are instance variables, not class variables. What happens to x after you set the value? – Wain Sep 3 '13 at 22:53

All of your @property variants are instance variables, so the value you're setting is set onto the instance, not the class. So, when you do nothing to retain x And it gets destroyed by ARC the value goes with it. The next time you create a new instance of ClassX it's clean and fresh so the value is nil. The solution is to retain x and reuse it instead of allowing it to be destroyed (and do a little research into class and instance variables).

shareimprove this answer
   
This works! Creating the variables as class variables instead of instance variables and then using the samex instead of recreating each time works! –  Sam Sep 3 '13 at 23:29


반응형