Skip to content

Instantly share code, notes, and snippets.

Avatar

Shane Cowherd shanecowherd

View GitHub Profile
@shanecowherd
shanecowherd / timeago.swift
Last active Dec 20, 2019
A modified version of timeAgo. The native Apple one has bugs in iOS 12-13
View timeago.swift
extension Date {
func timeAgoSinceDate() -> String {
// From Time
let fromDate = self
// To Time
let toDate = Date()
let time = Calendar.current.dateComponents([.month, .day, .hour, .minute, .second], from: fromDate, to: toDate)
View codableProtocols.swift
import Foundation
import UIKit
let json = """
[{
"first_name": "Shakti",
"last_name": "prakash",
"age":25,
"address":"Cuttack,Odisha,India"
},
@shanecowherd
shanecowherd / topPresentedViewController.swift
Created Dec 12, 2019
If you have a bunch of view controllers presented on top of each other, you can drill down the hierarchy to find the top VC.
View topPresentedViewController.swift
extension UIViewController {
func topPresentedViewController() -> UIViewController? {
var topViewController: UIViewController? = self
while true {
if topViewController?.presentedViewController != nil {
topViewController = topViewController?.presentedViewController
} else {
return topViewController
}
}
@shanecowherd
shanecowherd / UpdateUIAtBreakpoint.swift
Created Nov 27, 2019
Update the UI if your on a breakpoint, this helps you visualize it on the device.
View UpdateUIAtBreakpoint.swift
(lldb) expr CATransaction.flush()
// This simple command will update your UI on your device while you are at a breakpoint.
@shanecowherd
shanecowherd / SnapAnimation.swift
Created Nov 21, 2019
When you rotate a screen, prevent animations and just snap into place
View SnapAnimation.swift
// When you rotate a screen, prevent animations and just snap into place
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: nil) { _ in
UIView.setAnimationsEnabled(true)
}
UIView.setAnimationsEnabled(false)
super.viewWillTransition(to: size, with: coordinator)
}
View NumberOfRecentCalls.swift
//https://leetcode.com/problems/number-of-recent-calls/
// Tried for loop, etc. While loop was the best when dealing with 10000 requests
// Write a class RecentCounter to count recent requests.
// It has only one method: ping(int t), where t represents some time in milliseconds.
// Return the number of pings that have been made from 3000 milliseconds ago until now.
// Any ping with time in [t - 3000, t] will count, including the current ping.
// It is guaranteed that every call to ping uses a strictly larger value of t than before.
class RecentCounter {
@shanecowherd
shanecowherd / TwoSum.swift
Last active Nov 17, 2019
The typical two sum solution
View TwoSum.swift
// Create a dictionary with the key the index of each item in the array.
// The value is the difference between the array's value and the target.
// Now if we want to find the first solution that contains sum, just loop
// through the array and check the dictionary for other numbers who match the difference.
class Solution {
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var dict = [Int:Int]()
for (i, val) in nums.enumerated() {
@shanecowherd
shanecowherd / CircularQueue.swift
Last active Nov 17, 2019
LeetCode 622. Design Circular Queue
View CircularQueue.swift
//https://leetcode.com/problems/design-circular-queue/
//Passed
//Runtime: 104 ms, faster than 78.33% of Swift online submissions for Design Circular Queue.
//Memory Usage: 21.7 MB, less than 100.00% of Swift online submissions for Design Circular Queue.
class Item {
let value: Int
var next: Item?
var prev: Item?
View ExampleOperation.swift
let operationQueue = OperationQueue()
class ExampleOperation: Operation {
var delegate: SomeDelegate
var callbackDelegate: SomeCallbackDelegate
var success = false // I mark this when the operation has succeeded. Optional
init(delegate: SomeDelegate, callbackDelegate: SomeCallbackDelegate) {
self.delegate = delegate
self.callbackDelegate = callbackDelegate
@shanecowherd
shanecowherd / fileBiggerThan0KB.swift
Last active Nov 12, 2019
Check if a file is bigger than 0 KB
View fileBiggerThan0KB.swift
func fileBiggerThan0KB(path: String) -> Bool {
do {
var fileSize : UInt64 = 0
//return [FileAttributeKey : Any]
let attr = try FileManager.default.attributesOfItem(atPath: path)
fileSize = attr[FileAttributeKey.size] as! UInt64
//if you convert to NSDictionary, you can get file size old way as well.
let dict = attr as NSDictionary