What you probably want (if you are not using a very old OS and compiler), just use the property syntax. I.e:.
@interface MyClass : NSObject // method declarations here ... @property (copy) NSString* myVar; // ... or here. @end
This will do what you intended to do. This will implicitly synthesize the instance variable and the getter / setter pair for this variable. If you manually wanted to create an instance variable (you don't need it at all if you don't need your code to work on very old versions of MacOS), this is what the above code does under the hood to create ivar:
@interface MyClass : NSObject { NSString* _myVar; }
Note the braces that tell the compiler that this is not just a global variable somewhere between methods, but actually an instance variable belonging to this object.
If you create a property for internal use only and don’t want the clients of your class to ruin it, you can hide it a bit in everything except the oldest ObjC compilers, using a class extension that “continues” the class declaration from the header, but may be allocated separately from it (usually this is in your implementation file). The class extension looks like a category without a name:
@interface MyClass () @property (copy) NSString* myVar; @end
And you can either place a property ad, or even ivar ads (wrapped in braces again). You can even declare the same property as readonly in the class interface, and then re-declare it identical, but like readwrite in the extension so that clients only read it, but your code can change it.
Note that if you did not use ARC (that is, you turned off the default for automatic reference counting), you would need to set all your properties to nil in your dealloc method (except they are set to weak or assign , of course).
NB - all of the above @interface sections. Your actual code will go into separate sections of @implementation . Thus, you can have header files ( .h ) that you can transfer to your class clients that contain only those parts that you intend to use, and hide implementation details in the implementation file ( .m ), where you can change them without worrying that someone accidentally used them, and you break another code.
PS - Please note that NSStrings and other objects that you want to have an unchanging taste, but which also exist in a mutable taste (i.e. NSMutableString ), should always be copy properties, because this will turn NSMutableString into NSString so that no one externally could change the mutable string below you. For all other types of objects, you usually use strong (or retain , if not ARC). For your class owner (e.g. its delegate), you usually use weak (or assign , if not ARC).