Skip to content

Instantly share code, notes, and snippets.

View codelynx's full-sized avatar

Kaz Yoshikawa codelynx

View GitHub Profile
@codelynx
codelynx / Array+slice.swift
Last active July 18, 2022 19:23
Array extension to slice an array with given number
import Foundation
public extension Array {
func slice(count: Int) -> [some Collection] {
let n = self.count / count // quotient
let i = n * count // index
let r = self.count % count // remainder
let slices = (0..<n).map { $0 * count }.map { self[$0 ..< $0 + count] }
return (r > 0) ? slices + [self[i..<i + r]] : slices
}
@codelynx
codelynx / CGFloat+LosslessStringConvertible.swift
Created April 4, 2022 04:07
Convert between CGFloat and String
// Note:
// When you found that you cannot cast/convert between CGFloat and String, you may add following piece of code
// to make your life a bit easier.
//
// if let number = CGFloat("1234.5") {
// }
// let string = String(CGFloat.pi)
//
extension CGFloat: LosslessStringConvertible {
@codelynx
codelynx / archive-unarchive-3.swift
Created September 4, 2021 13:12
This code demonstrate archive and unarchive objects derived from a certain class and its descendants using Codable and Runtime Utility
/*
This sample code shows the problem of archive and unarchive abstruct objects. Please see `TODO` keyword
and make this Contents class to save and load shape objects.
CASE: All target objects are based on NSObject with NSCoding to archive and unarchive Shape desendant class objects
file: archive-unarchive-1.swift
https://gist.github.com/codelynx/428b27b3cfd58b8c7382346f1a4bc415
*/
@codelynx
codelynx / archive-unarchive-2.swift
Last active September 4, 2021 14:18
This code demonstrate archive and unarchive objects derived from a certain class and its descendants using NSObject and NSCoding
/*
This sample code shows the problem of archive and unarchive abstruct objects. Please see `TODO` keyword
and make this Contents class to save and load shape objects.
CASE: All target objects are based on NSObject with NSCoding to archive and unarchive Shape desendant class objects
file: archive-unarchive-1.swift
https://gist.github.com/codelynx/428b27b3cfd58b8c7382346f1a4bc415
*/
@codelynx
codelynx / archive-unarchive-1.swift
Last active September 4, 2021 07:48
save/restore abstract objects
/*
This sample code shows the problem of archive and unarchive abstruct objects. Please see `TODO` keyword
and make this Contents class to save and load shape objects.
*/
import Foundation
import CoreGraphics
protocol Shape {
}
@codelynx
codelynx / casteljau.swift
Last active August 23, 2021 17:57
casteljau function where t = 0...1
func casteljau<T: FloatingPoint>(_ v0: T, _ v1: T, _ t: T) -> T {
return v0 + (v1 - v0) * t
}
func casteljau<T: FloatingPoint>(_ v0: T, _ v1: T, _ v2: T, _ t: T) -> T {
let q0 = v0 + (v1 - v0) * t
let q1 = v1 + (v2 - v1) * t
return q0 + (q1 - q0) * t
}
@codelynx
codelynx / UIKit+swizzling.swift
Created March 17, 2021 09:10
Example of swizzling UIKit classes
//
// UIKit+swizzling.swift
//
// The MIT License (MIT)
//
// Copyright (c) 2022 Electricwoods LLC, Kaz Yoshikawa.
//
// 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
@codelynx
codelynx / Thread+Z.swift
Created March 15, 2021 12:26
Regardless of thread, execute closure on main thread
//
// Thread+Z.swift
// ZKit
//
// The MIT License (MIT)
//
// Copyright (c) 2020 Electricwoods LLC, Kaz Yoshikawa.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@codelynx
codelynx / XResponder+Z.swift
Created March 13, 2021 14:25
make UIResponder/NSResponder conforms to Sequence protocol
//
// UIResponder+Z.swift
// ZKit
//
// The MIT License (MIT)
//
// Copyright (c) 2021 Electricwoods LLC, Kaz Yoshikawa.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@codelynx
codelynx / UIGestureRecognizer+Z.swift
Created March 12, 2021 01:53
UIGestureRecognizer extension to keep arbitrary values associated with any gesture recognizers
//
// UIGestureRecognizer+Z.swift
// ZKit
//
// The MIT License (MIT)
//
// Copyright (c) 2021 Electricwoods LLC, Kaz Yoshikawa.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal