“Variable Undeclared” error when compiling to iOS Device, but not for Simulator

后端 未结 10 1286
时光取名叫无心
时光取名叫无心 2020-12-31 10:15

I have an custom UIVIewController that is the base class for other controllers and has an instance of a custom UIView variable that is accessed by inherited the classes.

相关标签:
10条回答
  • 2020-12-31 11:10

    It is possible that when you compile for target and for simulation the data members are either protected or private. Probably for target are private by default and this seems to cause the problem. Try out playing with the @private and @protected keywords.

    However, I strongly suggest that you use properties even between your super/sub-classes. Having a complex structure is a bit hard to debug. Setting up a property will transfer the access to the data through the getter/setter methods (a breakpoint on @synthesize also works) and you will be able to see in the call stack who is accessing what.

    Especially, the syntax @synthesize propertyName = prefixDataNameSufix; allows you to easily adjust your class interface style without having to modify your coding habits.

    0 讨论(0)
  • 2020-12-31 11:13

    I had the exact same problem and it turns out that I did not remove an unused iVar/property in the SUBCLASS. Let's call it session. I removed _session from the iVar but I forgot to remove it from the properties, then in the .m file I had this synthesize session = _session. Once I removed them all, I can compile for iOS device without problems.

    If you think your superclass is fine, look into your subclass, check your iVars and properties and synthesize section in the .m file

    0 讨论(0)
  • 2020-12-31 11:14

    I faced similar problem as you. In my case the reason was (strangely!) wrong synthesization of properties in subclass.

    Example:

    In .h file of subclass you have following declaration

    BOOL _flag;
    ...
    @property (nonatomic, assign) BOOL flag;
    

    while in you synthesize the property in the wrong way:

    @synthesize flag;
    

    instead of

    @synthesize flag = _flag;
    

    Strangely, the compiler does not complain about the wrong synthesization (the properties even work fine!), but raises an error, when I try to access protected fields declared in base class.


    Detailed explanation

    Here is what my code look like

    I have base class (excerpt):

    @interface BaseEditionModalController : NSObject 
    {
        DataContext *_dataContext;
    }
    

    And I have subclass of it (excerpt):

    @interface LocationModalController : BaseEditionModalController
    {
        MCLocation *_readLocation;
    
        LocationCommModel *_oldLocationCommModel;   
    }
    
    //This is MCLocation for reading only - from the main application context
    @property (nonatomic, retain) MCLocation *readLocation;
    
    @property (nonatomic, retain) LocationCommModel *oldLocationCommModel;
    
    @end
    

    And in the LocationModalController.m I have following wrong declarations:

    @implementation LocationModalController
    
    @synthesize readLocation;
    @synthesize oldLocationCommModel;
    

    Trying to access _dataContext in LocationModalController produced the error that _dataContext is undeclared.

    Changing the synthesization of properties to:

    @implementation LocationModalController
    
    @synthesize readLocation = _readLocation;
    @synthesize oldLocationCommModel = _oldLocationCommModel;
    

    MAGICALLY SOLVES THE PROBLEM!

    Regards

    0 讨论(0)
  • 2020-12-31 11:16

    If anyone is having this issue after upgrading their tools and devices to iOS10, I had it and found that declaring them as weak, nonatomic in the .h file was the issue. I had never encountered this when doing so before but after removing (weak, nonatomic) from the property declaration, everything worked fine again.

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