Why should we declare variables for #property - iphone

I am newbie in Objective. As I read many tutorial, #property has a variable for a type, the same variable is declared in #inferface too. Is this needed?
Example
#interface MyInterface : NSObject
{
NSInteger myVaribale;
}
#property(retain) NSInteger myVariable;
Here myVariable is declared in both place.

since iOS 4, you can write
#interface MyInterface : NSObject {
}
#property(assign) NSInteger myVariable;
#implementation MyInterface
#synthesize myVariable;
#end
Meaning you can omit the NSInteger myVaribale in your interface declaration as long as you synthesize it in the .m (the synthesize will create setter, getter and instance variable)
A drawback is that you won't see the value of myVariable in Xcode's debugger.

As a note, the redeclaration of the ivar's type in the #property statement can also be useful if you want to declare the property as the immutable type of the ivar's, for instance:
#interface MyClass : NSObject
{
NSMutableArray *myArray;
}
#property (retain) NSArray *myArray;
In this instance, the ivar is actually stored as an NSMutableArray, so it can be altered during the object's lifecycle.
However this is an internal detail and if you don't want to "advertise" is as being mutable (changeable), you can make the type of the property the immutable type – in this case an NSArray.
Although this won't actually stop other code using the returned array as mutable, it is good convention and notifies other code that it shouldn't treat it in this way.

It's not an inconvenience but more a basic concept of objc: A class can have member variables. In OOP you normally define getter and setters for member variables which should be available to the outside. In objective-c you are encouraged to define getters/setters at any time (if they should be private, put their declarations into a private interface). To simplify this task, objective-c uses properties which are not more than a abbreviation. Additionally there are things like key-value-coding and -observing which are enabled by properties, but basically, they are just abbreviations for getters and setters.
Conclusion: Inside of #interface you declare members, outside methods including getters/setters (properties).

Related

Can we have retain property with no getter and setters on Obj C elements?

Ok, so I was reading some articles regarding Good Programming Practices and I came across a statement which said that making all your Elements public for your classes isn't a good idea.. aka The concept of Encapsulation.
Now in Objective C, When I create a element for my class, I do the following, consider an NSMutableArray
#property (nonatomic, retain) NSMutableArray* myArray;
WHY I DO THIS?
So as to give the Retain property to myArray and therefore, giving it a simpler Memory Management cycle. Later on, I initialize the myArray in viewDidLoad as
self.myArray = [NSMutableArray arrayWithCapacity:0];
Later in Dealloc...
self.myArray = nil;
WHAT ELSE HAPPENS
By giving this property and synthesizing myArray in .m file, what I am unknowingly doing is making Public Getters and Setters for all the elements of my class.
Also, the auto-generated UI Elements from Xib files, do have the same declarations applied.
That isn't a nice idea to keep creating public Getters and setters for each and every element of your class, right?
So, there's absolutely no kind of encapsulation applied! Please correct me if I am wrong here and help me with any solutions!
Thanks!
Simply use the principle of Class Extensions that allows you to put part of your declarations in your .m file, thus making it invisible in your header and invisible from other classes.
(This is Apple's recommended way to declare private methods and properties, by the way)
YourClass.h
#interface MyClass : MySuperclass
// public properties
// public methods
#end
YourClass.m
#interface MyClass()
// This is a class extension
// put here private properties
// and private methods too
#end
#implementation MyClass
// And your implementation of course here
#end
You can even declare a property as readonly in your public interface (in the header file) and redeclare it as readwrite in your private interface (in the class extension in the .m file) for example.
See the Apple documentation for more details.
Note that:
There is NO NEED to declare the instance variable if you declare the property: the compiler will generate it automatically for you so you don't have to bother and to declare it in the .h. In the latest version of the compiler (Modern Objective-C) there is even no need for the #synthesize directive as it now generate it automatically if not present (see doc)
If you prefer to declare instance variables anyway, you can also do this in the class extension in your .m the same way you would do in your .h. That's a way to hide instance variables from the public header too. In general I really rarely use instance variables (as declaring only the properties is sufficient) and if I really need an ivar I declare it in the class extension to make it not visibile in the public header.
You can declare the property in your class.m file, so the getter and setter methods are accessible only in that class. An example:
MyClass.h
#interface MyClass : NSObject {
NSMutableArray *_myArray;
}
#end
MyClass.m
#interface MyClass ()
#property(nonatomic, retain) NSMutableArray *myArray;
#end
#implementation MyClass
[... MyClass implementation ..]
#synthesize myArray = _myArray;
#end
So you can use "self.myArray" only in "MyClass.h" file.

