• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Strange property behavior
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Strange property behavior


  • Subject: Strange property behavior
  • From: "Loren Brichter" <email@hidden>
  • Date: Mon, 15 Sep 2008 09:37:49 -0700

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 (email@hidden)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Strange property behavior
      • From: "Clark Cox" <email@hidden>
  • Prev by Date: Re: NSUInteger in for-loop?
  • Next by Date: Re: Strange property behavior
  • Previous by thread: Re: Can't get to work setDoubleAction on NSMatrix with NSButtonCell
  • Next by thread: Re: Strange property behavior
  • Index(es):
    • Date
    • Thread