Reputation: 31
Help me :( i dont know why?
@interface RootViewController : UITableViewController {
BOOL isSearchOn;
}
self->isSearchOn = NO; ( no error)
self.isSearchOn = NO; ( error)
Upvotes: 0
Views: 89
Reputation: 43842
There are essentially three operators here, two of which are inherited from C and the third which is from Objective-C.
In C, .
and ->
do the same thing—they retrieve a value from a struct—but -
automatically dereferences a pointer to a struct, so the following two expressions are equivalent:
(*a).b
a->b
The second operator is simply syntactic sugar. Importantly, however, .
doesn't make any sense when applied to a pointer—you have to use ->
.
Objective-C allows the use of .
with an object pointer (and only with a pointer, since all Objective-C objects are pointers) to reference a property value, a much higher level construct than the C operators provide.
Therefore, you may use .
on objects to retrieve properties defined in the interface with @property
, but you will need to use ->
to retrieve ivars (instance variables) directly, which isSearchOn
is in your example. However, it is usually better practice to expose variables using properties in Objective-C rather than refer to them directly using C's ->
operator.
Upvotes: 0
Reputation: 17535
-> is the traditional C operator to access a member of a structure referenced by a pointer. Since Objective-C objects are (usually) used as pointers and an Objective-C class is a structure, you can use -> to access its members, which (usually) correspond to instance variables.
or
When applied to pointer arrow operator is equivalent to applying dot operator to pointee (ptr->field is equivalent to (*ptr).field)
or
pSomething->someMember
is equivalent to
(*pSomething).someMember
Upvotes: 2
Reputation: 46533
Dot operator .
is used to access property
.
Arrow operator ->
is used to acces instance variable.
So you use
self->isSearchOn = NO; // which is an instance not a property
You can also use this way by skipping self->
isSearchOn = NO;
Upvotes: 2
Reputation: 5102
. is used when you have a object and want to reference a attribute or a method, while you must use -> when holding the pointer only. instead of using -> you could aswell go for *. but I guess that you should in any case read about pointers in C.
Upvotes: 0