This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public extension CGImage { | |
func bitmapImage() -> CGImage? { | |
guard | |
let colorSpace = colorSpace, | |
let context = CGContext( | |
data: nil, | |
width: width, | |
height: height, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
// According to article by John Sundell | |
// https://www.swiftbysundell.com/articles/caching-in-swift/ | |
final class Cache<Key: Hashable, Value> { | |
private let wrapped = NSCache<WrappedKey, Entry>() | |
private let dateProvider: () -> Date | |
private let entryLifetime: TimeInterval | |
private let keyTracker = KeyTracker() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 解决视图右滑与系统右滑返回上级页面手势冲突 | |
protocol PopGestureConflictProtocol { | |
} | |
extension UIViewController: PopGestureConflictProtocol {} | |
extension PopGestureConflictProtocol where Self: UIViewController { | |
/// 处理scrollView和系统右滑返回手势的冲突 | |
/// | |
/// 虽然self可能是某个UIViewController的子视图,但是不用管它。直接处理navigationController的手势冲突就行了 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public extension UIImage { | |
/// 圆角图片 | |
func roundCornerImage(withCornerRadius cornerRadius: CGFloat) -> UIImage? { | |
// 开关图形上下文 | |
UIGraphicsBeginImageContext(size) | |
defer { UIGraphicsEndImageContext() } | |
// 获取图形上下文 | |
let context = UIGraphicsGetCurrentContext() | |
// 带圆角的长方形贝塞尔曲线 | |
let roundedRect = CGRect(x: 0, y: 0, width: size.width, height: size.height) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Sequence { | |
func map<T>(_ keyPath: KeyPath<Element, T>) -> [T] { | |
return map { $0[keyPath: keyPath] } | |
} | |
} | |
extension Sequence { | |
func sorted<T: Comparable>(by keyPath: KeyPath<Element, T>) -> [Element] { | |
return sorted { a, b in | |
return a[keyPath: keyPath] < b[keyPath: keyPath] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alias cow=fortune | cowsay | lolcat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum PasswordRule { | |
enum CharacterClass { | |
case upper, lower, digits, special, asciiPrintable, unicode | |
case custom(Set<Character>) | |
} | |
case required(CharacterClass) | |
case allowed(CharacterClass) | |
case maxConsecutive(UInt) | |
case minLength(UInt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 将Lodash的方法注入到Vue的filter中 */ | |
_.each(_, (v, k) => { | |
if (_.isFunction(v)) { | |
Vue.filter('_' + k, (input, args) => { | |
return v.apply(_, _.flatten([[input], args])) | |
}) | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Collection { | |
func eachConsecutive(_ size: Int) -> [[Element]] { | |
let droppedIndices = indices.dropFirst(size - 1) | |
return zip(indices, droppedIndices) | |
.map { | |
return Array(self[$0...$1]) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Array { | |
func eachSlice(_ step: Int) -> [[Element]] { | |
return stride(from: 0, through: count, by: step) | |
.lazy | |
.map { from in | |
let to = Swift.min(from + step, count) | |
return (from..<to) | |
.lazy | |
.map { [self[$0]] } | |
.reduce([], +) |
NewerOlder