0

So I recently worked on extending the functionality of the List/Details app pattern: implementing a swipe on the Details view to move to the previous or next item in the parent list.

My solution requires adding one property to the Details view, 3 properties to the List view, implementing several new functions in each, and ensuring that some code gets run in the ViewDidLoad of both, and the DidSelect method of the table view.

I have been looking over the obvious mechanisms available in Cocoa and XCode 4 for re-use (snippets, subclassing and categories) in order to make this code (which is pretty much stand-alone) easier to add into other screens and apps and none seem suited for it. I can't figure out any mechanism that could encapsulate all of the needed changes, or simplify code re-use for other screens or projects.

Here are specific short comings:

Snippets: can only insert a single block of code, so isn't suited for code that requires changes in multiple files, or even multiple points in the same file.

Categories: can add new methods (or redefine existing methods) but can't add new properties.

Subclassing: can add new methods and properties, but doesn't address changes across multiple classes to implement a solution.

A hybrid approach (using snippets, categories and subclassing) could be used, but there is no mechanism to package such code modifications together!

Even if they are from other languages or IDEs, it would be interesting to know of any approaches used elsewhere.

Thanks

software evolved
  • 4,314
  • 35
  • 45

1 Answers1

1

Categories can in general add properties via associated references. This is a good solution to a wide variety of problems.


EDIT: If you just want some objects to have special behavior, and you know that when they're created, and you control their creation, then that sounds like subclassing. If you want specific instances to have special behavior and you don't control their creation, or you don't know that they need the behavior when they're created, then there is a tool to do that. It's called ISA swizzling, and it's how KVO is implemented.

Chapter 20 of iOS:PTL covers this quite a bit, but if you want some example code to show how it's done, see the ISASwizzle sample code. Robbie Hanson also has a short, accessible discussion of the topic. This is a fairly advanced technique and should be used carefully, but it is very powerful.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • A category does not apply to superclasses. But if you're creating a custom subclass already, then making a category seems somewhat redundant. I'm trying to picture this scenario. – Rob Napier Jul 26 '12 at 00:05
  • You should never use categories to replace methods. In many common cases this is undefined behavior. Categories must only be used to add new methods. – Rob Napier Jul 26 '12 at 00:49
  • Sorry, but your answer is missing the point of the question. I am trying to identify mechanisms that can re-use solutions that require logic across multiple classes/files, something which doesn't appear to be supported by snippets, categories, or subclassing. – software evolved Jul 26 '12 at 15:26
  • This sounds like Aspect Oriented Programming, which is not particularly amenable to ObjC, though there have been some attempts. One of the more recent: https://github.com/ndcube/AOP-for-Objective-C In ObjC, the patterns typically are more based on composition and inversion of control where you put your special logic into some controller delegate that drives the various other pieces, rather than modifying those other pieces. I'm not certain what changes you've made, but compare the approach taken by `UITableView` as a good example of the normal ObjC approach to problem solving. – Rob Napier Jul 26 '12 at 16:06
  • But I don't believe there is an ObjC pattern that really matches what you're discussing. – Rob Napier Jul 26 '12 at 16:06
  • Rob, patterns are design approaches and language independent, not implementation/code solutions. But thanks for the suggestions – software evolved Aug 02 '12 at 02:20