Skip to content

Instantly share code, notes, and snippets.

View kristofk's full-sized avatar

Kristof Kocsis kristofk

View GitHub Profile
@kristofk
kristofk / UIImage+PixelColor.swift
Created June 17, 2017 13:59 — forked from marchinram/UIImage+PixelColor.swift
iOS Swift UIImage subscript extension to get pixel color
import UIKit
extension UIImage {
subscript (x: Int, y: Int) -> UIColor? {
if x < 0 || x > Int(size.width) || y < 0 || y > Int(size.height) {
return nil
}
@kristofk
kristofk / auto-executingClosure.swift
Last active March 21, 2018 17:38
MEDIUM: Lazy Stored Property — Swift DevTip
class SomeClass {
lazy var fibonacciNumbers: [Int] = {
var fibonacciNumbers = [0, 1, 1]
for index in 2...8 {
let currentNum = fibonacciNumbers[index]
let prevNum = fibonacciNumbers[index - 1]
let newNum = currentNum + prevNum
fibonacciNumbers.append(newNum)
}
@kristofk
kristofk / git_create_orphan.sh
Last active April 22, 2018 12:07 — forked from seanbuscay/git_create_orphan.sh
Create an orphan branch in a repo
# Prepare branch
cd repository
git checkout --orphan <orphan-name>
git rm -rf .
rm '.gitignore'
# Create a commit (this can be empty) and push
git commit --allow-empty -m “root commit”
git push origin orphan_name
@kristofk
kristofk / testPrint.swift
Created May 29, 2018 09:36
Prints out the name of the function and the file it is located in! Good to track the flow of the app.
func testPrint(function: String, path: String) {
let pathElements = path.split(separator: "/")
print("TEST: \(function) -> \(pathElements.last!)")
}
// Callsite: testPrint(function: #function, path: #file)
// Filter in the console: test
@kristofk
kristofk / UIDevice+Extension.swift
Created July 9, 2018 14:23
An extension for UIDevice so that the device model NAME and IDENTIFIER can be determined.
extension UIDevice {
static let modelName: String = {
var systemInfo = utsname()
uname(&systemInfo)
let machineMirror = Mirror(reflecting: systemInfo.machine)
let identifier = machineMirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8, value != 0 else { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
}
@kristofk
kristofk / DeviceMeta.swift
Last active August 23, 2018 10:15
Get meta data like iPhone and iOS version of device
struct DeviceMeta {
/// Version of the application.
static let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
/// Name of the current operation system. (ios/macos/...)
static let osType = UIDevice.current.systemName
/// Version of the operating system. (e.g. 11.4)
static let osVersion = UIDevice.current.systemVersion
@kristofk
kristofk / swiftCode.html
Last active December 2, 2022 13:54
Html code that can be used to put highlighted Swift code into a Custom HTML Gutenberg block in Wordpress
<style>pre { background-color: #1a1a1a; border: 0px solid #ccc; padding: 0px; margin-bottom: 0px; } pre code { display: block; color: #a9bcbc; line-height: 1.4em; font-size: 0.95em; white-space: pre; } pre code .keyword { color: #e73289; } pre code .type { color: #8281ca; } pre code .call { color: #348fe5; } pre code .property { color: #21ab9d; } pre code .number { color: #db6f57; } pre code .string { color: #fa641e; } pre code .comment { color: #6b8a94; } pre code .dotAccess { color: #92b300; } .box { padding: 20px; margin: 0 auto; display: block; border-radius: 8px; background-color: #1a1a1a; border: 0px solid #ccc; } </style>
<!--Generate code at: https://splash.rambo.codes/ and then COPY HTML-->
<section class="box"><pre><code><!--Paste the generated code after this--><!--Paste the generated code before this--></code></pre></section>
@kristofk
kristofk / switchMatrixVectors.swift
Last active October 15, 2018 13:33
Switch the rows and colums in Swift for vectors ( [[T]] ).
func determineLongestItem<T>(in matrix: [[T]]) -> Int {
var maxLength = 0
for item in matrix {
if item.count > maxLength {
maxLength = item.count
}
}
return maxLength
}
@kristofk
kristofk / xcode_build_fix.sh
Last active July 26, 2019 06:24
Fix to the following error message in terminal: terminated(72): xcrun --sdk macosx --find xctest output:
sudo xcode-select --reset
swift build
@kristofk
kristofk / fixgitignore.sh
Last active July 25, 2019 00:47
Apply gitignore to already existing project.
#!/bin/sh
git rm -r --cached .
git add .
git commit -m "Fixed Gitignore"