12

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.)

Thus it's possible to create an Objective-C category that extends ALL Objective-C objects. (ignoring blocks)

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 setAssocValue:forKey: and assocValueForKey: that makes it possible to attach a key/value pair to any NSObject. This is useful in lots of circumstances.

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.

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.

I'd like to do the same thing in Swift.

However, Swift does not have a common base class for all objects like Objective-C does. AnyObject and Any are protcols.

Thus,

extension AnyObject

Won't compile.

I'm at a loss as to where to "attach" my setAssocValue:forKey: and assocValueForKey: methods in Swift.

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.

(BTW, this question applies to both the Mac OS and iOS platforms)

6
  • Thus my statement that "nearly all objects inherit from NSObject". I forgot about NSDistantObject, but did give the example of blocks, which are objects but not NSObjects. Commented May 11, 2015 at 19:30
  • I don't suppose Swift has any notion of "extension methods" (.NET) or "implicits" (Scala)? Commented May 11, 2015 at 19:31
  • I'm not familiar with C#/.NET. Can you explain what extension methods are? Commented May 11, 2015 at 20:27
  • Effectively compile-time decomposed static wrappers to 'fake' a method upon an expression of a particular compile-time type. That is, the original type/class/object itself is not modified, but it can be used as though an additional method was added. I have no idea if Swift offers something similar. Commented May 12, 2015 at 0:17
  • How is that different from Swift extensions or Objective-C categories (the topic of this thread)? It sounds like the same thing. Commented May 12, 2015 at 0:20

1 Answer 1

8

No. You've pretty much answered your own question--Swift objects don't have a base class, and the only real way to get around it is to inherit from NSObject.

Sign up to request clarification or add additional context in comments.

3 Comments

And in any case you wouldn't be covering structs and enums; most object types you define in Swift without needing inheritance should probably be structs.
@matt, true. It would be even more useful as an extension to Any, but Any is also a protocol.
This seems like a limitation of Swift that Apple should address. Extensions to NSObject can be very useful in Objective-C. Seems like there needs to be a language-level way to add extensions to AnyObject (or even Any)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.