title: Add an extension/method to all objects in Swift
body: <p>In Objective-C, all objects can be treated as type id, and nearly all objects inherit from NSObject. (Blocks don't, but that's about the only exception.)</p>
  
  <p>Thus it's possible to create an Objective-C category that extends ALL Objective-C objects. (ignoring blocks) </p>
  
  <p>In Objective-C, I created an extension to NSObject that uses associated objects to optionally attach a dictionary to any NSObject. That enabled me to implement methods <code>setAssocValue:forKey:</code> and <code>assocValueForKey:</code> that makes it possible to attach a key/value pair to any NSObject. This is useful in lots of circumstances.</p>
  
  <p>It makes it possible to add stored properties to a category, just for example. You just write a getter/setter that uses the associated value methods to attach a stored object, and away you go.</p>
  
  <p>It also makes it possible to attach values to existing system objects at runtime. You can hang data or blocks of code on buttons, or do whatever you need to do.</p>
  
  <p>I'd like to do the same thing in Swift.</p>
  
  <p>However, Swift does not have a common base class for all objects like Objective-C does. <code>AnyObject</code> and <code>Any</code> are protcols.</p>
  
  <p>Thus, </p>
  
  <pre><code>extension AnyObject
  </code></pre>
  
  <p>Won't compile. </p>
  
  <p>I'm at a loss as to where to "attach" my <code>setAssocValue:forKey:</code> and <code>assocValueForKey:</code> methods in Swift. </p>
  
  <p>I could create a base class for my extension, but that defeats the point of using an extension. I could make my base object an Objective-C NSObject, but that means all my objects have to be NSObjects, and Swift objects are not NSObjects by default.</p>
  
  <p>(BTW, this question applies to both the Mac OS and iOS platforms)</p>