Defining intance variable and #property [duplicate] - objective-c

This question already has answers here:
What's the purpose of an ivar when a property exists?
(3 answers)
Closed 8 years ago.
What is the difference between the following ways of defining variables?
#interface MyClass: NSObject
{
NSString *string;
}
#property (nonatomic, strong) NSString *string;
vs.
#interface MyClass: NSObject
#property (nonatomic, strong) NSString *string;
I know that #property takes care of setter and getter (in conjunction with #synthesize) and I know that both ways work fine.
I also know that the latter way does not work for NSArray, NSDictionary. But it works for IBOutlets linked to IB.
Does it have anything with alloc/init? or maybe there is a concept that I am missing totally?
Thanks.

The compiler will automatically create the NSString *string; ivar for you. There is no difference under the latest SDK.
I've never ran into issues with NSDictionary not being backed by an explicit ivar.
Update:
Here is the docs on declared properties.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html

Related

#property hides _ivar when setter and getter are custom [duplicate]

This question already has answers here:
Adding a getter makes using an underscore incorrect syntax
(3 answers)
Closed 7 years ago.
#import "AppDelegate.h"
#interface AppDelegate ()
#property (strong, readwrite, nonatomic) NSNumber *currentNumber;
#end
#implementation AppDelegate
- (NSNumber *)currentNumber {
}
- (void)setCurrentNumber:(NSNumber *)currentNumber {
}
Why I can't access _currentNumber in currentNumber?
If I will remove setCurrentNumber then I can access _currentNumber in currentNumber?
The #property does not cause the ivar generation; rather, it's the implicit #synthesize. However, when you implement both the getter and setter, there is nothing to (implicitly) synthesize, so clang doesn't bother. If you add an explicit #synthesize statement, you'll get your ivar.
Like Avi mentioned, there is #synthesize keyword. If you just declare property(without implementing getter and setter for it) the corresponding ivar will be generated. Its name will be [underscore][property_name].For example declaring #property currentNumber; leads to implicit applying `#synthesize currentNumber = _currentNumber;
But if you want to assign ivar with another name to your property you can declare this ivar and synthesize the property with it:
#interface AppDelegate ()
{
NSNumber *_anotherIvar;
}
#property (strong, readwrite, nonatomic) NSNumber *currentNumber;
#end
#implementation AppDelegate
#synthesize currentNumber = _anotherIvar;
#end
#synthesize tells the compiler to create corresponding getter and setter which are backed by ivar on the right side of assignment. And if you provide your own setter - you prevent compiler from creating implicit synthesize(and generating ivar), so you need to provide ivar by yourself somehow(for example use explicit synthesize).
Here is good explanation about #synthesize.

Whats the difference between property and property with instance variable? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Properties and instance variable declaration
Whats the difference between the following two:
SomeClass.h
#interface SomeClass : NSObject {
NSString *someString;
}
#property (strong, nonatomic) NSString *someString;
#end
SomeClass.h
#interface SomeClass : NSObject
#property (strong, nonatomic) NSString *someString;
#end
I know whats the difference between the declaration inside the { } after the interface and a property is, but whats the difference between using both and using just a property?
Since the LLVM version 4.2 compiler there is no longer a difference. You no longer HAVE to declare a property variable inside the {}.
{
NSString *someString;
}
This is an ivar.
#property (strong, nonatomic) NSString *someString;
This is a property which creates setter and getter (accessors). Also one class instance with same name is created for you.
EDIT:
If you only use ivar, you cant use self.ivar name.
You have to use by _ivar, means directly to the ivar.
Inside { & } are protected. While #property are public.

Why iOS cannot get subviews property for class UIView using class_getProperty function?

I want to get the information about subviews property of class UIView:
objc_property_t property = class_getProperty([UIView class], "subviews");
But, it returns nil? I think it is so strange. Could someone explain this behavior to me?
Weird. If you use -valueForKey:, it can clearly be shown to exist. This used to be a bug with the old LLVM clang compiler in Xcode 3.2.3, where properties in categories (yes, it is declared in a category on UIView) wouldn't get recognized by the runtime, and there was even a bug report filed here about it. I know recent versions of Xcode have been having trouble with categories of late...
I just write a test code:
#interface Cat : NSObject
#property (nonatomic, strong) NSString *name;
#property (nonatomic) NSInteger age;
#property (nonatomic, readonly, copy) NSArray *subviews;
#end
It is ok. So Apple maybe be do so magic on it I guess.

Is it necessary to declare ivars in #interface to match properties? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Properties and Instance Variables in Objective-C 2.0
I'm confused by these two code segments:
First:
//.h
#interface Student : NSObject {
}
#property (nonautomic, copy) NSString *name;
#property (nonautomic, retain) NSNumber *age;
#end
//.m
#implementation Student
#synthesize name;
#synthesize age;
#end
Second:
//.h
#interface Student : NSObject {
NSString *name; // <<============ difference
NSNumber *age; // <<============ difference
}
#property (nonautomic, copy) NSString *name;
#property (nonautomic, retain) NSNumber *age;
#end
//.m
#implementation Student
#synthesize name;
#synthesize age;
#end
Both of these can work. So is it necessary to declare variables in the {}?
Starting with the modern runtime (x86_64 and ARM6...and iOS Simulator) you no longer need to declare synthesized ivars. In the first example #synthesize is adding the instance variable for you.
Agree with #Joshua. I too was confused with this in the beginning. It's basically old convention vs new convention after the runtime updates. I think Apple realized that declaring ivars was redundant when you're gonna declare #property, so why not let the #synthesize take care of it when it creates the setters and getters. One less statement for us to write, yay!
(Some of these convention changes were explained in one of the earlier WWDC videos... i think)
The Objective-C Programming Language: Property Implementation Directives
There are differences in the behavior of accessor synthesis that depend on the runtime (see also “Runtime Difference”):
For the legacy runtimes, instance variables must already be declared in the #interface block of the current class. If an instance variable of the same name as the property exists, and if its type is compatible with the property’s type, it is used—otherwise, you get a compiler error.
For the modern runtimes (see “Runtime Versions and Platforms” in Objective-C Runtime Programming Guide), instance variables are synthesized as needed. If an instance variable of the same name already exists, it is used.

Class declaration in Objective C. What is the difference? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Properties and Instance Variables in Objective-C 2.0
Objective-C Properties with or without instance variables
What is the difference between the following two pieces of code? Both are compilable and I don't know which is "correct".
#interface JTPlayer : NSObject {
NSString *userId;
NSString *name;
BOOL inBattle;
}
#property (nonatomic, copy) NSString *userId;
#property (nonatomic, copy) NSString *name;
#property (nonatomic, assign) BOOL inBattle;
#end
and
#interface JTPlayer : NSObject
#property (nonatomic, copy) NSString *userId;
#property (nonatomic, copy) NSString *name;
#property (nonatomic, assign) BOOL inBattle;
#end
One is the previous version of declaring properties. As you can see, you needed to declare variables by hand, and then declare properties applied to those variables.
The second is the newer version that manages the rest for you, declaration of variables and correspondences with properties.
Both blocks are correct, but the first one requires more keystrokes. This code (both versions) is supposed to belong to a header file, accompanied with a source file with .m extension that contains the implementation. This implementation will contain #synthesize instructions, that generate the getter and setter methods for you.
For more information, you really should read the Apple Guide to Objective-C. Also check out http://www.raywenderlich.com .
The current version of the Objective-C runtime does not require you to specify you instance variables for properties. #synthesize will add them for you automatically.
Check out this article that I put up awhile ago. It explains about instance variables and properties.
Objective-C Properties with or without instance variables