3

I'll start with what I'm trying to do: I wanted to create a nice little mixin that UIViews could utilize to abstract out intended touch events (that is, "in the way the user is dragging right now, do they really want to complete this action").

I thought the best way to do this would be to use a category, but I come to find they've been replaced by "extensions" in swift. Mostly. Apparently properties are now all computed.

To make this mixin work, I would need a local stored property in which to store a token "buffer of intent". But any attempt to add a var to the extension is presented with errors about how I need to provide setters and getters. A protocol won't work for the same reason. If I create a protocol alongside the extension, I still have to have a local ivar in which we store this new property.

So either one of two questions:

Am I going the long way around this, and if so, what should I be doing instead?

or

How do I do something where I can create a "mixin"-like structure that allows me to define simple properties and methods that should be mixed in?

dclowd9901
  • 6,756
  • 9
  • 44
  • 63
  • Mixins are not a pattern that is used with Cocoa. Could you give a more specific use case for what you are trying to do. More specific than "in the way the user is dragging right now, do they really want to complete this action". – Steve Waddicor Jun 19 '14 at 15:21
  • Let's say I'm dragging my thumb down to pull in a new view. If the drag was only, say, 3px, maybe I want to cancel pulling in the new view. But if the drag was 10px, and downward, I'd want to bring in the new view. Conversely, if they drag 20px, but then start pushing back up, I can infer they may not intend to pull in the view after all, and subsequently cancel bring in the new view after all. It seems like a pretty common vernacular in iOS, and I want to maintain it. I can think of many instances where this might be useful outside of a single context. – dclowd9901 Jun 19 '14 at 15:25
  • possible duplicate of [Is there a way to set associated objects in Swift?](http://stackoverflow.com/questions/24133058/is-there-a-way-to-set-associated-objects-in-swift) – Kreiri Jun 19 '14 at 15:31
  • @Kreiri I don't think so; that doesn't address my question. Though maybe I'm missing something so if you could explain to me how they're related. – dclowd9901 Jun 19 '14 at 15:43
  • "Apparently properties are now all computed" - you cannot add properties/ivars to class in Objective C category either. You can get around it by setting/getting associated objects in category using objc runtime functions. – Kreiri Jun 19 '14 at 15:47
  • Thank you Kreiri! Super informative. – dclowd9901 Jun 20 '14 at 18:46

1 Answers1

0

Mixins are not a pattern that is used with Cocoa, whether with Swift or Objective-C. As you've discovered, categories cannot add additional ivars, and protocols only define an interface, so don't have ivars either.

Cocoa generally prefers object use (or encapsulation) to inheritance. Inheritance does get used, but it's only single inheritance, so mixins are not possible. And they are never needed.

For the use case described, the right way to do it is via gesture recognizers. Information and code can be found here:

https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html#//apple_ref/doc/uid/TP40009541-CH2-SW2

Steve Waddicor
  • 2,197
  • 15
  • 20