Difference between @interface declaration and @property declaration

前端 未结 6 2011
忘了有多久
忘了有多久 2020-12-23 10:05

I\'m new to C, new to objective C. For an iPhone subclass, Im declaring variables I want to be visible to all methods in a class into the @interface class definition eg

6条回答
  •  鱼传尺愫
    2020-12-23 10:45

    Class A      
    
          @interface myclass : UIImageView {
        int aVar;
    }
    
    If you declare like this then you can only use this variable within your class A.
    
    
    But suppose in Class B
    
        A *object=[A new];
    object.aVar---->not available
    
    
    
    For this you should **declare aVar as a property in Class A**
    
    so class A should look like 
    
    Class A      
    
      @interface myclass : UIImageView {
        int aVar;
    }
    @property int iVar;
    
    
    and 
    
    
    .m file 
    @synthesize iVar;
    
    
    
    So now you can use this iVar in another class Suppose B
    
    
     Class B
    #import "Class A.h"
    
        enter code here
    
    A *object=[A new];
    object.aVar---->available
    means
    object.aVar=10;
    

提交回复
热议问题