objective c quirk

4 views
Skip to first unread message

Shaun Pryszlak

unread,
Jan 16, 2012, 5:45:08 AM1/16/12
to brighton-iph...@googlegroups.com
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

Chris Ross

unread,
Jan 16, 2012, 5:46:57 AM1/16/12
to brighton-iph...@googlegroups.com

Presumably you also mean that B inherits from A?

> --
> 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-iph...@googlegroups.com.
> To unsubscribe from this group, send email to brighton-iphone-cr...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/brighton-iphone-creators?hl=en.

Shaun Pryszlak

unread,
Jan 16, 2012, 6:11:29 AM1/16/12
to brighton-iph...@googlegroups.com
Nope, both are separate classes with _score declared locally.

Shaun

Nick Kuh

unread,
Jan 16, 2012, 6:15:25 AM1/16/12
to brighton-iph...@googlegroups.com
Hi Shaun

Perhaps you can share some code that demonstrates the problem as I'd also assumed you must be subclassing?

Nick

Paul Ledger

unread,
Jan 16, 2012, 6:16:11 AM1/16/12
to brighton-iph...@googlegroups.com
Not sure if I'm understanding this, but depending on what you are doing with the object you will get a number back, i.e. the objects Id?

Chris Ross

unread,
Jan 16, 2012, 6:17:18 AM1/16/12
to brighton-iph...@googlegroups.com

So to clarify:

What "returns an int" ?

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:

Shaun Pryszlak

unread,
Jan 16, 2012, 6:53:39 AM1/16/12
to brighton-iph...@googlegroups.com
Hi

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

Adam Martin

unread,
Jan 16, 2012, 7:42:58 AM1/16/12
to brighton-iph...@googlegroups.com
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.

Reply all
Reply to author
Forward
0 new messages