Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Tifo Audi A.P tifoaudii

🎯
Focusing
View GitHub Profile
View URLHandlerViewController.swift
extension URLHandler where Self: UIViewController {
func openUrl(url: URL) {
// open url
}
}
final class ProfileViewController: UIViewController, URLHandler {}
View URLHandler.swift
protocol URLHandler {
func openUrl(url: URL)
}
extension URLHandler {
func openUrl(url: URL) {
// open url
}
}
View UITextViewLocalizable.swift
extension UITextView: MutableLocalizable {
func translate(to language: Language) {
switch language {
case .indonesia:
text = "Tolong"
case .thailand:
text = "..."
case .vietnam:
text = "..."
}
View Text.swift
struct Text: MutableLocalizable {
private var content: String = "Help"
static let languages: [Language] = [
.indonesia,
.vietnam,
.thailand
]
View Localizable.swift
enum Language {
case indonesia
case thailand
case vietnam
}
protocol Localizable {
static var languages: [Language] { get }
}
View Loadable.swift
protocol Loadable {
func showLoading()
}
class HomeViewController: UIViewController, Loadable {
func showLoading() {}
}
class ExploreViewController: UIViewController, Loadable {
func showLoading() {}
View Search.swift
extension TreeNode where T: Equatable {
public func search(_ value: T) -> TreeNode? {
var result: TreeNode?
depthFirstTraversal { node in
if node.value == value {
result = node
}
}
return result
View LevelOrderResult.swift
Level-Order Result:
Food
western
asia
pizza
steak
pasta
sushi
chinese food
View LevelOrderTraversal.swift
extension TreeNode {
public func levelOrderTraversal(visit: (TreeNode) -> Void) {
visit(self)
var queue = [TreeNode]()
children.forEach { queue.append($0) }
while !queue.isEmpty {
let node = queue.removeFirst()
visit(node)
node.children.forEach { queue.append($0) }
View DepthFirst.swift
Depth-first traversal result:
Food
western
pizza
meat lover
cheese lover
american favorite
steak
pasta