Skip to content

Instantly share code, notes, and snippets.

View monsoir's full-sized avatar

Monsoir monsoir

View GitHub Profile
// 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 }
@monsoir
monsoir / PipeOperator.swift
Created August 31, 2020 07:42
Pipe operator in Swift
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.
@monsoir
monsoir / variadic-swift-function.swift
Last active June 11, 2020 07:56
Swift 参数数量可变函数
// https://stackoverflow.com/a/24035942
func foo(args:AnyObject...) {
for arg: AnyObject in args {
println(arg)
}
}
foo(5, "bar", NSView())
@monsoir
monsoir / binary-search.swift
Last active April 18, 2020 11:02
Algorithms in Swift
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
@monsoir
monsoir / AppInfo.swift
Created February 3, 2020 06:54
Read App configuration info
extension App {
static var infoDictionary: [String: Any]? {
return Bundle.main.infoDictionary
}
static var appVersion: String {
return infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
}
static var appBuildVersion: String {
@monsoir
monsoir / UIViewController+Hierachy.swift
Created January 4, 2020 08:49
UIViewController extensions
extension UIViewController {
/// 获取最顶层的视图控制器
func topMostViewController() -> UIViewController? {
if self.presentedViewController == nil {
return self
}
if let navigation = self.presentedViewController as? UINavigationController {
return navigation.visibleViewController?.topMostViewController()
}
@monsoir
monsoir / CNContactStore+Permission.swift
Created December 24, 2019 05:24
Contacts Extensions
extension CNContactStore {
/// 是否可以访问通讯录
/// - true 表示可访问,false 表示未知或不可访问
class var canAssessContact: Bool {
return CNContactStore.authorizationStatus(for: .contacts) != .denied
}
class var maybeCanAccessContact: Bool {
return CNContactStore.authorizationStatus(for: .contacts) == .notDetermined
@monsoir
monsoir / Data+Numeric.swift
Last active December 25, 2019 05:05
Data Extensions
extension Data {
/// 将二进制数据转换为 Int8 类型数值
var uint8: UInt8 {
var result: UInt8 = 0
self.copyBytes(to: &result, count: MemoryLayout<UInt8>.size)
return result
}
/// 将二进制数据转换为 Bool 类型数值
var bool: Bool {
@monsoir
monsoir / Bool+Extensions.swift
Created December 24, 2019 05:15
Bool Extensions
extension Bool {
/// 转换为 16 进制数据
var bytes: [UInt8] {
return self ? [UInt8(1)] : [UInt8(0)]
}
var binaryData: Data {
return Data(bytes)
}
}
@monsoir
monsoir / String+Regex.swift
Last active December 24, 2019 05:21
String regex
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)