In general, follow the official guidelines.
.swift
extension.EasyTapButton
then the file should be called EasyTapButton.swift
. If you have a view controller, along with some helper types, then using the name of the view controller will likely be the best choice. A good heuristic to use is that if it’s ever unclear what to name a file, it may be worth splitting that file up.MyType+Extensions.swift
unless the extension is purely adopting a protocol; in that case the extension should be named MyType+ProtocolBeingConformedTo.swift
, for example MyDictionary+NSCopying
.swiftUpperCamelCase
Provide a pre-fixed Objective-C name before the class or protocol definition.
@objc class Event {
// bad: exported to Objective-C as class Event without a prefix
}
@objc(XYZEvent)
class Event {
// good
}
protocol IFooEventHandler {
// bad: the "I" prefix is not necessary
}
protocol FooEventHandler {
// bad: "er" is not a good suffix for a protocol
}
protocol FooEventHandling {
// good
}
The first parameter should always be the item that is calling the delegate method.
Dedicating the first parameter to be the item that is calling the delegate method follows Apple’s API design pattern.
// really bad: there's no parameter for the item that is calling the delegate method.
func didDeleteDraft()
// bad: the delegate method caller is not the first parameter
func didDeleteDraft(draft: Draft, draftManager: DraftManager)
// good
func draftManager(_ manager: DraftManager, didDeleteDraft draft: Draft)
An event handler method should be name following this pattern:
handle<EventSource>[<Action>]
where EventSource
is the UIControl or NSNotification which fired the event and Action
is a past-tense verb described what happened.
// bad: unclear what was the event source
// An NSNotification handler (EventSource=KeyboardDidShowNotification, Action=NULL)
didShowKeyboardNotification()
// good: the event source is clearly part of the function name
// An NSNotification handler (EventSource=KeyboardDidShowNotification, Action=NULL)
handleKeyboardDidShowNotification()
// bad: unclear that this function is handling the notification
// A UIButton handler (EventSource=ConfirmButton, Action=Tapped)
confirmButtonWasTapped()
// good: the function clearly handles the notification
// A UIButton handler (EventSource=ConfirmButton, Action=Tapped)
handleConfirmButtonTapped()
lowerCamelCase
.index
and counter
.let confirm: Button
is bad, but let confirmButton: Button
is good.is
and has
make it clear that a variable is a boolean.