18

I have a question, here's the code:

@interface MyFoo : NSObject {
    NSString *nameStr;
}
@end
@implementation MyFoo
- (id)init {
    self = [super init];
    if (self) {
        self->nameStr = [@"some value of the string that is set right into the private ivar" copy];
    }
    return self;
}
@end

The question is: ignoring all the C++ rules, ignoring memory dump vulnerability, why exactly I shouldn't use such arrow operator syntax? Is there somewhere in Apple documentation a rule which says that it's incorrect because in future class may be represented differently than a pointer to a struct in runtime etc. ?

Thanks in advance!

James Webster
  • 31,873
  • 11
  • 70
  • 114
art-divin
  • 1,635
  • 1
  • 20
  • 28

3 Answers3

18

The use of self->someIvar is identical to someIvar. It's not wrong but it's not needed either.

The only time I use the arrow notation is in an implementation of copyWithZone: so I can copy each of the ivars that don't have properties.

SomeClass *someCopy = ...
someCopy->ivar1 = ivar1; // = self->ivar1
someCopy->ivar2 = ivar2; // = self->ivar2

Where are you seeing anything that says you shouldn't use such arrow operator syntax?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 4
    It is a matter of style when using `self`. It's required when accessing ivars of an object instance other than `self`. – rmaddy Dec 17 '12 at 18:37
  • Yes it is! Many people like to underscore ivar variables as well, to distinction ivars. – Guilherme Torres Castro Dec 17 '12 at 18:47
  • 1
    Take it from someone who deplored them but has forced himself to adapt: the underscores are now advocated style by Apple; ignore that at your peril. Re: the arrow dereference, historically Objective-C objects explicitly had structs directly backing them (i.e. every instance variable had a named struct member, in the order declared in the @interface) and this syntax pretty much inherits from that. – Tommy Dec 17 '12 at 18:52
  • 7
    You shouldn't be using -> to dip into the ivars of another object outside of exceptionally rare cases (implementation of `copyWithZone:` being about the only one). – bbum Dec 17 '12 at 22:37
5

Using arrow notation on just the ivar name to access a property will not guarantee they will be retain, assign or etc ... Because you are directing accessing an ivar and not calling and setter ou getter method used in properties.

Example:

@interface MyFoo : NSObject {
}
@property(nonatomic,retain)  NSString *nameStr;
@end
@implementation MyFoo
- (id)initWithString:(NSString *)name {
    self = [super init];
    if (self) {
        self->nameStr = name; // will not be retained
    }
    return self;
}
@end

For ivar variables as already be answer there`s nothing wrong.

Guilherme Torres Castro
  • 15,135
  • 7
  • 59
  • 96
  • The question has nothing to do with properties. – rmaddy Dec 17 '12 at 18:35
  • Of course there is, he is asking if there's any problem using arrow notation, if you use it in properties there's a problem. – Guilherme Torres Castro Dec 17 '12 at 18:37
  • 2
    He didn't ask about using it with properties. He asked about using it with ivars. You can't use the arrow for properties, you use the dot for properties. Reread the question then my answer. Properties are not involved. He is not in any way asking about memory management either. – rmaddy Dec 17 '12 at 18:41
  • 1
    @rmaddy i get it , i'm justing pointing something that i think is important – Guilherme Torres Castro Dec 17 '12 at 18:44
  • @rm it has to do with properties too -- since "auto synthesis" of properties is the default behaviour of Obj-C. – Motti Shneor Jan 20 '19 at 11:06
3

Using the arrow notation isn't incorrect, and there is a difference between using the arrow and the dot notation.If you use the arrow operator you access to the instance variable, if you use the dot operator you access to the property.
It doesn't work like C structs where you use the arrow notation to access to a member of a struct pointed, and dot notation to access the struct members. So I would make a significative example:

@property (nonatomic, strong) NSString *text;

In .m file:

- (void)setText:(NSString *)string {
    NSLog(@"Accessing property");
    self->text = string; // let's suppose that text is not synthesized
}

If you use the dot notation , then you access to the property and it would print "Accessing property".But this hasn't to do with C structs syntax.

Alex Terente
  • 12,006
  • 5
  • 51
  • 71
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • 1
    "It doesn't work like C structs where you use the arrow notation to access to a member of a struct pointed." Actually, that's exactly how it works. "...there is a difference between using the arrow and the dot notation." Is that really relevant to the original question? – jlehr Dec 17 '12 at 18:51
  • I believe it's my fault here, I get always misunderstood in my sentences, i should specify everything.Edited. – Ramy Al Zuhouri Dec 17 '12 at 19:11