This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import Combine | |
class ViewModel { | |
var updateLabel: ((String?) -> Void)? | |
var textString: String? { | |
didSet { | |
updateLabel?(textString) | |
} | |
} |