Skip to content

Instantly share code, notes, and snippets.

View heestand-xyz's full-sized avatar

Anton Heestand heestand-xyz

View GitHub Profile
@doozMen
doozMen / LocalNetworkAuthorization.swift
Created February 25, 2022 15:44
Uses bonjour networking to relialby check if user has granted local network access with async await as of iOS14
import Foundation
import Network
/// Uses bonjour networking to relialby check if user has granted local network access
/// How to use:
/// Add LocalNetworkAuthorization class to your project
/// Open .plist file and add "_bonjour._tcp", "_lnp._tcp.", as a values under "Bonjour services"
/// Call requestAuthorization() to trigger the prompt or get the authorization status if it already been approved/denied
/// about the author: https://stackoverflow.com/a/67758105/705761
@nicklockwood
nicklockwood / CodableVersioning.swift
Last active January 29, 2024 11:31
Example demonstrating how to use versioning for Codable structs
// This gist demonstrates how you can implement versioning for a Codable struct to support loading
// old serialized data after changing the structure. Notable features of this solution:
//
// * No need to make new properties optional, or to perform post-processing on the struct after
// loading in ordeer to populate missing values
// * No need to change the call site - from the outside this struct behaves just the same
// as if we had implemented codable directly in the normal way.
// * Versioning can be applied individually to parents or leaves in a larger tree of
// structs without affecting the other elements
// * This approach will work even if the original struct was not designed with versioning in mind
@Jswizzy
Jswizzy / InstallingSwift.md
Last active October 28, 2024 20:04
Swift Install Instruction 20.04

Installing Swift on Ubuntu 20.04

1. Install Depencies

“clang”[ˈklæŋ] is the compiler based on LLVM for C, C++, Objective-C, and Objective-C++. clang is needed to install in order to Swift. Run the following code in Ubuntu terminal.

Before installing swift “libpython2.7” and “libpython2.7-dev” are needed to get Swift running. Run the following code.

@WorldDownTown
WorldDownTown / KanaConverter.swift
Created December 18, 2019 04:51
Kanji to hiragana or katakana
import Foundation
private extension CFStringTokenizer {
var hiragana: String { string(to: kCFStringTransformLatinHiragana) }
var katakana: String { string(to: kCFStringTransformLatinKatakana) }
private func string(to transform: CFString) -> String {
var output: String = ""
while !CFStringTokenizerAdvanceToNextToken(self).isEmpty {
output.append(letter(to: transform))
@adamgraham
adamgraham / UIColor+LinearRGB.swift
Last active November 1, 2021 19:24
An extension of the iOS class UIColor to provide conversion to and from linear RGB colors.
/// An extension to provide conversion to and from linear RGB colors.
extension UIColor {
/// A set of non-gamma corrected linear RGB values, in the range [0, 1].
typealias LinearRGB = (r: CGFloat, g: CGFloat, b: CGFloat)
/// The inverse companded sRGB components to get non-gamma corrected linear values,
/// in the range [0, 1].
var linearRGB: LinearRGB {
var (r, g, b) = (CGFloat(), CGFloat(), CGFloat())
@dmsl1805
dmsl1805 / SnakeCase.swift
Last active September 25, 2024 23:14 — forked from ivanbruel/SnakeCase.swift
Camel case to snake case in Swift
extension String {
func snakeCased() -> String? {
let pattern = "([a-z0-9])([A-Z])"
let regex = try? NSRegularExpression(pattern: pattern, options: [])
let range = NSRange(location: 0, length: count)
return regex?.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: "$1_$2").lowercased()
}
}
@wakita
wakita / Makefile
Last active July 21, 2023 08:53
Metal compute shader example
SDK = xcrun -sdk macosx
all: compute.metallib compute
compute.metallib: Compute.metal
# Metal intermediate representation (.air)
$(SDK) metal -c -Wall -Wextra -std=osx-metal2.0 -o /tmp/Compute.air $^
# Metal library (.metallib)
$(SDK) metallib -o $@ /tmp/Compute.air
@yusuke024
yusuke024 / ViewController.swift
Created November 16, 2018 03:15
Recording video with AVAssetWriter
import UIKit
import AVFoundation
class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
switch AVCaptureDevice.authorizationStatus(for: .video) {
case .notDetermined:
@matthewreagan
matthewreagan / Perlin
Last active March 30, 2024 16:18
Perlin noise generation in Swift
// Perlin.swift
// Created by Matthew Reagan on 8/7/18.
//
// Quick implementation of the the classic Perlin noise
// generation function, useful for creating organic textures
// or terrain. Perlin noise can also be easily generated with
// Apple's GameplayKit framework, this code is mostly for
// experimentation purposes. (Disclaimer: This code is not
// optimized, nor particularly elegant, but it can be used as
// a jumping off point for writing custom noise functions.)
@michaelevensen
michaelevensen / Date+StartOfWeekEndOfWeek.swift
Created January 26, 2018 10:12
Start and end of week with Date.
extension Date {
var startOfWeek: Date? {
let gregorian = Calendar(identifier: .gregorian)
guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else { return nil }
return gregorian.date(byAdding: .day, value: 1, to: sunday)
}
var endOfWeek: Date? {
let gregorian = Calendar(identifier: .gregorian)
guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else { return nil }