Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JohnSundell/b1eeba78cb0042e829b4771f3b6c1a54 to your computer and use it in GitHub Desktop.
Save JohnSundell/b1eeba78cb0042e829b4771f3b6c1a54 to your computer and use it in GitHub Desktop.
Code sample #6 from my Medium post "Namespacing Swift code with nested types"
struct Post {
typealias TextFormatter = PostTextFormatter
let id: Int
let author: User
let title: String
let text: String
}
class PostTextFormatter {
typealias Option = PostTextFormatterOption
private let options: Set<Option>
init(options: Set<Option>) {
self.options = options
}
func formatTitle(for post: Post) -> String {
return post.title.formatted(withOptions: options)
}
func formatText(for post: Post) -> String {
return post.text.formatted(withOptions: options)
}
}
enum PostTextFormatterOption {
case highlightNames
case highlightLinks
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment