1

With Swift, it's no longer necessary to inherit from NSObject. Should I still subclass from NSObject for the data model classes in my iOS app?

Boon
  • 40,656
  • 60
  • 209
  • 315
  • Thank you Martin. Useful link - but my question may still be relevant because I am asking specifically data model classes. – Boon Jun 23 '14 at 16:12

1 Answers1

3

The main benefit of subclassing NSObject is that when bridging to Objective-C code, the class will be bridged over. If you have a bare Swift class, then it will not be visible via the Objective-C bridge.

You can decide whether or not you want to subclass NSObject based on whether or not you want to maintain Objective-C compatibility.

Cezary Wojcik
  • 21,745
  • 6
  • 36
  • 36
  • 3
    I want to add that subclassing `NSObject` implicitly adds `@objc` to your class. If you don't subclass `NSObject` and add `@objc` yourself, it will still be bridged to ObjC – Jack Jun 23 '14 at 16:29
  • Good point, forgot about that. – Cezary Wojcik Jun 23 '14 at 16:36
  • 1
    Also, pure swift classes cannot be the target or client of Key Value Observation, which is in my mind, the best way to interface between your model and your views. – David Berry Jun 23 '14 at 18:58