Create FUNDING.yml
Extensions and syntactic sugar to enrich the Swift standard library, iOS frameworks, and SwifterSwift.
pod 'SweeterSwift'
dependencies: [ .package(url: "https://github.com/yonat/SweeterSwift", from: "1.2.1") ]
Add button at the center of view:
view.addConstrainedSubview(button, constrain: .centerX, .centerY)
Put label over textField:
view.constrain(label, at: .leading, to: textField) view.constrain(textField, at: .top, to: label, at: .bottom, diff: 8)
Add child view controller covering all but the bottom margin:
addConstrainedChild(vc, constrain: .bottomMargin, .top, .left, .right)
App name with reasonable fallback to process name:
let appName = Bundle.main.name
App info string with name, version, and build number:
let appInfo = Bundle.main.infoString // "MyApp 1.0 #42"
Create object from a dictionary:
let decodableObject = MyDecodableClass(dictionary: aDictionary)
Export object to a dictionary:
let aDictionary = encodableObject.dictionary
Create with format:
let dateFormatter = DateFormatter(format: "cccc, MMMM dd")
Post a local notification using NotificationCenter.default:
NotificationCenter.default
notify(notificationName, userInfo: infoDictionary)
Respond to a local notification from NotificationCenter.default:
observeNotification(notificationName, selector: #selector(someFunc))
Create from HTML:
let attributedString = NSAttributedString(htmlString: "Hello, <b>world</b>!")
Turn a substring into a link:
mutableAttributedString.link(anchorText: "Hello", url: "https://ootips.org")
Dump contents to console for debugging:
managedObjectContext.printAllObjects()
Create a copy of the store for backup or for using later as initial setup:
managedObjectContext.backupStore()
Separate CamelCase into capitalized words:
let words = "winterIsComing".unCamelCased // "Winter Is Coming"
Change CamelCase into snake_case:
let key = "winterIsComing".camelToSnakeCased // "winter_is_coming"
Index of current enum case in allCases:
allCases
let index = MyEnum.someCase.index
Unwrap collection, shorthand for compactMap { $0 }:
compactMap { $0 }
let nonOptionals = optionalsCollection.compact
Avoid retain cycles when passing a member function as an @escaping closure:
var closure = weak(self, in: MyClass.someFunction)
Standard time intervals:
let myInterval: TimeInterval = .year + .month + .week + .day + .hour + .minute
Find the top-most view controller:
let topVC = UIApplication.topViewController()
Present modal over the top view controller:
UIApplication.present(modalVC)
Create a label with links by using a UITextView to auto-detect links and simulate UILabel appearance:
UITextView
UILabel
let hyperlinkedLabel = UITextView(simulatedLabelWithLinksInText: "More at https://ootips.org")
Remove an arranged subview from the view hierarchy, not just the stack arrangement:
stackView.removeArrangedSubviewCompletely(subview)
Remove all arranged subviews from the view hierarchy, not just the stack arrangement:
stackView.removeAllArrangedSubviewsCompletely()
Search the view hierarchy recursively for a subview that meets a condition:
let blueSubview = view.viewInHierarchy(where { $0.backgroundColor == .blue })
Search the view hierarchy recursively for a subview with a specific class:
let button = view.viewWithClass(UIButton.self)
@yonatsharon
https://github.com/yonat/SweeterSwift
©Copyright 2023 CCF 开源发展委员会 Powered by Trustie& IntelliDE 京ICP备13000930号
SweeterSwift
Extensions and syntactic sugar to enrich the Swift standard library, iOS frameworks, and SwifterSwift.
Installation
CocoaPods:
Swift Package Manager:
Usage
Auto Layout
Add button at the center of view:
Put label over textField:
Add child view controller covering all but the bottom margin:
Bundle
App name with reasonable fallback to process name:
App info string with name, version, and build number:
Codable
Create object from a dictionary:
Export object to a dictionary:
DateFormatter
Create with format:
NotificationCenter
Post a local notification using
NotificationCenter.default
:Respond to a local notification from
NotificationCenter.default
:NSAttributedString
Create from HTML:
Turn a substring into a link:
NSManagedObjectContext
Dump contents to console for debugging:
Create a copy of the store for backup or for using later as initial setup:
String
Separate CamelCase into capitalized words:
Change CamelCase into snake_case:
Swift Standard Library
Index of current enum case in
allCases
:Unwrap collection, shorthand for
compactMap { $0 }
:Avoid retain cycles when passing a member function as an @escaping closure:
TimeInterval
Standard time intervals:
UIApplication
Find the top-most view controller:
Present modal over the top view controller:
UILabel / UITextView
Create a label with links by using a
UITextView
to auto-detect links and simulateUILabel
appearance:UIStackView
Remove an arranged subview from the view hierarchy, not just the stack arrangement:
Remove all arranged subviews from the view hierarchy, not just the stack arrangement:
UIView
Search the view hierarchy recursively for a subview that meets a condition:
Search the view hierarchy recursively for a subview with a specific class:
Meta
@yonatsharon
https://github.com/yonat/SweeterSwift