Skip to content

Instantly share code, notes, and snippets.

View trilliwon's full-sized avatar
🎯
Focusing

WonJo trilliwon

🎯
Focusing
View GitHub Profile
@trilliwon
trilliwon / swift-string-subscript.swift
Created September 23, 2018 04:59
Swift string subscript using Int value as index
extension String {
subscript(i: Int) -> Character? {
guard i < self.count else {
return nil
}
return self[self.index(self.startIndex, offsetBy: i)]
}
}
extension Array where Element: Hashable {
func counter() -> [Element: Int] {
return self.reduce(into: [:]) { counts, elem in counts[elem, default: 0] += 1 }
}
}
@trilliwon
trilliwon / latency.txt
Created September 6, 2018 09:09 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@trilliwon
trilliwon / haptic.swift
Created August 29, 2018 05:42
iOS haptic feedback code
// class UINotificationFeedbackGenerator
let notificationFeedback = UINotificationFeedbackGenerator()
notificationFeedback.notificationOccurred(.success) // .warning, .error
// class UIImpactFeedbackGenerator
let impactFeedback = UIImpactFeedbackGenerator(style: .light) // .medium, .heavy
impactFeedback.impactOccurred()
// class UISelectionFeedbackGenerator
let selectionFeedback = UISelectionFeedbackGenerator()
@trilliwon
trilliwon / uitableview-swipe-actions.swift
Last active September 13, 2021 14:14
uitableview-swipe-actions
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let share = UIContextualAction(style: .normal, title: "Share") { action, view, completion in
completion(true)
}
let delete = UIContextualAction(style: .destructive, title: "Delete") { [weak self] action, view, completion in
self?.langs.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
import UIKit
class FadeInOutUnwind: UIStoryboardSegue {
override func perform() {
let sourceView: UIView = self.source.view as UIView!
let destinationView: UIView = self.destination.view as UIView!
sourceView.alpha = 1.0
import UIKit
class FadeInOut: UIStoryboardSegue {
override func perform() {
let sourceView: UIView = self.source.view as UIView!
let destinationView: UIView = self.destination.view as UIView!
sourceView.alpha = 1.0
import UIKit
class ToLeftSegueUnwind: UIStoryboardSegue {
override func perform() {
let sourceView = self.source.view as UIView!
let destinationView = self.destination.view as UIView!
let screenWidth = UIScreen.main.bounds.size.width
import UIKit
class ToLeftSegue: UIStoryboardSegue {
override func perform() {
let sourceView = self.source.view as UIView!
let destinationView = self.destination.view as UIView!
let screenWidth = UIScreen.main.bounds.size.width
@trilliwon
trilliwon / makeI18N.sh
Last active January 11, 2022 08:03
makeI18N
#!/bin/sh
echo "generating I18N.swift"
touch tempI18N.swift
echo "struct I18N {" >> tempI18N.swift
inputfile=${SRCROOT}/Pomodoro/Resources/en.lproj/Localizable.strings
while IFS= read -r line