Skip to content

Latest commit

 

History

History
58 lines (47 loc) · 1.53 KB

README.md

File metadata and controls

58 lines (47 loc) · 1.53 KB

SwiftHelper

Convenience classes wrapped with Swift features and useful Swift extensions

AttributedStringBuilder

AttributedString Builder encapsulated using the Swift resultBuilder feature

Screenshot 2023-06-03 at 21 38 06

Before:

let build1 = NSMutableAttributedString()
let start = NSAttributedString(string: "Hello", attributes: [
    NSAttributedString.Key.foregroundColor: UIColor.red
])
build1.append(start)
build1.append(NSAttributedString(string: " "))
let end = NSAttributedString(string: "World", attributes: [
    NSAttributedString.Key.foregroundColor: UIColor.yellow,
    NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 18)
])
build1.append(end)

Now:

let build = NSAttributedString {
    Text("Hello").color(.red)
    SpecialCharacters.space
    Text("World").color(.yellow).font(.boldSystemFont(ofSize: 18))
}

Building Alert using DSL

 let alertController = ActionSheet(title: "", message: nil) {
            Action.default("Share") {  }
            Action.default("Download") { }
            Action.cancel("Cancel") {  }
        }
 present(alertController, animated: true, completion: nil)
 

UserDefaults

extension UserDefaults {
    @UserDefault(key: "username", defaultValue: "Antoine van der Lee")
    static var username: String
}

let subscription = UserDefaults.$username.sink { username in
    print("New username: \(username)")
}
UserDefaults.username = "Test"