I have noticed a major quirk in Objective C a couple of times now but haven't seen it reported anywhere else. Is it just me?
You have two classes and both contain a private variable with the same name. Class A has an int called _score and class B has an object called _score. Now I have just had an instance where class B declares this object, calls the alloc/init methods but what is returned is an int. This of course gets my app very confused. Surely with encapsulation, you shouldn't have to worry about using the same variable name twice. Is this a "feature" of OC and if so, how can i turn it off?
> I have noticed a major quirk in Objective C a couple of times now but haven't seen it reported anywhere else. Is it just me?
> You have two classes and both contain a private variable with the same name. Class A has an int called _score and class B has an object called _score. Now I have just had an instance where class B declares this object, calls the alloc/init methods but what is returned is an int. This of course gets my app very confused. Surely with encapsulation, you shouldn't have to worry about using the same variable name twice. Is this a "feature" of OC and if so, how can i turn it off?
> Shaun
> -- > You received this message because you are subscribed to the Google Groups "Brighton iPhone Creators" group. > To post to this group, send email to brighton-iphone-creators@googlegroups.com. > To unsubscribe from this group, send email to brighton-iphone-creators+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/brighton-iphone-creators?hl=en.
> Presumably you also mean that B inherits from A?
> On 16 Jan 2012, at 10:45, Shaun Pryszlak wrote:
> > Hi all
> > I have noticed a major quirk in Objective C a couple of times now but > haven't seen it reported anywhere else. Is it just me?
> > You have two classes and both contain a private variable with the same > name. Class A has an int called _score and class B has an object called > _score. Now I have just had an instance where class B declares this object, > calls the alloc/init methods but what is returned is an int. This of > course gets my app very confused. Surely with encapsulation, you shouldn't > have to worry about using the same variable name twice. Is this a > "feature" of OC and if so, how can i turn it off?
> > Shaun
> > -- > > You received this message because you are subscribed to the Google > Groups "Brighton iPhone Creators" group. > > To post to this group, send email to > brighton-iphone-creators@googlegroups.com. > > To unsubscribe from this group, send email to > brighton-iphone-creators+unsubscribe@googlegroups.com. > > For more options, visit this group at > http://groups.google.com/group/brighton-iphone-creators?hl=en.
> -- > You received this message because you are subscribed to the Google Groups > "Brighton iPhone Creators" group. > To post to this group, send email to > brighton-iphone-creators@googlegroups.com. > To unsubscribe from this group, send email to > brighton-iphone-creators+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/brighton-iphone-creators?hl=en.
> Nope, both are separate classes with _score declared locally.
> Shaun
> On 16 January 2012 10:46, Chris Ross <darkrock.ch...@googlemail.com> wrote:
> Presumably you also mean that B inherits from A?
> On 16 Jan 2012, at 10:45, Shaun Pryszlak wrote:
> > Hi all
> > I have noticed a major quirk in Objective C a couple of times now but haven't seen it reported anywhere else. Is it just me?
> > You have two classes and both contain a private variable with the same name. Class A has an int called _score and class B has an object called _score. Now I have just had an instance where class B declares this object, calls the alloc/init methods but what is returned is an int. This of course gets my app very confused. Surely with encapsulation, you shouldn't have to worry about using the same variable name twice. Is this a "feature" of OC and if so, how can i turn it off?
> > Shaun
> > -- > > You received this message because you are subscribed to the Google Groups "Brighton iPhone Creators" group. > > To post to this group, send email to brighton-iphone-creators@googlegroups.com. > > To unsubscribe from this group, send email to brighton-iphone-creators+unsubscribe@googlegroups.com. > > For more options, visit this group at http://groups.google.com/group/brighton-iphone-creators?hl=en.
> -- > You received this message because you are subscribed to the Google Groups "Brighton iPhone Creators" group. > To post to this group, send email to brighton-iphone-creators@googlegroups.com. > To unsubscribe from this group, send email to brighton-iphone-creators+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/brighton-iphone-creators?hl=en.
> -- > You received this message because you are subscribed to the Google Groups "Brighton iPhone Creators" group. > To post to this group, send email to brighton-iphone-creators@googlegroups.com. > To unsubscribe from this group, send email to brighton-iphone-creators+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/brighton-iphone-creators?hl=en.
> Nope, both are separate classes with _score declared locally.
> Shaun
> On 16 January 2012 10:46, Chris Ross <darkrock.ch...@googlemail.com>wrote:
>> Presumably you also mean that B inherits from A?
>> On 16 Jan 2012, at 10:45, Shaun Pryszlak wrote:
>> > Hi all
>> > I have noticed a major quirk in Objective C a couple of times now but >> haven't seen it reported anywhere else. Is it just me?
>> > You have two classes and both contain a private variable with the same >> name. Class A has an int called _score and class B has an object called >> _score. Now I have just had an instance where class B declares this object, >> calls the alloc/init methods but what is returned is an int. This of >> course gets my app very confused. Surely with encapsulation, you shouldn't >> have to worry about using the same variable name twice. Is this a >> "feature" of OC and if so, how can i turn it off?
>> > Shaun
>> > -- >> > You received this message because you are subscribed to the Google >> Groups "Brighton iPhone Creators" group. >> > To post to this group, send email to >> brighton-iphone-creators@googlegroups.com. >> > To unsubscribe from this group, send email to >> brighton-iphone-creators+unsubscribe@googlegroups.com. >> > For more options, visit this group at >> http://groups.google.com/group/brighton-iphone-creators?hl=en.
>> -- >> You received this message because you are subscribed to the Google Groups >> "Brighton iPhone Creators" group. >> To post to this group, send email to >> brighton-iphone-creators@googlegroups.com. >> To unsubscribe from this group, send email to >> brighton-iphone-creators+unsubscribe@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/brighton-iphone-creators?hl=en.
> -- > You received this message because you are subscribed to the Google Groups > "Brighton iPhone Creators" group. > To post to this group, send email to > brighton-iphone-creators@googlegroups.com. > To unsubscribe from this group, send email to > brighton-iphone-creators+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/brighton-iphone-creators?hl=en.
> Nope, both are separate classes with _score declared locally.
> Shaun
> On 16 January 2012 10:46, Chris Ross <darkrock.ch...@googlemail.com> wrote:
> Presumably you also mean that B inherits from A?
> On 16 Jan 2012, at 10:45, Shaun Pryszlak wrote:
> > Hi all
> > I have noticed a major quirk in Objective C a couple of times now but haven't seen it reported anywhere else. Is it just me?
> > You have two classes and both contain a private variable with the same name. Class A has an int called _score and class B has an object called _score. Now I have just had an instance where class B declares this object, calls the alloc/init methods but what is returned is an int. This of course gets my app very confused. Surely with encapsulation, you shouldn't have to worry about using the same variable name twice. Is this a "feature" of OC and if so, how can i turn it off?
> > Shaun
> > -- > > You received this message because you are subscribed to the Google Groups "Brighton iPhone Creators" group. > > To post to this group, send email to brighton-iphone-creators@googlegroups.com. > > To unsubscribe from this group, send email to brighton-iphone-creators+unsubscribe@googlegroups.com. > > For more options, visit this group at http://groups.google.com/group/brighton-iphone-creators?hl=en.
> -- > You received this message because you are subscribed to the Google Groups "Brighton iPhone Creators" group. > To post to this group, send email to brighton-iphone-creators@googlegroups.com. > To unsubscribe from this group, send email to brighton-iphone-creators+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/brighton-iphone-creators?hl=en.
> -- > You received this message because you are subscribed to the Google Groups "Brighton iPhone Creators" group. > To post to this group, send email to brighton-iphone-creators@googlegroups.com. > To unsubscribe from this group, send email to brighton-iphone-creators+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/brighton-iphone-creators?hl=en.
I have fixed it now but it was the second time it had come up, so I just wondered if anyone else had noticed it. It is probably just some subtle scoping thing that I am not aware of. Never mind.
We got a meeting this month? I might make a new years resolution to try and attend at least one.
Shaun
On 16 January 2012 11:17, Chris Ross <darkrock.ch...@googlemail.com> wrote:
> Can you provide an example of this where it happens?
> [I just put together an example and can't see that it is going wrong]
> Chris
> On 16 Jan 2012, at 11:11, Shaun Pryszlak wrote:
> > Nope, both are separate classes with _score declared locally.
> > Shaun
> > On 16 January 2012 10:46, Chris Ross <darkrock.ch...@googlemail.com> > wrote:
> > Presumably you also mean that B inherits from A?
> > On 16 Jan 2012, at 10:45, Shaun Pryszlak wrote:
> > > Hi all
> > > I have noticed a major quirk in Objective C a couple of times now but > haven't seen it reported anywhere else. Is it just me?
> > > You have two classes and both contain a private variable with the same > name. Class A has an int called _score and class B has an object called > _score. Now I have just had an instance where class B declares this object, > calls the alloc/init methods but what is returned is an int. This of > course gets my app very confused. Surely with encapsulation, you shouldn't > have to worry about using the same variable name twice. Is this a > "feature" of OC and if so, how can i turn it off?
> > > Shaun
> > > -- > > > You received this message because you are subscribed to the Google > Groups "Brighton iPhone Creators" group. > > > To post to this group, send email to > brighton-iphone-creators@googlegroups.com. > > > To unsubscribe from this group, send email to > brighton-iphone-creators+unsubscribe@googlegroups.com. > > > For more options, visit this group at > http://groups.google.com/group/brighton-iphone-creators?hl=en.
> > -- > > You received this message because you are subscribed to the Google > Groups "Brighton iPhone Creators" group. > > To post to this group, send email to > brighton-iphone-creators@googlegroups.com. > > To unsubscribe from this group, send email to > brighton-iphone-creators+unsubscribe@googlegroups.com. > > For more options, visit this group at > http://groups.google.com/group/brighton-iphone-creators?hl=en.
> > -- > > You received this message because you are subscribed to the Google > Groups "Brighton iPhone Creators" group. > > To post to this group, send email to > brighton-iphone-creators@googlegroups.com. > > To unsubscribe from this group, send email to > brighton-iphone-creators+unsubscribe@googlegroups.com. > > For more options, visit this group at > http://groups.google.com/group/brighton-iphone-creators?hl=en.
> -- > You received this message because you are subscribed to the Google Groups > "Brighton iPhone Creators" group. > To post to this group, send email to > brighton-iphone-creators@googlegroups.com. > To unsubscribe from this group, send email to > brighton-iphone-creators+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/brighton-iphone-creators?hl=en.
How/where are you declaring this? And which compiler are you using (old or new)?
Obj-C only recently got upgraded from "a bunch of hand-written scripts that convert it to C code, which is then sent to a C compiler".
Obj-C will let you declare almost anything almost anywhere, but with the Xcode 3.x compiler, small quirks in those scripts could end up generating C code that didn't really do what it was supposed to.
In particular, when I started with iPhone, you could get some pretty bad screw-ups because the scripts had a poor naming scheme, and would sometimes convert two independently scoped obj-c things to exactly the same name in C - and C has no namespacing, so that's guaranteed to blow up in your face.
IF you're using the old compiler, I'd assume you're seeing another one of those.
If you're using the new compiler, well ... it still has some bugs, although I haven't seen any problems with scope / global names so far.
On 16 January 2012 10:45, Shaun Pryszlak <thirdpr...@gmail.com> wrote:
> I have noticed a major quirk in Objective C a couple of times now but > haven't seen it reported anywhere else. Is it just me?
> You have two classes and both contain a private variable with the same name. > Class A has an int called _score and class B has an object called _score. > Now I have just had an instance where class B declares this object, calls > the alloc/init methods but what is returned is an int. This of course gets > my app very confused. Surely with encapsulation, you shouldn't have to > worry about using the same variable name twice. Is this a "feature" of OC > and if so, how can i turn it off?
> Shaun
> -- > You received this message because you are subscribed to the Google Groups > "Brighton iPhone Creators" group. > To post to this group, send email to > brighton-iphone-creators@googlegroups.com. > To unsubscribe from this group, send email to > brighton-iphone-creators+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/brighton-iphone-creators?hl=en.