SwiftDevUtility
一个可以让编程更容易的 Swift 开发工具。
目录
基本信息
此工具提供了一些常用于编程的有用功能。它扩展了一些 Swift 标准库并创建了一些有用的结构。
要求
- iOS 11+
- Swift 4.2
安装
Cocoapods
pod 'SwiftDevUtility'
Carthage
github "chkkassd/SwiftDevUtility"
使用方法
- StringExtension
public var md5String: String
public var convertedDate: Date?
public static func timeFormatString(_ seconds: Double) -> String
- ArrayExtension
public func reject(_ predicate: (Element) -> Bool) -> [Element]
public func allMatch(_ predicate: (Element) -> Bool) -> Bool
- DictionaryExtension
public mutating func merge<S>(_ sequence: S) where S: Sequence, S Iterator.Element == (key: Key, value: Value)
public init<S: Sequence>(_ sequence: S) where S Iterator.Element == (key: Key, value: Value)
public func valueMap<T>(_ transform:(Value) -> T) -> [Key:T]
- SequenceExtension
public func unique() -> [Iterator.Element]
- DateExtension
public var standardTimeString: String
public var weekdayIndex: Int?
public var weekdayEnum: WeekDay
public var weekdayString: String
public var firstDayDate: Date?
public var lastDayDate: Date?
- URLExtension
public var pathString: String?
- UIImageExtension
public func scaledImage(_ rect: CGRect, _ compressionQuality: CGFloat) -> UIImage?
- UIViewExtension
@IBInspectable public var viewCornerRadius: CGFloat
@IBInspectable public var viewBorderWidth: CGFloat
@IBInspectable public var viewBorderColor: UIColor
- AppInfoHelper
public static var appVersion: String?
public static var appBuildVersion: String?
public static var appName: String?
- DirectoryPath
public static func pathOfDocuments() -> String
public static func pathOfTemporary() -> String
public static func pathOfCache() -> String
public static func urlInDocument(with component: String) -> URL
public static func urlInTemporary(with component: String) -> URL
public static func urlInCache(with component: String) -> URL
public static func directoryPathInDocument(withDirectoryName name: String) -> String
public static func directoryURLInDocument(withDirectoryName name: String) -> URL
- ScreenShot
public static func screenShot(withView view: UIView) -> UIImage?
- SSFSortDescriptor
StringExtension
public var md5String: String
返回原始字符串的MD5字符串的只读属性。
"Hello world".md5String//3e25960a79dbc69b674cd4ec67a72c62
public var convertedDate: Date?
将ISO8601字符串(yyyy-MM-dd'T'HH:mm:ssZ)转换为Date。
let time = "2019-3-12T12:34:23+0000" let dateTime = time.convertedDate//Optional(2019-03-12 12:34:23 +0000)
public static func timeFormatString(_ seconds: Double) -> String
将秒数转换为"mm:ss"样式的字符串。
String.timeFormatString(188.8)//03:08
ArrayExtension
public func reject(_ predicate: (Element) -> Bool) -> [Element]
删除匹配谓词的元素。
let a = [1,2,3,4,5,6] let b = a.reject{$0 % 2 == 0}//[1,3,5]
public func allMatch(_ predicate: (Element) -> Bool) -> Bool
检查所有元素是否匹配谓词。
let a = [1,2,3,4,5,6] let c = a.allMatch{$0 > 0}//true
DictionaryExtension
public mutating func merge<S>(_ sequence: S) where S: Sequence, S.Iterator.Element == (key: Key, value: Value)
将序列合并到字典中。
var a = ["name": "jack", "age": "10"] let b = ["name": "Tom", "age": "10", "address": "shanghai"] a.merge(b)//["address": "shanghai", "age": "10", "name": "Tom"]
init<S: Sequence>(_ sequence: S) where S.Iterator.Element == (key: Key, value: Value)
通过一个序列初始化字典,例如 [(键:xx, 值:xx)]。
let c = [("name", "Peter"), ("age", "20")] let d = Dictionary(c)//["age": "20", "name": "Peter"]
public func valueMap<T>(_ transform:(Value) -> T) -> [Key:T]
将字典的值与转换函数相关联,然后返回一个新的字典。
let e = ["height": 80, "width": 100] let f = e.valueMap { $0 / 2 }//["height": 40, "width": 50]
SequenceExtension
public func unique() -> [Iterator.Element]
寻找序列中的唯一元素,将重复元素删除。
let a = [1,2,3,4,4,8,8] let b = a.unique()//[1, 2, 3, 4, 8]
DateExtension
public var standardTimeString: String
将日期转换为字符串(yyyy-MM-dd HH:mm:ss)
let date = Date() date.standardTimeString//"2019-03-06 14:14:34"
public var weekdayIndex: Int?
将日期转换为周索引(星期六是0,星期日是1,星期一是2等)。
let date = Date() date.weekdayIndex//Optional(4)
public var weekdayEnum: WeekDay
将日期转换为星期枚举。
let date = Date() date.weekdayEnum//WED
public var weekdayString
将日期转换为中文星期字符串。
let date = Date() date.weekdayString//"周三"
public var firstDayDate: Date?
根据日期获取一周的第一天。
let date = Date() date.firstDayDate//Optional(2019-03-02 06:14:34 +0000)
public var lastDayDate: Date?
根据日期获取一周的最后一天。
let date = Date() date.lastDayDate//Optional(2019-03-08 06:14:34 +0000)
URLExtension
public var pathString: String?
将文件URL转换为路径字符串
let fileURL = DirectoryPath.urlInDocument(with: "test")//file:///Users/xxx/Library/Developer/CoreSimulator/Devices/761F483C-6A5C-4A4F-8F3C-7FDABBEC5A86/data/Containers/Data/Application/099F3AE8-8743-4466-B147-F84A91AE8C17/Documents/test let filePath = fileURL.pathString//Optional("/Users/xxx/Library/Developer/CoreSimulator/Devices/761F483C-6A5C-4A4F-8F3C-7FDABBEC5A86/data/Containers/Data/Application/099F3AE8-8743-4466-B147-F84A91AE8C17/Documents/test")
UIImageExtension
public func scaledImage(_ rect: CGRect, _ compressionQuality: CGFloat) -> UIImage?
使用特定矩形缩放大图像为小图像。
let aImage = UIImage(named: "water") aImage.size//Optional((180.0, 180.0)) let bImage = aImage?.scaledImage(CGRect(x: 0, y: 0, width: 20, height: 20), 1.0) bImage.size//Optional((20.0, 20.0))
UIViewExtension
@IBInspectable public var viewCornerRadius: CGFloat
描述视图的圆角并在Storyboard中体现。
@IBInspectable public var viewBorderWidth: CGFloat
描述视图的边框宽度并在故事板中反映。
@IBInspectable public var viewBorderColor: UIColor
描述视图的边框颜色并在故事板中反映。
AppInfoHelper
public static var appVersion: String?
获取应用程序版本。
AppInfoHelper.appVersion//Optional("1.0.1")
public static var appBuildVersion: String?
获取应用程序构建版本。
AppInfoHelper.appBuildVersion//Optional("1")
public static var appName: String?
获取应用程序名称。
AppInfoHelper.appName//Optional("SwiftDevUtilityDemo")
DirectoryPath
public static func pathOfDocuments() -> String
文档目录路径。
DirectoryPath.pathOfDocuments()// /Users/xxx/Library/Developer/CoreSimulator/Devices/761F483C-6A5C-4A4F-8F3C-7FDABBEC5A86/data/Containers/Data/Application/11F3E559-1365-473D-AFA5-C7C10AD2800A/Documents
public static func pathOfTemporary() -> String
临时目录路径。
DirectoryPath.pathOfTemporary()// /Users/xxx/Library/Developer/CoreSimulator/Devices/761F483C-6A5C-4A4F-8F3C-7FDABBEC5A86/data/Containers/Data/Application/11F3E559-1365-473D-AFA5-C7C10AD2800A/tmp/
public static func pathOfCache() -> String
缓存目录路径。
DirectoryPath.pathOfCache()// /Users/xxx/Library/Developer/CoreSimulator/Devices/761F483C-6A5C-4A4F-8F3C-7FDABBEC5A86/data/Containers/Data/Application/11F3E559-1365-473D-AFA5-C7C10AD2800A/Library/Caches
public static func urlInDocument(with component: String) -> URL
在文档组件中创建URL。
DirectoryPath.urlInDocument(with: "haha")//file:///Users/xxx/Library/Developer/CoreSimulator/Devices/761F483C-6A5C-4A4F-8F3C-7FDABBEC5A86/data/Containers/Data/Application/11F3E559-1365-473D-AFA5-C7C10AD2800A/Documents/haha
public static func urlInTemporary(with component: String) -> URL
在临时组件中创建URL。
DirectoryPath.urlInTemporary(with: "hihi")//file:///Users/xxx/Library/Developer/CoreSimulator/Devices/761F483C-6A5C-4A4F-8F3C-7FDABBEC5A86/data/Containers/Data/Application/331A1DE9-81A4-4322-88BD-334E34AEBD06/tmp/hihi
public static func urlInCache(with component: String) -> URL
在缓存组件中创建URL。
DirectoryPath.urlInCache(with: "hehe")//file:///Users/xxx/Library/Developer/CoreSimulator/Devices/761F483C-6A5C-4A4F-8F3C-7FDABBEC5A86/data/Containers/Data/Application/331A1DE9-81A4-4322-88BD-334E34AEBD06/Library/Caches/hehe
public static func directoryPathInDocument(withDirectoryName name: String) -> String
在文档目录中创建目录路径。
DirectoryPath.directoryPathInDocument(withDirectoryName: "test1")// /Users/xxx/Library/Developer/CoreSimulator/Devices/761F483C-6A5C-4A4F-8F3C-7FDABBEC5A86/data/Containers/Data/Application/331A1DE9-81A4-4322-88BD-334E34AEBD06/Documents/test1
public static func directoryURLInDocument(withDirectoryName name: String) -> URL
在文档目录中创建目录URL。
DirectoryPath.directoryURLInDocument(withDirectoryName: "test2")//file:///Users/xxx/Library/Developer/CoreSimulator/Devices/761F483C-6A5C-4A4F-8F3C-7FDABBEC5A86/data/Containers/Data/Application/331A1DE9-81A4-4322-88BD-334E34AEBD06/Documents/test2
ScreenShot
public static func screenShot(withView view: UIView) -> UIImage?
裁剪特定视图。
let image = SSFScreenShot.screenShot(withView: aView)//It will crop aView and return a image
SSFSortDescriptor
public typealias SortDescriptor<T> = (T, T) -> Bool
函数类型(T, T)-> Bool的文档。
infix operator |>: LogicalDisjunctionPrecedence
public func |><T>(l: @escaping SortDescriptor<T>, r: @escaping SortDescriptor<T>) -> SortDescriptor<T>
将两个SortDescriptor类型混合。
public static func makeDescriptor<Key, Value>(key: @escaping (Key) -> Value, _ isAscending: @escaping (Value, Value) -> Bool) -> SortDescriptor<Key>
通过排序数据和排序方法创建特定的sortDescriptor。参数:
key
是一个返回实际排序值的函数。参数:isAscending
是一个比较排序值的函数。返回:一个使用key
和isAscending
的SortDescriptor函数。
SortedDescriptor示例
struct Dog {
var name: String = ""
var age: Int = 0
}
let dog1 = Dog(name: "laifu", age: 4)
let dog2 = Dog(name: "xiaohu", age: 3)
let dog3 = Dog(name: "dingdang", age: 8)
let dog4 = Dog(name: "laifu", age: 6)
let dog5 = Dog(name: "laifu", age: 9)
let dogs = [dog1, dog2, dog3, dog4, dog5]
let nameSortDescriptor: SortDescriptor<Dog> = SSFSortDescriptor.makeDescriptor(key: {$0.name}, >)
let ageSortDescriptor: SortDescriptor<Dog> = SSFSortDescriptor.makeDescriptor(key: {$0.age}, >)
let newDogs = dogs.sorted(by: nameSortDescriptor |> ageSortDescriptor)
print(newDogs)//[Dog(name: "xiaohu", age: 3), Dog(name: "laifu", age: 9), Dog(name: "laifu", age: 6), Dog(name: "laifu", age: 4), Dog(name: "dingdang", age: 8)]
联系
- 电子邮件:[email protected]
- 微博:@PeterShi
许可证
SwiftDevUtility 在 MIT 许可证下发布。详细信息请见LICENSE。