0
@interface DZActionView(){
    NSString* _name;
}

@interface DZActionView()
  @property(strong, nonatomic)NSString* name;
@end

What's the difference? I only know that if I use @property; it generates getter/setter methods and a _name property.

batman
  • 1,937
  • 2
  • 22
  • 41
dushandz
  • 117
  • 5

1 Answers1

2

If you mean for this:

@interface DZActionView(){
    NSString* _name;
}

to be your first way of creating a property, and

@interface DZActionView()
    @property(strong, nonatomic)NSString* name;
@end

to be your second way, then you're misunderstanding how Objective-C uses properties.

The first method is not a property, it's an instance variable only. It can't be accessed by other objects.

The second way does in fact create a property. In earlier versions of Objective-C, you would need to create a backing instance variable for the property. Which is what the _name variable you've included is for. This is mostly done for you automatically now unless you do something that requires it to be added explicitly.

If you include your own getter and setter methods (similar these):

- (NSString*)name {
    return _name;
}
- (void)setName:(NSString*)name {
    _name = name
}

Then these are explicitly referring to the _name instance variable you've mentioned, and are using it as the backing variable.

Dave Wood
  • 13,143
  • 2
  • 59
  • 67