Skip to content

Instantly share code, notes, and snippets.

View codelynx's full-sized avatar

Kaz Yoshikawa codelynx

View GitHub Profile
@codelynx
codelynx / UnsafePointer Converter.swift
Created September 13, 2020 14:57
[swift] Unsafe Pointer conversion
View UnsafePointer Converter.swift
//
// Swift UnsafePointer converter
// Kaz Yoshikawa
//
// These code demonstrates how to convert swift pointers to the other forms of pointers.
//
// Source:
// Swift の Array やら ArraySlice やらポインタの変換まとめ
// https://qiita.com/Satachito/items/4c39c9b06304e4d86660
//
@codelynx
codelynx / Runtime.swift
Last active July 21, 2023 18:07
[Swift] To retrieve classes at runtime which conforms to a protocol or to retrieve subclasses of a given class
View Runtime.swift
//
// Runtime.swift
// Swift Runtime [Swift 4]
//
// The MIT License (MIT)
//
// Copyright (c) 2016 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 / FileHandle+Z.swift
Last active June 19, 2023 00:32
enumerate lines from FileHandle (NSFileHandle) in swift3 – good for enumerating huge text file line by line
View FileHandle+Z.swift
//
// FileHandle+Z.swift
// ZKit
//
// The MIT License (MIT)
//
// Copyright (c) 2016 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
View Hanlde not found error for AWS HEAD request.swift
// 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
View Data+hexadecimal.swift
//
// 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
View UIImage+color.swift
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
View UIKit+swizzling.swift
//
// 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 / MTLTexture+Z.swift
Created December 28, 2016 14:58
Piece of Utility code to make CGImage from MTLTexture under (BGRA8Unorm)
View MTLTexture+Z.swift
//
// MTLTexture+Z.swift
// ZKit
//
// The MIT License (MIT)
//
// Copyright (c) 2016 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 / Array+slice.swift
Last active July 18, 2022 19:23
Array extension to slice an array with given number
View Array+slice.swift
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.
View Array+RunLength.swift
//
// 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