-2

What does a variable in the interface without a property means, like below?

@interface SubObject : BaseObject{
    NSString *name;
}

@end

What is the significance and usage of this variable?

Easwaramoorthy Kanagaraj
  • 3,925
  • 8
  • 36
  • 62
  • possible duplicate of [Why are Objective-C instance variables declared in an interface?](http://stackoverflow.com/questions/3040811/why-are-objective-c-instance-variables-declared-in-an-interface) – Volker Mar 20 '14 at 11:00

2 Answers2

1

This is known as a ivar or Instance Variable and has default access rights.

fantastic accepted answer on Reason to use ivars vs properties in objective c

I have also given an to The Field between Objective-c and Java, and I don't understand the @property and instance variable which may help

Community
  • 1
  • 1
Popeye
  • 11,839
  • 9
  • 58
  • 91
  • Ok, if you don't give @Property, will the ivar create getters and setters? How we can use this? – Easwaramoorthy Kanagaraj Mar 20 '14 at 11:12
  • You will be able to set `name` like `self.name = @"myName";` and get it by just doing `NSString *myName = self.name;`. It is default access so can only accessed within the class itself not outside the class. Where as using `@property` will automatically generate the `ivar` and the getters and setters for you which can then be access outside the class. – Popeye Mar 20 '14 at 11:19
  • If there is no property, the self.name is not working as you said. – Easwaramoorthy Kanagaraj Mar 20 '14 at 11:22
  • Sorry not actually sat at a computer where I can test this, try without the self, just so you know I would recommend using a `@property` and if you want it private to your class just add `@interface SubObject() @property (nonatomic, strong) NSString *name; @end` to the top of your `.m` file. – Popeye Mar 20 '14 at 11:38
  • Will the ivars create getters and setters internally without @Property? – Easwaramoorthy Kanagaraj Mar 20 '14 at 11:48
0

By declaring variable like this mean you are declaring an string which is accessible in all methods in your.m class, but it will not accessible in other class in your project. If you want this string in another class then make property

like this

@property(strong, nonatomic)NSString *string;
Gajendra Rawat
  • 3,673
  • 2
  • 19
  • 36