DevilInDisguise
DevilInDisguise

Reputation: 360

self keyword semantics when changing an instance method to class method

-(UIView*)showMenu{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 400)];
    [view addSubview:self.menuTableView];
    return view;
}

I have this really simple function, which works fine. But then I wanted to make the function a class method (so I wouldn't have to make an instance of the class to use it..), and thus prefixed it with + instead.

Which results in error message: Member reference type 'struct obj_class' is a pointer, maybe u meant to use ->. I try that, but still error. Can somebody pls explain to me why this is happening?

Upvotes: 1

Views: 22

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

self variable inside an instance method refers to the instance of the object on which the instance method is called.

Since class methods are not associated with any particular instance, self means the Class object on which the class method is defined. Since Class class does not have a member called menuTableView, the compiler reports an error.

Upvotes: 1

Related Questions