Groups | Search | Server Info | Login | Register


Groups > comp.lang.objective-c > #280

Re: instance variables & properties

From Jon Rossen <jonr17@comcast.net>
Newsgroups comp.lang.objective-c
Subject Re: instance variables & properties
Date 2016-07-26 17:40 -0700
Organization albasani.net
Message-ID <nn8vtb$5da$1@news.albasani.net> (permalink)
References <nn5rh0$rep$1@news.albasani.net> <878twpthae.fsf@kuiper.lan.informatimago.com> <nn6gj7$ega$1@news.albasani.net> <871t2g6ruc.fsf@kuiper.lan.informatimago.com>

Show all headers | View raw


On 7/26/2016 1:56 PM, Pascal J. Bourguignon wrote:
> Jon Rossen <jonr17@comcast.net> writes:
>
>> On 7/25/2016 4:43 PM, Pascal J. Bourguignon wrote:
>>> 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.
>>>
>>>
>> 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?
>
> 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.
>

Hello Pascal,

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

Cheers,

jonR

Back to comp.lang.objective-c | Previous | NextPrevious in thread | Find similar


Thread

instance variables & properties Jon Rossen <jonr17@comcast.net> - 2016-07-25 13:06 -0700
  Re: instance variables & properties "Pascal J. Bourguignon" <pjb@informatimago.com> - 2016-07-26 01:43 +0200
    Re: instance variables & properties Jon Rossen <jonr17@comcast.net> - 2016-07-25 19:05 -0700
      Re: instance variables & properties "Pascal J. Bourguignon" <pjb@informatimago.com> - 2016-07-26 22:56 +0200
        Re: instance variables & properties Jon Rossen <jonr17@comcast.net> - 2016-07-26 17:40 -0700

csiph-web