I'm no fan of these newfangled Objective-C 2.0 features that you
whippersnappers seem so excited about (call me old fashioned), but I'm
giving properties a shot, especially now that I'm working on some
stuff for [REDACTED].

I've notice something strange with properties that I hope someone can
clear up for me.  Either something is very, very wrong with
Objective-C 2.0, or I just haven't gotten enough sleep.

Say I have a class like so:

------------------

@interface MyTestClass : NSObject
@property (readonly) NSString *someProperty;
- (MyTestClass *)someMethod;
@end

@implementation MyTestClass

- (NSString *)someProperty
{
        return @"Some String";
}

- (MyTestClass *)someMethod
{
        NSLog(@"someMethod called");
        return self;
}

@end

-----------------------

MyTestClass has one method, and one property.

Now, say I do something like this:

        MyTestClass *o = [[MyTestClass alloc] init];
        [o someMethod];

things work great.  [o someMethod] gets called, logs, and returns
itself.  Fantastic.

Of course, now I can do something like this:

        MyTestClass *ret = [o someMethod];
        NSString *s = ret.someProperty;
        NSLog(@"got string %@", s);

Which, again, works as expected.  My output looks like so:
> someMethod called
> got string Some String

Now, the confusing part.  Say I smoosh together the above like so:

        NSString *n = [o someMethod].someProperty; // this is where the
weirdness happens
        NSLog(@"got string %@", n);

My output looks like this:
> someMethod called
> someMethod called
> got string Some String

Call me crazy, but I'm only calling [o someMethod] once in the above
code... so why is it getting called twice?  Imagine my frustration
tracking down the bug when -someMethod has side effects :).

Interesting side note, calling the property with the normal
Objective-C syntax works fine like so:
        NSString *m = [[o someMethod] someProperty];
        NSLog(@"got string %@", m);
-- outputs --
> someMethod called
> got string Some String

Note that I've only tried this on [REDACTED], but imagine that it
works the same way on the desktop.

WTF?

Thanks,
Loren
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to