Skip to content

Instantly share code, notes, and snippets.

View codelynx's full-sized avatar

Kaz Yoshikawa codelynx

View GitHub Profile
// AWS-SDK-Swift
//
// When you need to find if error is caused by resource not found,rather than other type of errors
// such as network error or access permissions related error. This is how to find, if this error
// is cause of resource not found.
//
// Note:
// I don't want to spend time for this next time, so I paste code snippet here for my future reference
func processHeadRequest(s3client: S3Client, bucket: String, key: String) async throws {
@codelynx
codelynx / Data+hexadecimal.swift
Last active May 8, 2023 20:18
convert hexadecimal string to Data and/or the other way around
//
// Data+Hexadecimal.swift
//
// The MIT License (MIT)
//
// Copyright (c) 2023 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 / UIImage+color.swift
Created March 2, 2023 05:40
UIImage extension to create 1 pixel color image
import UIKit
extension UIImage {
convenience init(color: UIColor) {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 1, height: 1))
let image = renderer.image { context in
context.cgContext.setFillColor(UIColor.blue.cgColor)
context.cgContext.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
}
guard let cgImage = image.cgImage else { fatalError() }
@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 / 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 / Array+RunLength.swift
Created July 17, 2022 19:53
encoding and decoding equatable elements from Array.
//
// Array+RunLength.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 / Error.swift
Created July 18, 2022 19:22
General purpose to make a concrete Error instance in Swift
//
// ZError.swift
// ZKit
//
// Created by Kaz Yoshikawa on 7/18/22.
//
// Usage:
// General usage for throwing Error in Swift language.
//
// Example:
@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 / simd+ext.swift
Last active November 17, 2021 18:56
swift: float4x4 extension to scale, rotate, translate 4x4 matrix
//
// simd+ext.swift
//
// Created by Kaz Yoshikawa on 11/6/15.
//
//
import Foundation
import simd
import GLKit
@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
*/