Skip to content

Instantly share code, notes, and snippets.

View zhihuitang's full-sized avatar
😜

Zhihui Tang zhihuitang

😜
View GitHub Profile
@zhihuitang
zhihuitang / Python3 Virtualenv Setup.md
Created April 22, 2019 15:03 — forked from pandafulmanda/Python3 Virtualenv Setup.md
Setting up and using Python3 Virtualenv on Mac

Python3 Virtualenv Setup

Requirements
  • Python 3
  • Pip 3
$ brew install python3
@zhihuitang
zhihuitang / Identifier
Created April 10, 2019 07:46
https://www.swiftbysundell.com/posts/type-safe-identifiers-in-swift In "Identifying objects in Swift" we took a look at how objects can be identified using the built-in ObjectIdentifier type - and this week, we're going to construct similar identifi
struct Identifier: Hashable {
let string: String
}
extension Identifier: ExpressibleByStringLiteral {
init(stringLiteral value: String) {
string = value
}
}
class Observable<Value> {
private var value: Value
private var observations = [UUID : (Value) -> Void]()
init(value: Value) {
self.value = value
}
func update(with value: Value) {
self.value = value
import UIKit
import CoreGraphics
extension UIView {
func createRoundedRectPath(for rect: CGRect, radius: CGFloat) -> CGMutablePath {
let path = CGMutablePath()
let midTopPoint = CGPoint(x: rect.midX, y: rect.minY)
path.move(to: midTopPoint)
/// Copyright (c) 2019 Razeware LLC
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
extension Optional where Wrapped: Collection {
var isNilOrEmpty: Bool {
return self?.isEmpty ?? true
}
}
@zhihuitang
zhihuitang / CGRect extension
Last active February 29, 2020 10:20
CGRect centre, largestContainedSquare, smallestContainingSquare
import QuartzCore
public extension CGRect {
public init(centre: CGPoint, size: CGSize) {
self.init(origin: centre.applying(CGAffineTransform(translationX: size.width / -2, y: size.height / -2)), size: size)
}
public var centre: CGPoint {
return CGPoint(x: midX, y: midY)
}
public extension CGSize {
public func rescale(_ scale: CGFloat) -> CGSize {
return applying(CGAffineTransform(scaleX: scale, y: scale))
}
}
@zhihuitang
zhihuitang / Collection
Last active February 29, 2020 10:20
Returns the element at the specified index if it is within bounds, otherwise nil.
public extension Collection {
/// Returns the element at the specified index if it is within bounds, otherwise nil.
subscript (safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
}
@zhihuitang
zhihuitang / AsynchronousOperation.swift
Created February 14, 2018 13:58 — forked from calebd/AsynchronousOperation.swift
Concurrent NSOperation in Swift
import Foundation
/// An abstract class that makes building simple asynchronous operations easy.
/// Subclasses must implement `execute()` to perform any work and call
/// `finish()` when they are done. All `NSOperation` work will be handled
/// automatically.
open class AsynchronousOperation: Operation {
// MARK: - Properties