feasible to not declare ivar but use in synthesize?

I'm looking at some sample code and I'm puzzled over the lack of declaration of a specific ivar. Hoping someone can help me understand this better:
typedef NSUInteger (^NumberOfItemsInSection)(ViewClass *viewClass, NSUInteger section);
// class declaration
#interface SampleScrollView : UIScrollView
#property (nonatomic, copy) NumberOfItemsInSection itemsSectionBlock;
#end
// class implementation
#implementation SampleScrollView
#synthesize itemsSectionBlock = _itemsSectionBlock;
- (void)setItemsSectionBlock:(NumberOfItemsInSection)itemsSectionBlock
{
// _itemsSectionBlock is not declared any where in the class
// How does the compiler not complain?
_itemsSectionBlock = [itemsSectionBlock copy];
[self reloadData];
}
#end
The instance variable, "_itemsSectionBlock", is not declared any where and it can just be used in the property's setter override. How does that work?
It's part of the modern runtime, and cuts down on the duplication of code - declaring iVars and then declaring properties for those iVars.
It's handled for you by the #synthesize
The modern runtime lets you do other things that you thought you couldn't do before. For example, you can now declare iVars in the .m file as part of a class extension, which reduces the amount of information you expose in your public interface.
Update
The modern LLVM 4 compiler even lets you do away with the #sytnthesize line. If you declare a property it will auto-synthesize for you and it will even create a backing store with a leading underscore.

What’s the difference between the following property declarations? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What are differences between these?
#interface { id value ) , No #property.
#interface { id value ) , #property id value; and #synthesis value;
#interface ( /* */ ) , id value in #implementation
#interface ( /* */ ) , id value out of #implementation
the others, if you think the others good example
Thanks in advance..
Instance variable with no compiler-generated accessors. Can be accessed from the class directly, can be accessed from the outside only by breaking encapsulation (foo->value).
Regular class property (= instance variable with accessors). Can be accessed both from the class and from the outside using accessors, can be accessed from the class directly.
If you mean #implementation {id value;}, this is a private instance variable. Very much like (1), only the variable is hidden from the public header. This is a recent addition AFAIK.
Not a class variable, shared by all instances.
There’s also one more option, private class property declared in the implementation file:
#interface YourClass ()
#property(retain) id foo;
#end
#implementation YourClass
#synthesize foo;
#end
This gives you nice compiler-generated accessors without the variable having to be exposed in the public header. To wrap things up, I usually use regular properties (number 2) for public stuff and properties declared in a class extension (number 5) for private stuff.
This is an instance variable (an ivar):
#interface sampleClass : superclass
{
NSUInteger sampleIvar;
}
This is a private iVar:
#interface sampleClass : superclass
{
#private
NSUInteger sampleIvar;
}
This are a properties:
#property (nonatomic,copy) NSString *sampleCopiedProperty;
#property (atomic,copy) NSString *sampleAtomicCopiedProperty;
#property (nonatomic,retain) yourClass *sampleRetainProperty;
#property (nonatomic,assign) BOOL sampleAssignProperty;
Properties are iVars that have their getters and setters generated by a compiler. properties save you time by reducing boilerplate code and let other classes access ivars of your class.
iVars have file scope, properties can be accessed from other objects.
Note: You can create properties and then specify the name of the setter, getter and even the iVar.
Note: If you use NSMutableArray, NSMutableDictionary etc. you MUST write the setter yourself and use mutableCopy otherwise the compiler generated setter will use copy on the new value which will result in a non mutable version.
in iOS 5.0 in addition to readOnly, atomic, nonatomic, copy, retain, assign etc. a new keyword __weak will be added to property definitions which helps you free memory for circular references.

Object as a data member in Objective C

