Skip to content

Instantly share code, notes, and snippets.

View AnYuan's full-sized avatar
🎯
Focusing

Anyuan AnYuan

🎯
Focusing
View GitHub Profile
@AnYuan
AnYuan / Scanner.swift
Created April 4, 2019 08:34
Scanner extension
// Scanner
extension Scanner {
public struct Error: Swift.Error {
let location: Int
var localizedDescription: String {
return "scan failure at location: \(location)"
}
}
@AnYuan
AnYuan / ExpressibleByStringInterpolation.swift
Created April 4, 2019 07:40
ExpressibleByStringInterpolation example
enum Style {
case none
case bold
case italic
case boldItalic
case sansSerif(bold: Bool, italic: Bool)
case script(bold: Bool)
case fraktur(bold: Bool)
case doubleStruck
case monospace
@AnYuan
AnYuan / ExpressibleByStringLiteral.swift
Last active April 4, 2019 07:30
Swift String ExpressibleByStringLiteral Protocol
public enum BookingCode: String {
case firstClass = "F"
case businessClass = "J"
case premiumEconomy = "W"
case economy = "Y"
}
extension BookingCode: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(rawValue: value)!
@AnYuan
AnYuan / BehaviorRelay+AcceptAppending.swift
Created December 22, 2018 12:44
RxSwift BehaviorRelay extension
extension BehaviorRelay where Element: RangeReplaceableCollection {
func acceptAppending(_ element: Element.Element) {
accept(value + [element])
}
}
@AnYuan
AnYuan / measure.swift
Created June 15, 2018 03:16
When we want to quickly test the performance of some code, we paste the following function into our project
@discardableResult
func measure<A>(name: String = "", _ block: () -> A) -> A {
let startTime = CACurrentMediaTime()
let result = block()
let timeElapsed = CACurrentMediaTime() - startTime
print("Time: \(name) - \(timeElapsed)")
return result
}
let result = measure { (0..<1_000_000).reduce(0, +) }
@AnYuan
AnYuan / HowToTime.m
Created June 12, 2018 09:31
How to time on mac
#import <mach/mach_time.h>
uint64_t start = mach_absolute_time();
// do stuff to be timed
uint64_t end = mach_absolute_time();
uint64_t elapsed = end - start;
mach_timebase_info_data_t info;
@AnYuan
AnYuan / Calculate
Created April 10, 2018 10:43
TextKit calculate text height
+ (CGSize)rectForAttributedString:(NSAttributedString *)string
size:(CGSize)theSize {
if (!string || CGSizeEqualToSize(theSize, CGSizeZero)) {
return CGSizeZero;
}
// setup TextKit stack
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:theSize];
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:string];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
@AnYuan
AnYuan / draw.swift
Created December 19, 2017 15:02
Draw a dot
let xCoord = 10
let yCoord = 20
let radius = 8
let dotPath = UIBezierPath(ovalInRect: CGRectMake(xCoord, yCoord, radius, radius))
let layer = CAShapeLayer()
layer.path = dotPath.CGPath
layer.strokeColor = UIColor.blueColor().CGColor
@AnYuan
AnYuan / AYStickyHeaderCollectionViewLayout.m
Last active December 1, 2017 06:40
Sticky collection view layout
//
// AYListStickyHeaderCollectionViewLayout.m
//
//
// Created by anyuan on 30/11/2017.
//
#import "AYListStickyHeaderCollectionViewLayout.h"
@implementation ListStickyHeaderCollectionViewLayout
@AnYuan
AnYuan / Result.swift
Created August 5, 2016 08:19
swift Result error handling
import Foundation
enum Result<T, E: Error> {
case success(T)
case failure(E)
}
extension Result {