Skip to content

Instantly share code, notes, and snippets.

View hossamghareeb's full-sized avatar

Hossam Ghareeb hossamghareeb

View GitHub Profile
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//.. do other setup
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
// Transition neatly from splash screen
// Very odd, on iPhone 5 you need to position the splash screen differently..
@hossamghareeb
hossamghareeb / gist:7de379d2131831cc10f1
Created February 12, 2016 05:26
Convert video to GIF in mac
Converting your screencast/screen recording to Gif is not a big deal. All you need:
1. QuickTime Player to record your screen OR video file if you already have the video that you wanna convert
2. ffmpeg installed in mac
First of all, install HomeBrew using the following command
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Then install ffmpeg using the following commmand:
@hossamghareeb
hossamghareeb / ascii.swift
Last active January 19, 2020 14:10
ASCII value from one character String in Swift
extension String {
var ascii: Int {
guard let u = UnicodeScalar(self) else { return -1 }
return Int(u.value)
}
}
"a".ascii // 97
"A".ascii // 65
@hossamghareeb
hossamghareeb / digitAt.swift
Created March 29, 2018 09:45
Get digit at index in Int in Swift
extension Decimal {
var intValue: Int {
return NSDecimalNumber(decimal: self).intValue
}
}
extension Int {
func digitAt(i: Int) -> Int {
let div = pow(10, i).intValue
let x = self / div
@hossamghareeb
hossamghareeb / optionals.swift
Created April 2, 2018 15:50
Uncommon ways to handle optionals in Swift
var dic = [String: Int]()
dic["hello", default: 0] += 1
dic // ["hello": 1]
func parse(_ string: String) -> String {
switch Int(string) {
case .some(7): return "SEVEN"
case 5?: return "FIVE"
case let x? where x < 10: return "1-9"
default: return "Not valid INT"
@hossamghareeb
hossamghareeb / binary-search.swift
Created April 16, 2018 18:50
Binary search Swift
/**
Returns index of item if found. Otherwise returns -1 * position where the item should be insertd to
*/
func binarySearch(arr: [Int], start: Int, end: Int, k: Int) -> Int {
var start = start
var end = end
var mid = 0
while start <= end {
mid = start + (end - start) / 2
if arr[mid] == k {
@hossamghareeb
hossamghareeb / partition.swift
Created September 20, 2018 06:00
Partition in Swift
var arr = [1, 2, 5, 5, 6, 7, 10]
let pivot = arr.partition { $0 % 2 == 0 }
arr // [1, 7, 5, 5, 6, 2, 10]
let part1 = arr[..<pivot] // odd numbers
let part2 = arr[pivot...] // even numbers
@hossamghareeb
hossamghareeb / swizzle.swift
Created November 6, 2018 11:35
Swizzle and print all method of a class
private func swizzle(method: String, of class: String, to newMethod: String) {
let original: Method
let swizzled: Method
guard let aClass = objc_getClass(`class`) as? AnyClass else { return }
original = class_getInstanceMethod(aClass, Selector((method)))!
swizzled = class_getInstanceMethod(aClass, Selector((newMethod)))!
method_exchangeImplementations(original, swizzled)
}
@hossamghareeb
hossamghareeb / dynamic.swift
Created February 24, 2019 14:58
Dynamic UITable header view
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
updateTableViewHeaderHeight()
}
private func updateTableViewHeaderHeight() {
guard let headerView = tableView.tableHeaderView else { return }
/// Get the size that should meet the constraints
let size = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
guard size.height != headerView.frame.height else { return }
@hossamghareeb
hossamghareeb / memberwise.swift
Created May 11, 2020 21:49
structs memberwise init
struct Player {
let name: String
var score: Int = 0
}
let p1 = Player(name: "John")
let p2 = Player(name: "Sam", score: 100)