value assigned to a property in the model class disappears in the view/controller class

后端 未结 4 475
心在旅途
心在旅途 2021-01-28 10:13

Right before my model class sends the variable stringToDisplay, NSLog shows me that it has a value. But when I try to use it in my ViewController, I just get

相关标签:
4条回答
  • 2021-01-28 10:54

    Maybe I'm missing something but your property is declared in the class extension of CalculatorBrain so nobody outside CalculatorBrain.m knows about this property.

    So if you want to expose this property to other objects, you will have to declare it in CalculatorBrain.h instead.

    0 讨论(0)
  • 2021-01-28 10:56

    It really depends how you set stringForDisplay inside the performOperation:withArray: method.

    for a blind guess, try using

    NSString *otherString = self.brain.stringForDisplay;
    

    after this line

    double result = [self.brain performOperation:operation withArray:[self.brain whatHappenedSinceLastClear]];
    
    0 讨论(0)
  • 2021-01-28 11:00

    Oh - your declaration of the property whatHappenedSinceLastClear isn't exposed to other classes that import CalculatorBrain.h because you put the property declaration in an interface extension in the .m file, which other classes will not see.

    To make it publicly accessible move the @property line for whatHappenedSinceLastClear to CalculatorBrain.h, not the .m file.

    0 讨论(0)
  • 2021-01-28 11:02

    I can guess that problem lies in the way you assign your stringForDisplay, eg.:

    if you use something like

    stringForDisplay_ = anotherString;
    

    setter for property doesn't fire, so you have to retain your variable yourself otherwise it'll live just until your method finishes;

    If so - use property setters, eg.:

    self.stringForDisplay = anotherString;
    

    that way ARC will do all the memory management.

    0 讨论(0)
提交回复
热议问题