Skip to content

Instantly share code, notes, and snippets.

View saoudrizwan's full-sized avatar

Saoud Rizwan saoudrizwan

View GitHub Profile
let sortedNumbers = [3, 1, 5, 6, 2, 4].sorted { $1 > $0 }
print(sortedNumbers) // [1, 2, 3, 4, 5, 6]
func injected() {
self.view.backgroundColor = .green
}
@saoudrizwan
saoudrizwan / AppDelegate.swift
Created February 28, 2017 16:22
Set initial view controller when not working with storyboards (don't forget to remove Main.storyboard from Build Settings)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let vc = ViewController()
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = vc
window.makeKeyAndVisible()
self.window = window
return true
// http://nshipster.com/swift-objc-runtime/
extension UIViewController {
private struct AssociatedKeys {
static var DescriptiveName = "nsh_DescriptiveName"
}
var descriptiveName: String? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.DescriptiveName) as? String
}
@saoudrizwan
saoudrizwan / ProtocolFuncDefault.swift
Created March 1, 2017 16:49
Set a default implementation for a protocol function.
public protocol SwipeTableViewCellDelegate: class {
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]?
func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeTableOptions
}
/**
Default implementation of `SwipeTableViewCellDelegate` methods
*/
public extension SwipeTableViewCellDelegate {
func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeTableOptions {
@saoudrizwan
saoudrizwan / ArrayClosures.swift
Created March 1, 2017 17:05
Get a lil functional with your arrays
// .filter() returns a new array of elements that satisfy a predicate declared in our closure
let evenNumbers = [1, 2, 3, 4, 5].filter { $0 % 2 == 0 }
// .map() returns a new array after applying a function to each element
let numbers = [1, 2, 3, 4, 5].map { $0 * 2 }
print(numbers) // [2, 4, 6, 8, 10]
// .flatMap() is like .map(), except it filters out any nil values
let arrayWithNoNils = [1, 2, 3, nil].flatMap { $0 }
print(arrayWithNoNils) // [1, 2, 3]
@saoudrizwan
saoudrizwan / SlideShow.swift
Created May 4, 2017 19:22
Simple UIScrollView + UIPageControl slideshow interface.
import UIKit
class ViewController: UIViewController {
let colors: [UIColor] = [.red, .blue, .green]
lazy var scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.delaysContentTouches = false
scrollView.isPagingEnabled = true
@saoudrizwan
saoudrizwan / ArrayContains.swift
Created May 7, 2017 02:14
Quickly check if an array contains a certain element.
class Person {
let name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
}
import UIKit
class MainController: Controller {
override func viewDidLoad() {
super.viewDidLoad()
controllerView = MainControllerView(controller: self)
}
override func setViewHandlers() {
import UIKit
import SnapKit
class MainControllerView: ControllerView {
// MARK: Views
let label: UILabel = {
let label = UILabel()
label.text = "Hello world"