SweeterSwift 1.2.5

SweeterSwift 1.2.5

Yonat Sharon 维护。



  • 作者
  • Yonat Sharon

SweeterSwift

Swift Version Build Status License CocoaPods Compatible
Platform PRs Welcome

扩展和语法糖来丰富 Swift 标准库、iOS 框架和 SwifterSwift

安装

CocoaPods

pod 'SweeterSwift'

Swift 包管理器

dependencies: [
    .package(url: "https://github.com/yonat/SweeterSwift", from: "1.2.1")
]

用法

自动布局

将按钮添加到视图中心

view.addConstrainedSubview(button, constrain: .centerX, .centerY)

将标签放置在文本字段上方

view.constrain(label, at: .leading, to: textField)
view.constrain(textField, at: .top, to: label, at: .bottom, diff: 8)

添加子视图控制器以覆盖除底部边距外的所有内容

addConstrainedChild(vc, constrain: .bottomMargin, .top, .left, .right)

应用名称,如有必要则回退到进程名称

let appName = Bundle.main.name

含名称、版本和构建号的App信息字符串

let appInfo = Bundle.main.infoString // "MyApp 1.0 #42"

Codable

从字典创建对象

let decodableObject = MyDecodableClass(dictionary: aDictionary)

将对象导出到字典

let aDictionary = encodableObject.dictionary

DateFormatter

使用格式创建

let dateFormatter = DateFormatter(format: "cccc, MMMM dd")

NotificationCenter

使用NotificationCenter.default发布本地通知

notify(notificationName, userInfo: infoDictionary)

NotificationCenter.default响应本地通知

observeNotification(notificationName, selector: #selector(someFunc))

NSAttributedString

从HTML创建

let attributedString = NSAttributedString(htmlString: "Hello, <b>world</b>!")

将子字符串转换为链接

mutableAttributedString.link(anchorText: "Hello", url: "https://ootips.org")

NSManagedObjectContext

将内容输出到控制台以进行调试

managedObjectContext.printAllObjects()

创建存储的副本以备份或稍后用作初始设置

managedObjectContext.backupStore()

String

将驼峰命名法拆分为首字母大写的单词

let words = "winterIsComing".unCamelCased // "Winter Is Coming"

将驼峰命名法转换为 snake_case

let key = "winterIsComing".camelToSnakeCased // "winter_is_coming"

Swift 标准库

当前枚举案例在allCases中的索引

let index = MyEnum.someCase.index

展开集合,简写为compactMap { $0 }

let nonOptionals = optionalsCollection.compact

在将成员函数作为@escaping闭包传递时,避免保留循环

var closure = weak(self, in: MyClass.someFunction)

TimeInterval

标准时间间隔

let myInterval: TimeInterval = .year + .month + .week + .day + .hour + .minute

UIApplication

查找最顶层的视图控制器

let topVC = UIApplication.topViewController()

在顶层视图控制器之上呈现模态视图

UIApplication.present(modalVC)

UILabel / UITextView

使用UITextView通过自动检测链接并模拟UILabel外观来创建带链接的标签

let hyperlinkedLabel = UITextView(simulatedLabelWithLinksInText: "More at https://ootips.org")

UIStackView

从视图层级中移除一个已排列的子视图,不仅仅是堆栈排列

stackView.removeArrangedSubviewCompletely(subview)

从视图层级中移除所有已排列的子视图,不仅仅是堆栈排列

stackView.removeAllArrangedSubviewsCompletely()

UIView

递归搜索视图层次结构中满足特定条件的子视图

let blueSubview = view.viewInHierarchy(where { $0.backgroundColor == .blue })

递归搜索具有特定类的子视图

let button = view.viewWithClass(UIButton.self)

Meta

@yonatsharon

https://github.com/yonat/SweeterSwift