Skip to content

Instantly share code, notes, and snippets.

View khawajafarooq's full-sized avatar

Khawaja Farooq khawajafarooq

View GitHub Profile
@khawajafarooq
khawajafarooq / CustomPrintable.swift
Last active November 14, 2017 07:33
Customize the console printing of object types in swift
import UIKit
// The protocol use to convert your instance into textual pretty form
protocol CustomPrintable: CustomStringConvertible {
var emoji: String { get }
}
// Defaults
extension CustomPrintable {
@khawajafarooq
khawajafarooq / ReferenceSemantics.swift
Created September 28, 2017 04:30
Underlying concept of reference semantics in swift
class Machine {
let model: String
var make: String
init(model: String, make: String) {
self.model = model
self.make = make
}
}
@khawajafarooq
khawajafarooq / ValueSemantics.swift
Created September 28, 2017 04:29
Underlying concept of value semantics in swift
enum Direction {
case up
case down
case left
case right
}
struct Position {
var x: Int
var y: Int
@khawajafarooq
khawajafarooq / MixedSemantics.swift
Last active September 28, 2017 03:51
Underlying concept of mixed semantics in swift
struct Address {
let unitNumber: String
let buildingNumebr: String
}
struct Residance {
var numberOfRooms: Int
var address: Address
}
@khawajafarooq
khawajafarooq / SieveOfEratosthenes.swift
Last active April 30, 2019 21:40
Sieve of Eratosthenes' method to find prime numbers up to a give number N
/*
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
The sieve of Eratosthenes is one of the most efficient way to find all prime numbers up to any given limit.
It's a simple and ancient algorithm that is know for it's performance. Here is a swift flavor of this algorithm.
*/
func sieveOfEratosthenes(number n: UInt) -> [UInt] {
var list = Array(2...n)
list = list.filter {
@khawajafarooq
khawajafarooq / selectionsort.swift
Last active August 4, 2017 13:46
Swifty selection sort
func selectionSort<T: Comparable>(_ a:[T]) -> [T] {
guard array.count > 1 else { return array }
var array = a
for i in 0..<array.count {
if let min = Array(array[i..<array.count]).min() {
if let idx = array.index(of: min) {
array.swapAt(i, idx)
}
class TimerTestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Timer.scheduledTimer(timeInterval: interval, target: self,
selector: #selector(ViewController.methodToRepeat(timer:)),
userInfo: nil, repeats: true)
}
@khawajafarooq
khawajafarooq / String+Common.swift
Last active October 10, 2017 17:25
String extension for swift
import Foundation
extension String {
/* Get length of string */
var length: Int {
return self.characters.count
}
/* Trim leading or trailing white spaces */
@khawajafarooq
khawajafarooq / UIViewController+KeyboardFeature.swift
Last active September 28, 2017 04:31
Hiding keyboard category on clicking anywhere on the screen
import Foundation
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
@khawajafarooq
khawajafarooq / RoundingDecimal.swift
Last active October 10, 2017 17:27
Rounding a double number to a number of decimal places
import Foundation
extension Double {
mutating func round(to places: Int) -> Double {
let divisor = pow(10.0, Double(places))
return Darwin.round(self * divisor) / divisor
}
}