Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

instance variables & properties

10 views
Skip to first unread message

Jon Rossen

unread,
Jul 25, 2016, 4:06:57 PM7/25/16
to
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?

thanks,

jonr


Pascal J. Bourguignon

unread,
Jul 25, 2016, 7:43:55 PM7/25/16
to
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.


--
__Pascal Bourguignon__ http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk

Jon Rossen

unread,
Jul 25, 2016, 10:06:33 PM7/25/16
to
Thanks for the info. But looking at it more closely, do I *really* 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?

-jonR

Pascal J. Bourguignon

unread,
Jul 26, 2016, 4:56:45 PM7/26/16
to
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.

Jon Rossen

unread,
Jul 26, 2016, 8:40:13 PM7/26/16
to
Hello Pascal,

Much thanks for this info. I'll play around with it.

Cheers,

jonR

0 new messages