bogen
bogen

Reputation: 10422

How to chain multiple method calls in Swift?

I am building an open source component for shaking a UIView, and for accessibility I want the method call to be able to be called with an extra method specifying an accessibility text that should be read to VoiceOver

My current API is

button.shake(.Horizontal)

I want it to be able to be called with a way of posting an accessabilitynotification as well, and since there allready are so many optional arguments in the shake method signature, I was thinking chaining the method call would be nice.

So my idea is to

button.shake(.Horizontal).postAccessabilityNotification(text: "Your attention is required  on \(button.accessabilityLabel)")

Method signature would be

 public func postAccessabilityNotification(#text : String ) {
    UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, text)
}

Is it possible to chain the method call? Would be nice to post a default notification on shake() if the postAccessabilityNotification was not chained.

Here's the GitHub page: https://github.com/haaakon/SingleLineShakeAnimation

Upvotes: 1

Views: 1589

Answers (1)

rounak
rounak

Reputation: 9387

To chain, you'll have to return self from the shake method.

Upvotes: 5

Related Questions