• instance variables & properties

    From Jon Rossen@21:1/5 to All on Mon Jul 25 13:06:23 2016
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Pascal J. Bourguignon@21:1/5 to Jon Rossen on Tue Jul 26 01:43:53 2016
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jon Rossen@21:1/5 to Pascal J. Bourguignon on Mon Jul 25 19:05:58 2016
    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?

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Pascal J. Bourguignon@21:1/5 to Jon Rossen on Tue Jul 26 22:56:43 2016
    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.

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jon Rossen@21:1/5 to Pascal J. Bourguignon on Tue Jul 26 17:40:07 2016
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)