I have a theoretical question on a situation where duplicate instance variables are created. It's not something that is advised to do but I
am wondering what is going on 'under the hood'.
You declare two instance variables:
{ int foo1, foo2 }
You also set properties:
@property int foo1, foo2;
You *don't* use @synthesize, so auto synthesis is used; the
properties' backing instance variables by default are set to _foo1 &
_foo2.
When building, you get a warning (that is to be expected I suppose): 'Autosynthesized property 'foo1' will use synthesized variable
'_foo1', not existing instance variable 'foo1'.
If you have methods that are accessing your instance variables, you
don't get warnings or errors if you use either the synthesized
instance variable (_foo1) or the existing intstance variable (foo1).
So, what's going on here? How is this not an out and out error rather
than just a mere warning?
Jon Rossen <jonr17@comcast.net> writes:
I have a theoretical question on a situation where duplicate instance
variables are created. It's not something that is advised to do but I
am wondering what is going on 'under the hood'.
You declare two instance variables:
{ int foo1, foo2 }
You also set properties:
@property int foo1, foo2;
You *don't* use @synthesize, so auto synthesis is used; the
properties' backing instance variables by default are set to _foo1 &
_foo2.
When building, you get a warning (that is to be expected I suppose):
'Autosynthesized property 'foo1' will use synthesized variable
'_foo1', not existing instance variable 'foo1'.
If you have methods that are accessing your instance variables, you
don't get warnings or errors if you use either the synthesized
instance variable (_foo1) or the existing intstance variable (foo1).
So, what's going on here? How is this not an out and out error rather
than just a mere warning?
It's just that you have two instance variables, foo1 and _foo1.
Imagine that you wrote in C:
struct {
int foo1;
int fool;
} s;
and the C compiler would issue a warning: "your structure s contains two fields named foo1 and fool and those names could easily be confused".
Do as you wish, but it might be a good idea to rename one of them.
The compile won't be confused by them but any human reader will probably
be.
On 7/25/2016 4:43 PM, Pascal J. Bourguignon wrote:
Jon Rossen <jonr17@comcast.net> writes:Thanks for the info. But looking at it more closely, do I *really*
I have a theoretical question on a situation where duplicate instance
variables are created. It's not something that is advised to do but I
am wondering what is going on 'under the hood'.
You declare two instance variables:
{ int foo1, foo2 }
You also set properties:
@property int foo1, foo2;
You *don't* use @synthesize, so auto synthesis is used; the
properties' backing instance variables by default are set to _foo1 &
_foo2.
When building, you get a warning (that is to be expected I suppose):
'Autosynthesized property 'foo1' will use synthesized variable
'_foo1', not existing instance variable 'foo1'.
If you have methods that are accessing your instance variables, you
don't get warnings or errors if you use either the synthesized
instance variable (_foo1) or the existing intstance variable (foo1).
So, what's going on here? How is this not an out and out error rather
than just a mere warning?
It's just that you have two instance variables, foo1 and _foo1.
Imagine that you wrote in C:
struct {
int foo1;
int fool;
} s;
and the C compiler would issue a warning: "your structure s contains two
fields named foo1 and fool and those names could easily be confused".
Do as you wish, but it might be a good idea to rename one of them.
The compile won't be confused by them but any human reader will probably
be.
have two instance variables or do I have one instance variable with
different names?
If they are indeed two distinct instance variables, does the
object/instance look at it that way and treat them as such?
Also, can I assume that only the one with the leading underscore
(_foo1) is backing the property?
Jon Rossen <jonr17@comcast.net> writes:
On 7/25/2016 4:43 PM, Pascal J. Bourguignon wrote:
Jon Rossen <jonr17@comcast.net> writes:Thanks for the info. But looking at it more closely, do I *really*
I have a theoretical question on a situation where duplicate instance
variables are created. It's not something that is advised to do but I >>>> am wondering what is going on 'under the hood'.
You declare two instance variables:
{ int foo1, foo2 }
You also set properties:
@property int foo1, foo2;
You *don't* use @synthesize, so auto synthesis is used; the
properties' backing instance variables by default are set to _foo1 &
_foo2.
When building, you get a warning (that is to be expected I suppose):
'Autosynthesized property 'foo1' will use synthesized variable
'_foo1', not existing instance variable 'foo1'.
If you have methods that are accessing your instance variables, you
don't get warnings or errors if you use either the synthesized
instance variable (_foo1) or the existing intstance variable (foo1).
So, what's going on here? How is this not an out and out error rather >>>> than just a mere warning?
It's just that you have two instance variables, foo1 and _foo1.
Imagine that you wrote in C:
struct {
int foo1;
int fool;
} s;
and the C compiler would issue a warning: "your structure s contains two >>> fields named foo1 and fool and those names could easily be confused".
Do as you wish, but it might be a good idea to rename one of them.
The compile won't be confused by them but any human reader will probably >>> be.
have two instance variables or do I have one instance variable with
different names?
You have two instance variables.
Check it by writing one and reading the other.
You could also try to check it by getting the size of the instance, but
it doesn't work with Objective-C 2.0 since that run-time optimizes allocations by rounding the object sizes:
----(instancevar.m)-------------------------------------------------------------
#import <Foundation/Foundation.h>
#import <objc/objc-runtime.h>
@interface DisplaySelfSize:NSObject
{
}
-(void)display;
@end
@interface Example1:DisplaySelfSize
{
int foo1;
}
@end
@interface Example2:DisplaySelfSize
{
int foo1;
int foo2;
}
@end
@implementation Example1
@end
@implementation Example2
@end
@implementation DisplaySelfSize
-(int)size{
return class_getInstanceSize([self class]);
}
-(void)display{
NSLog(@"size of an instance of %@ = %d\n",
[self class],[self size]);
}
@end
int main(){
[[[Example1 alloc] init] display];
[[[Example2 alloc] init] display];
return 0;
} --------------------------------------------------------------------------------
$ cc -x objective-c instancevar.m -lobjc -framework Foundation -o instancevar && ./instancevar
2016-07-26 22:54:15.926 instancevar[70818:1934406] size of an instance of Example1 = 16
2016-07-26 22:54:15.927 instancevar[70818:1934406] size of an instance of Example2 = 16
$
If they are indeed two distinct instance variables, does the
object/instance look at it that way and treat them as such?
Yes.
Also, can I assume that only the one with the leading underscore
(_foo1) is backing the property?
Yes.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 86:38:42 |
Calls: | 6,658 |
Files: | 12,203 |
Messages: | 5,333,788 |