1

I am looking for a way to extend an existing class with new variables without creating my own new class, e.g. add an NSIndexPath to UITableViewCell to be able to access row or section without the use of tags.

I known i can add new functions with an extension

extension UITableViewCell {
    // i can do this
    func myExtendedFunction() {}
    // but this is not possible
    var indexPath:NSIndexPath
}

Is there a way to do this, or do i always have to build my own classes?

Wain
  • 118,658
  • 15
  • 128
  • 151
Michael
  • 182
  • 1
  • 9
  • http://stackoverflow.com/questions/24133058/is-there-a-way-to-set-associated-objects-in-swift – Wain Jan 28 '16 at 15:11
  • In order to get the indexPath of a `UITableViewCell`, you can use `UITableView`'s `indexPathForCell(_:)`. – Thomas Müller Jan 29 '16 at 00:41
  • @Thomas: though this does not answer the general question, it is a too-easy-to-be-true solution for the indexPath 'problem', thx ;) – Michael Jan 29 '16 at 10:46

1 Answers1

0

You need to subclass to add properties to a class.

There are workarounds relying on the objc runtime to allow you to associate objects with each other, but these should not be used in the typical case (they are typically more complex than subclassing, they make the relationships between objects non obvious, and have their own limitations).

alexkent
  • 1,556
  • 11
  • 25