Disparate types for the same value

I noticed that the "contents" property of SCNMaterialProperty can be any of fivesix things:

  • NSColor
  • NSImage
  • CALayer
  • CGColorRef
  • CGImageRef
  • [Update: Michael Tsai points out that it can also be an NSArray of six images.]

The three classes — NSColor, NSImage, and CALayer — are about as unrelated as you can get within the Cocoa class hierarchy. Their only common ancestor is NSObject. It's true they all implement NSCoding, and two of them implement NSCopying, but these are such common, generic protocols they hardly count as relating the three classes in a meaningful way.

The two CG pointer types — CGColorRef and CGImageRef — are not toll-free bridged to any of the classes, although they are C-based counterparts to NSColor and NSImage.

I wonder if this property sets a record for the greatest number of disparate, specific types a value is allowed to have in Cocoa. I'm talking about formal Objective-C types, disregarding semantics. Offhand I can't think of an example with more than two.

For example, CALayer itself has a "contents" property which can be either a CGImageRef or, as of Mac OS 10.6, an NSImage. Another example is buttons and cells, which at least unofficially allow NSAttributedString for their titles even though the API specifies NSString. (I don't know if this is officially documented.) Bear in mind that NSString and NSAttributedString are not related by inheritance.

Note that in all the examples above, there is only one conceptual "type" for the value in question, whether it's "graphical contents of a rectangle" or simply "a string".

One thought on “Disparate types for the same value

  1. Pingback: Michael Tsai - Blog - SCNMaterialProperty.contents

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.