Skip to content

Instantly share code, notes, and snippets.

View khawajafarooq's full-sized avatar

Khawaja Farooq khawajafarooq

View GitHub Profile
@khawajafarooq
khawajafarooq / RectUtility.swift
Last active September 28, 2017 04:31
CGRect Utility Methods
public class CGRectUtility {
// MARK: Geometry Methods
public class func GMCenteredRectWithinRect(rect: CGRect, containerRect: CGRect) -> CGRect
{
var r: CGRect = rect
r.origin.y = containerRect.size.height / 2 - (r.size.height / 2)
r.origin.x = containerRect.size.width / 2 - (r.size.width / 2)
@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
}
}
@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 / 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 */
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 / 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)
}
@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 / 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 / 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 / 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
}
}