Swift Doesn’t Understand Past Tense

The feature I’m missing most from C#, in Swift, is multicast delegation. If that doesn’t mean anything to you, as a Swift user, think of it as a closure which can reference multiple functions at once. Events are backed by multicast delegates, and they allow us to properly take on the tense which was used in that Wikipedia article I’ve been referencing.

English: Bob laughed.
C#: Bob.Laughed();

English: Sam helped you.
C#: Sam.Helped(you);

English: Jim gave Jill his dog.
C#: Jim.Gave(new GaveEventArgs(receiver: Jill, item: Dog));

class Person {
   public event Action Laughed;
   public event Action<Person> Helped;
   public event Action<GaveEventArgs> Gave;
	
   public struct GaveEventArgs {
      public readonly Person Receiver;
      public readonly object Item;
      public GaveEventArgs(Person receiver, object item) {
         Receiver = receiver;
         Item = item;
      }
   }

   // Necessary to support the code above, 
   // but not realistic usage, I know.
   Person Jim, Jill, Dog;
}

Responding to events

C#
Bob.laughed += LaughAtBobsWeirdLaugh;

Swift

NSNotificationCenter.defaultCenter().addObserver(
   self,
   selector: "laughAtBobsWeirdLaugh",
   name: "bob laughed",
   object: nil
)

Ten sees NSNotificationCenter

Leave a Reply

Your email address will not be published. Required fields are marked *