Skip to content

Instantly share code, notes, and snippets.

View haikieu's full-sized avatar

Hai K haikieu

View GitHub Profile
@haikieu
haikieu / SwiftUITimer.swift
Last active February 26, 2024 11:36
Best solution to enhance timer in SwiftUI
//
// SwiftUITimer.swift
//
//
// Created by Hai Kieu on 7/13/23.
// https://medium.com/@programmingpassion/best-solution-to-enhance-timer-in-swiftui-501096572139
import SwiftUI
import Combine
@haikieu
haikieu / imageToPixelBuffer.swift
Created May 31, 2020 08:20 — forked from omarojo/imageToPixelBuffer.swift
Image To CVPixelBuffer in Swift
var thePixelBuffer : CVPixelBuffer?
let testImage : UIImage = UIImage.init(named: "twdEnds.png")!
self.thePixelBuffer = self.pixelBufferFromImage(image: testImage)
func pixelBufferFromImage(image: UIImage) -> CVPixelBuffer {
@haikieu
haikieu / SimpleRegExSearch.swift
Last active May 20, 2020 20:52
Simple Regular Expression Search Function
func search(pattern: String, input text: String) -> [NSTextCheckingResult] {
guard let regex = try? NSRegularExpression.init(pattern: pattern, options: .caseInsensitive) else {
return []
}
return regex.matches(in: text, options: [], range: NSRange.init(location: 0, length: text.count))
}
func searchStrings(pattern: String, input text: String) -> [String] {
@haikieu
haikieu / resize-vi.swift
Last active March 28, 2024 04:29 — forked from darcwader/resize-vi.swift
Resize Image using vImage (Updated for Swift 5)
extension UIImage {
func resize(size: CGSize) -> UIImage? {
guard let cgImage = self.cgImage else { return nil}
var format = vImage_CGImageFormat(bitsPerComponent: 8, bitsPerPixel: 32, colorSpace: nil,
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.first.rawValue),
version: 0, decode: nil, renderingIntent: CGColorRenderingIntent.defaultIntent)
var sourceBuffer = vImage_Buffer()
@haikieu
haikieu / CircularReferenceWithClosure.swift
Created May 24, 2019 21:13
Circular reference with closure
import UIKit
class Student {
var name: String
var age: Int
init(_ name: String, age: Int) {
print("Hello student \(name)")
@haikieu
haikieu / KeepWatchAppHidden.sh
Last active March 1, 2023 07:05
This script is help to attach the watch app into host app for Debug configuration only
#/bin/sh
#The script is used to embed Watch App into the mobile app.
#Prepare some directory paths
WATCH_APP_NAME="WatchApp.app" # <--- put your watch app name here
WATCH_APP_DIR="${TARGET_BUILD_DIR}/${WATCH_APP_NAME}"
APP_BUILD_DIR="${CODESIGNING_FOLDER_PATH}"
DESTINATION_DIR="${APP_BUILD_DIR}/Watch"
#By checking this condition, the script is valid for Debug configuration only
@haikieu
haikieu / function-with-golden-path.swift
Created August 15, 2018 07:47
function with golden path
func authenticateUser(_ username: String?, with password: String?) -> Bool {
guard let username = username, let password = password else {
//Bad input, do something and return
...
return false
}
//Golden path, everything goes well so far
let found = lookupDatabase(username, password)
return found
@haikieu
haikieu / function-without-golden-path.swift
Last active March 1, 2023 07:06
function without golden path
func authenticateUser(_ username: String?, with password: String?) -> Bool {
if let username = username, let password = password {
//Validate usernane and password
...
return true //Assuming, the username and password are valid
} else {
//Base input, do something
return false
}
}
@haikieu
haikieu / unwrap-optional-type-by-guard.swift
Created August 15, 2018 07:10
unwrap optional type by guard
var optionalString : String?
guard let theString = optionalString else {
return
}
//do something with theString
@haikieu
haikieu / UIImageFixedOrientationExtension.swift
Last active March 27, 2018 22:35 — forked from schickling/UIImageFixedOrientationExtension.swift
Extension to fix orientation of an UIImage (Sets orientation to portrait)
extension UIImage {
///UIImage instance somtimes can be at any various orientation, so this method basically converts it back to default orientation.
///This is picked from the original source -> https://gist.github.com/schickling/b5d86cb070130f80bb40
func fixedOrientation() -> UIImage? {
guard imageOrientation != UIImageOrientation.up else {
//This is correct orientation, don't need to do anything
return self.copy() as? UIImage
}