Groups | Search | Server Info | Login | Register


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

Re: instance variables & properties

From "Pascal J. Bourguignon" <pjb@informatimago.com>
Newsgroups comp.lang.objective-c
Subject Re: instance variables & properties
Date 2016-07-26 01:43 +0200
Organization Informatimago
Message-ID <878twpthae.fsf@kuiper.lan.informatimago.com> (permalink)
References <nn5rh0$rep$1@news.albasani.net>

Show all headers | View raw


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.


-- 
__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

Back to comp.lang.objective-c | Previous | NextPrevious in thread | Next 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