From what I have experienced it seems as if objects cannot be shared data members in objective c. I know you can init a pointer and alloc the object in each method but I cannot seem to figure out how one can say define a NSMutableString as a data member and allow all of the methods to use and modify its data as in c++. Is this true or am I missing something?
To define an instance variable (member), edit your .h file:
#interface MyClass : NSObject {
// ivars go here
NSObject *member;
}
// methods go here
#end
Then, in your .m file, from any instance method (one which begins with -), you can access this variable.
- (void)doThingWithIvar {
[member doThing];
}
If you want to access the variable from outside the object itself, you'll need accessors. You can do this easily with Obj-C properties:
#interface MyClass : NSObject {
// ivars go here
NSObject *member;
}
// methods go here
#property (nonatomic, retain) NSObject *member;
#end
And in the .m:
#implementation MyClass
#synthesize member;
// ...
#end
The #synthesize line creates getter/setter methods for the ivar. Then you can use property syntax:
MyClass *thing = ...;
NSLog(#"%#", thing.member); // getting
thing.member = obj; // setting
(Note that I specified (retain) for the #property; if your member isn't an Objective-C object you won't want that. And if your property's class has a mutable counterpart, you'll want (copy) instead.)
It sounds like you want to synthesize (create getter/setter methods) a property for a member variable. I just found this cheat sheet, go down to the section called, "Properties", should give a quick overview.
Other than that Apple's documentation should give you more info.

#property #synthesize

What do #synthesize and #property do in Xcode? Please provide an explanation in really simple terms?
You asked for simple terms:
#property declares a property in your
class header
#property (nonatomic, retain) NSString *myString;
#synthesize creates your setter and
getter for your property (accessor
methods)
Without synthesize you have to write
your own setter and getter
implemention, like getMyString or
setMyString (capitalize the first
character of your property)
Sam: Just an advice: http://www.cocoadevcentral.com/d/learn_objectivec/ is a pretty solid resource to learn about basics like properties.
Good Luck!
Properties and synthesized accessors are new features in Objective-C 2.0.
When you declare a #property you declare somewhat an instance var. Then you #synthesize accessor methods (i.e. getter and setter) for that property.
There are also #dynamic accessors if you're interested.
You should really do your homework on this. Apple has nifty pdf for that.
Think of all objective-c magic as just a "smarter macro", like a "smarter #define statement"
#property if you notice is always in the h file,
#synthesize is always in the m file.
So in the background
#property (whatever) NSString *myString;
becomes a declaration of 2 methods and a private variable;
void set_myString:(NSString *) str;
(NSString*) get_myString;
declarations in the header file
to make them do something their implementation is added into m file when you type in
#synthesize myString;
which becomes something like
void set_myString:(NSString *)str
{
myString = str;
}
(NSString *) get_myString
{
return (myString);
}
But it's smarter than this
depending on if you say "retain" "strong" or "weak"
it will either just return the pointer to the myString or it will copy the myString into a new object
So all of this is done automatically by a compiler just by reading your declarations.
Which is quite useful and saves a lot of time
By default all our variables are Private so we can't acess out of the class.
if we want to use our instance variable in out of the class.
When you declare a #property you declare somewhat an instance var. Then you #synthesize accessor methods (i.e. getter and setter) for that property.
There are also #dynamic accessors if you're interested.
it simply sets the property's setter variable name in it's own class.
For example lets say I have this: #property (nonatomic, copy) NSArray* viewControllers;
Well if i want to access the setter _viewController i wouldn't set a synthesize variable.
but if i want to access the viewController variable by the name viewController instead of _viewController, I would do #synthesize viewController;.
If I wanted to use it as a completely different name I could do this #synthesize viewControllers = viewControlololer; but that's only setter. As you will notice [self viewControllers] only works and not [self viewControlololer];
So I don't understant why everyone writes sets the "setter and getter" of a property. It doesn't change the getter variable at all... unless that means [self viewController] is aware of viewControlololer (obviously).
Actually properties are synthesized, either implicitly or explicitly. Properties are implicitly synthesized. So there's no need to use synthesized unless you wanna change the name of the variable to something different than _property_name.
There are other use cases, for example if you don't want an instance variable to back your property.
Properties are explicitly synthesized by using the #synthesized directive.
(Answer extracted from the Big Nerd Ranch guide)