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
// use in a custom view | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
setupShadow() | |
} | |
private var shadowLayer: CAShapeLayer! // layer for shadow back | |
private func setupShadow() { | |
guard columnStackView.bounds.size != .zero else { return } |
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
precedencegroup ForwardPipe { | |
associativity: left | |
higherThan: LogicalConjunctionPrecedence | |
} | |
infix operator |> : ForwardPipe | |
///Swift implementation of the forward pipe operator from F# | |
/// Used for better readibility when piping results of one function to the next ones. |
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
// https://stackoverflow.com/a/24035942 | |
func foo(args:AnyObject...) { | |
for arg: AnyObject in args { | |
println(arg) | |
} | |
} | |
foo(5, "bar", NSView()) |
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 where Element: Comparable { | |
func binarySearch(_ target: Element) -> Int? { | |
var lower = 0 | |
var upper = count | |
while lower < upper { | |
let mid = lower + (upper - lower) / 2 | |
if self[mid] == target { | |
return mid | |
} else if self[mid] < target { | |
lower = mid + 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 App { | |
static var infoDictionary: [String: Any]? { | |
return Bundle.main.infoDictionary | |
} | |
static var appVersion: String { | |
return infoDictionary?["CFBundleShortVersionString"] as? String ?? "" | |
} | |
static var appBuildVersion: String { |
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 UIViewController { | |
/// 获取最顶层的视图控制器 | |
func topMostViewController() -> UIViewController? { | |
if self.presentedViewController == nil { | |
return self | |
} | |
if let navigation = self.presentedViewController as? UINavigationController { | |
return navigation.visibleViewController?.topMostViewController() | |
} |
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 CNContactStore { | |
/// 是否可以访问通讯录 | |
/// - true 表示可访问,false 表示未知或不可访问 | |
class var canAssessContact: Bool { | |
return CNContactStore.authorizationStatus(for: .contacts) != .denied | |
} | |
class var maybeCanAccessContact: Bool { | |
return CNContactStore.authorizationStatus(for: .contacts) == .notDetermined |
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 Data { | |
/// 将二进制数据转换为 Int8 类型数值 | |
var uint8: UInt8 { | |
var result: UInt8 = 0 | |
self.copyBytes(to: &result, count: MemoryLayout<UInt8>.size) | |
return result | |
} | |
/// 将二进制数据转换为 Bool 类型数值 | |
var bool: Bool { |
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 Bool { | |
/// 转换为 16 进制数据 | |
var bytes: [UInt8] { | |
return self ? [UInt8(1)] : [UInt8(0)] | |
} | |
var binaryData: Data { | |
return Data(bytes) | |
} | |
} |
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 String { | |
/// 是否为合法邮箱地址 | |
var isValidEmail: Bool { | |
// https://stackoverflow.com/a/41782027/5211544 | |
let firstPart = "[A-Z0-9a-z]([A-Z0-9a-z._%+-]{0,30}[A-Z0-9a-z])?" | |
let serverPart = "([A-Z0-9a-z]([A-Z0-9a-z-]{0,30}[A-Z0-9a-z])?\\.){1,5}" | |
let regex = "\(firstPart)@\(serverPart)[A-Za-z]{2,8}" | |
let predicate = NSPredicate(format: "SELF MATCHES %@", regex) | |
return predicate.evaluate(with: self) |
NewerOlder