要求
- iOS 10.0+ / macOS 10.12+
- Swift 5
安装
Tapticc 可通过 CocoaPods 获得。为了安装它,只需将以下行添加到您的 Podfile 中即可:
pod 'SwiftDevHints'
用法
- 字符串扩展
- 集合可选扩展
- 可选扩展
- 集合扩展
- UITableView 扩展
- UserDefaults 扩展
- UIColor 扩展
- Bundle 扩展
- 日期扩展
字符串扩展
返回字符串切片的安全方式
let string = "Hello, Swift!"
string[safe: 0..<5] // "Hello"
string[safe: 0...4] // "Hello"
string[safe: 0..<14] // nil
intBaseHex
从十六进制字符串获取整数值
print("FF".intBaseHex) // 255
print("0xFF".intBaseHex) // 255
trimmed()
" hello world \n\t".trimmed() // "hello world"
trimming()
var string = " hello world \n\t"
string.trimming()
string // "hello world"
isBlank
" \n\t ".isBlank // true
nilIfEmpty
"".nilIfEmpty // nil
收藏-可选扩展
isNilOrEmpty
"".isNilOrEmpty // true
([] as? [String]).isNilOrEmpty // true
可选扩展
isNone
和 isSome
"hello".isNone // false
"hello".isSome // true
var name: String? = nil
name.isNone // true
name.isSome // false
noneDo
和 someDo
var name: String? = nil
name.noneDo {
print("name is nil")
}
name.someDo {
print("name has a value \($0)")
}
收藏-扩展
在指定索引处返回元素的安全方式
let animals = ["Zebra", "Giraffe", "Tiger"]
let zebra = animals[safe: 0] // "Zebra"
let lion = animals[safe: 3] // nil
UITableView-扩展
注册与队列 UITableViewCell
class UserCell: UITableViewCell, NibReusable { }
tableView.register(UserCell.self)
let cell: UserCell = tableView.dequeueReusableCell(for: indexPath)
UICollectionView-扩展
注册与队列 UICollectionViewCell
class PhotoCell: UICollectionViewCell, NibReusable { }
collectionView.register(PhotoCell.self)
let cell: PhotoCell = collectionView.dequeueReusableCell(for: indexPath)
NSUserDefaults-扩展
使用 UserDefaults 的安全方式
extension UserDefaults.Name {
static let username: UserDefaults.Name = "SwiftDevHints-Demo.Username"
static let password: UserDefaults.Name = "SwiftDevHints-Demo.Password"
}
// Register Initial Value
UserDefaults.standard.register(defaults: [.username: "Unknown"])
// Set Value
UserDefaults.standard.set("Derek", forName: .username)
UserDefaults.standard.set("12345", forName: .password)
// Get Value
let username = UserDefaults.standard.string(forName: .username)
let password = UserDefaults.standard.string(forName: .password)
UIColor 扩展
使用基于 255 的 RGB 初始化 UIColor
let color = UIColor(red: 255, green: 32, blue: 171)
使用 RGB 十六进制字符串初始化 UIColor
let color = UIColor(hexString: "FF20AB")
创建与指定颜色对比的黑或白 UIColor 对象。
let color = UIColor(constrastingBlackOrWhiteColorOn: UIColor.green)
获取此颜色的十六进制值字符串(以“#”开头)
let hexString = UIColor.red.hexString // #FF0000
获取 RGB 或 HSBA 分量
let rgb = UIColor.red.rgbComponents // (red: 255, green: 0, blue: 0)
let rgba = UIColor.red.rgbaComponents // (red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
let hsba = UIColor.red.hsbaComponents // (hue: 0.0, saturation: 1.0, brightness: 1.0, alpha: 1.0)
获取随机颜色
let randomColor = UIColor.random
Bundle 扩展
简便方法访问 Info.plist 文件
let bundle = Bundle.main
let displayName = bundle.displayName
let identifier = bundle.identifier
let version = bundle.version
let build = bundle.build
// All keys
private enum InfoPlistKey: String {
case name = "CFBundleName"
case displayName = "CFBundleDisplayName"
case developmentRegion = "CFBundleDevelopmentRegion"
case identifier = "CFBundleIdentifier"
case version = "CFBundleShortVersionString"
case build = "CFBundleVersion"
case packageType = "CFBundlePackageType"
}
Date 扩展
一些便捷的方法
let today = Date() // December 17, 2017 at 5:54:46 PM GMT+8
let startOfToday = today.startOfDay // December 17, 2017 at 12:00:00 AM GMT+8
let endOfToday = today.endOfDay // December 17, 2017 at 11:59:59 PM GMT+8
let previousDay = today.previousDay // December 16, 2017 at 5:54:46 PM GMT+8
let nextDay = today.nextDay // December 18, 2017 at 5:54:46 PM GMT+8
// December 14, 2017 at 5:54:46 PM GMT+8
// December 15, 2017 at 5:54:46 PM GMT+8
// December 16, 2017 at 5:54:46 PM GMT+8
let last3Days = today.lastDays(withCount: 3, includingToday: false)
// December 17, 2017 at 5:54:46 PM GMT+8
// December 18, 2017 at 5:54:46 PM GMT+8
// December 19, 2017 at 5:54:46 PM GMT+8
let next3Days = today.nextDays(withCount: 3, includingToday: true)
参考
- https://github.com/Luur/SwiftTips#1-safe-way-to-return-element-at-specified-index
- https://github.com/onevcat/Kingfisher
- https://github.com/AliSoftware/Reusable
- https://github.com/andyyhope/nemo
作者
- Twitter: @derekcoder_
- Weibo: @derekcoder
- Email: [email protected]
许可
SwiftDevHints 在 MIT 许可下发布。详细信息请参阅 LICENSE 文件 See LICENSE。