Skip to content

Instantly share code, notes, and snippets.

@tgnivekucn
tgnivekucn / getCRC16ForModbus.swift
Created September 27, 2022 10:19
Get CRC 16 For Modbus in Swift
// Ref 1: https://www.jianshu.com/p/b7fbe502d909
// Ref 2: https://crccalc.com/
// CRC 16 MODBUS
extension String {
func asHexUInt16Array() -> [UInt16] {
var startIndex = self.startIndex
return stride(from: 0, to: count, by: 2).compactMap { _ in
let endIndex = index(startIndex, offsetBy: 2, limitedBy: self.endIndex) ?? self.endIndex
defer { startIndex = endIndex }
return UInt16(self[startIndex..<endIndex], radix: 16)
@tgnivekucn
tgnivekucn / GetMemoryAddress.swift
Created September 1, 2022 07:13
Get memory address of the object(p.s. class or struct)
import Foundation
// Ref: https://stackoverflow.com/questions/24058906/printing-a-variable-memory-address-in-swift
// Author: nyg (profile: https://stackoverflow.com/users/5536516/nyg)
struct MemoryAddress<T>: CustomStringConvertible {
let intValue: Int
@tgnivekucn
tgnivekucn / MemoryAddress+Testing.swift
Created September 1, 2022 05:56
Get memory address of object in Swift
// Ref: https://stackoverflow.com/questions/24058906/printing-a-variable-memory-address-in-swift
// Author nyg (profile: https://stackoverflow.com/users/5536516/nyg)
struct MemoryAddress<T>: CustomStringConvertible {
let intValue: Int
var description: String {
let length = 2 + 2 * MemoryLayout<UnsafeRawPointer>.size
return String(format: "%0\(length)p", intValue)
@tgnivekucn
tgnivekucn / TestTimezoneWithTimestamp.swift
Created August 30, 2022 03:37
Test timezone with timestamp
let date2 = toGmtDateTest(year: 2022, month: 8, date: 30, hour: 11, minute: 19, second: 00, secondsFromGMT: 28800)
print(date2?.timeIntervalSince1970)
print(date2)
let date3 = toGmtDateTest2(year: 2022, month: 8, date: 30, hour: 11, minute: 19, second: 00)
print(date3?.timeIntervalSince1970)
print(date3)
func validatePhoneNumber(phoneNumber: String) -> Bool {
if let regex = try? NSRegularExpression(pattern: "09\\d{2}-\\d{3}-\\d{3}") {
let range = NSRange(location: 0, length: phoneNumber.utf16.count)
let result = regex.firstMatch(in: phoneNumber, range: range)
if result != nil {
return true
}
}
return false
}
@tgnivekucn
tgnivekucn / gist:58b48eae9d1fbaf7e83daf08fcf52915
Created August 2, 2022 03:21
Custom color identifier rule
Color identifier naming rule:
Regular expression:
(color)\.((common)|([A-Za-z])+(Page)){1}(\.([A-Za-z])+)*(\.([A-Za-z])+(Color)s?)$
Example:
1. Color category
2. UI page name or plist file name
3. Subview name(ex: customCell) (p.s. this field might be zero)
4. Unique color name (p.s. or unique color array name)
// Product and Concrete Product
protocol Adventurer {
func getType() -> String
}
class Archer: Adventurer {
func getType() -> String {
print("I'm an archer")
return String(describing: Archer.self)
}
// Product and Concrete Product
protocol Adventurer {
func getType() -> String
}
class Archer: Adventurer {
func getType() -> String {
print("I'm an archer")
return String(describing: Archer.self)
}
@tgnivekucn
tgnivekucn / Singleton.swift
Created July 17, 2022 11:09
Singleton pattern in Swift
// lazy Singleton: create the singleton object when needed
// Note: Global constants and variables are always computed lazily
// ref: https://docs.swift.org/swift-book/LanguageGuide/Properties.html#ID263
class SingletonGreed {
static let shared = SingletonGreed()
private init() {}
}
class SingletonThreadSafe {
private static var shared: SingletonThreadSafe?
@tgnivekucn
tgnivekucn / gist:e93bc51a4d651c6ef7b560e9c7644d21
Last active December 3, 2021 13:23
IOS combine framework sample
import UIKit
import Combine
class ViewModel {
var updateLabel: ((String?) -> Void)?
var textString: String? {
didSet {
updateLabel?(textString)
}
}