Search code examples
objective-ciosivar

I want to set ivar using -> operator


I want to set ivar using -> operator. I got an error: Interface type cannot be statically allocated. I don't want dot operator.

 @interface Dream : NSObject

 @property (nonatomic, assign) BOOL isBeer;

 @end

Class ViewController

 - (void)viewDidLoad
{
 [super viewDidLoad];

  Dream dr1;
  (&dr1)->isBear = YES;
 }

Solution

  • You need to make the instance variable public:

    @interface Dream : NSObject
    {
    @public
        BOOL isBeer;
    }
    @end
    

    Class ViewController

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        Dream *dr1 = [[Dream alloc] init];
        dr1->isBeer = YES;
    }