This file contains hidden or 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 sorted<T>(by kp: KeyPath<Element, T>) -> [Element] where T: Comparable { | |
self.sorted { | |
$0[keyPath: kp] < $1[keyPath: kp] | |
} | |
} | |
} |
This file contains hidden or 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 Either<Left, Right> { | |
case left(Left) | |
case right(Right) | |
} | |
extension Either: Decodable where Left: Decodable, Right: Decodable { | |
init(from decoder: Decoder) throws { | |
let container = try decoder.singleValueContainer() | |
do { |
This file contains hidden or 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 Countable { | |
var count: Int? {get} | |
} | |
struct OptionalCount: Countable { | |
var count: Int? = 0 | |
} | |
struct DeadlyCount: Countable { // Type 'DeadlyCount' does not conform to protocol 'Countable' | |
var count: Int! = 0 // 1. Candidate has non-matching type 'Int?' <---------- |
This file contains hidden or 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 P {} | |
struct A: P {} | |
struct B: P {} | |
struct C: P {} | |
var someP: P.Type = A.self | |
someP == A.self // true | |
someP == B.self // false | |
switch someP { |
This file contains hidden or 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 UILabel { | |
func hyphenate() { | |
let paragraphStyle = NSMutableParagraphStyle() | |
let attstr = NSMutableAttributedString(attributedString: self.attributedText!) | |
paragraphStyle.hyphenationFactor = 1.0 | |
attstr.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(0..<attstr.length)) | |
self.attributedText = attstr | |
} | |
} |
This file contains hidden or 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 UILabel { | |
/// Minimum width fitting text in alotted number of lines | |
var minimumWidth:CGFloat { | |
get { | |
guard intrinsicContentSize.width >= bounds.width || intrinsicContentSize.height >= ceil(font.lineHeight) * CGFloat(numberOfLines) | |
else { return intrinsicContentSize.width } | |
let rect = self.textRect(forBounds: CGRect.init(origin: .zero, size: CGSize(width: bounds.width, height: .infinity)), limitedToNumberOfLines: self.numberOfLines) | |
return calculateWidth(0 ..< rect.width, height: rect.height) | |
} |