• 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
Re: Initialize a subclass object with a base class object
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Initialize a subclass object with a base class object


  • Subject: Re: Initialize a subclass object with a base class object
  • From: Andy Lee <email@hidden>
  • Date: Tue, 20 May 2008 08:55:34 -0400

On May 20, 2008, at 6:17 AM, Liviu Andron wrote:
On Tue, May 20, 2008 at 10:57 AM, stephen joseph butler <
email@hidden> wrote:

On Tue, May 20, 2008 at 1:50 AM, Liviu Andron
<email@hidden> wrote:
setRepresentedObject: is a hint, thanks, but it doesn't resolve the
problem
either: he MUST copy every property from the original NSButtonCell (font,
attributedTitle, imageDimsWhenDisabled, etc).

This is how you will have to do it.


[buttonCellInstance setAlignment:[nsButtonCellInstance alignment]];

With some comments the code has 40 lines, and I copied only what I thought
it will be useful.
This is the solution for the moment. Solution A (see below).

Though tedious and vulnerable to future changes to NSButtonCell, this has the advantage that it is very straightforward. Unlike approaches that copy ivars, it also handles the case where nsButtonCellInstance is actually an instance of some weird subclass that happens to override the -alignment method, for example, to return a hard-coded value.


Thinking a little more about this, I wonder if you could take advantage of KVC not only for this case but in the general case of copying properties with the same name from one arbitrary object to another, assuming both objects are key-value coded. You could add a category to NSObject like this:

- (void)copyProperties:(id)otherObject
{
for all keys in otherObject
if I have a key of the same name
[self setValue:[otherObject valueForKey:key] forKey:key];
}


There would be some trickiness if the type of a property does not match, but if you know you're passing a compatible object as otherObject this shouldn't be a problem in many cases. Also, note this only makes a shallow copy. And implementing the "for all keys" part might have to do some guesswork (or perhaps simply fail) if the object doesn't implement -classDescription. Note that I have only a shallow knowledge of KVC, so maybe I'm missing something that would make this simple.

I wonder if IB does something like this when you change the class of an object. For example, suppose you have two subclasses of NSTextField, MyTextField and MyOtherTextField. If you change an NSTextField to an instance of MyTextField, IB has to make an instance of MyTextField with all the NSTextField properties copied over. If you then change the class again to MyOtherTextField, IB has to make an instance of MyOtherTextField with the NSTextField properties -- and only those properties -- copied over.

--Andy

_______________________________________________

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: Initialize a subclass object with a base class object
      • From: Andy Lee <email@hidden>
References: 
 >Re: Initialize a subclass object with a base class object (From: "Liviu Andron" <email@hidden>)
 >Re: Initialize a subclass object with a base class object (From: "stephen joseph butler" <email@hidden>)
 >Re: Initialize a subclass object with a base class object (From: "Liviu Andron" <email@hidden>)

  • Prev by Date: Re: Accessing [event modifierFlags] in an action method?
  • Next by Date: Re: Initialize a subclass object with a base class object
  • Previous by thread: Re: Initialize a subclass object with a base class object
  • Next by thread: Re: Initialize a subclass object with a base class object
  • Index(es):
    • Date
    • Thread