Skip to content

Instantly share code, notes, and snippets.

@kirsteins
kirsteins / Array -> UnsafeMutablePointer -> Array
Last active June 16, 2024 09:42
Array -> UnsafeMutablePointer -> Array
var initalArray = [1, 2, 3]
let pointer: UnsafeMutablePointer<Int> = UnsafeMutablePointer(initalArray)
let arrary = Array(UnsafeBufferPointer(start: pointer, count: initalArray.count))
@kirsteins
kirsteins / Back button tapped
Last active August 29, 2015 14:13
Check when back button has been pressed
public override func viewWillDisappear(animated: Bool) {
if let navigationStackViewControllers = self.navigationController?.viewControllers {
if navigationStackViewControllers.contains(self) == false {
// View controller has been popped as it is not in navigation stack any more
}
}
super.viewWillDisappear(animated)
}
@kirsteins
kirsteins / KeyboardListener
Last active August 29, 2015 14:17
KeyboardListener
//
// KeyboardListener.swift
// KeyboardListener
//
// Created by Jānis Kiršteins on 15/03/15.
// Copyright (c) 2015 Jānis Kiršteins. All rights reserved.
//
import UIKit
@kirsteins
kirsteins / Result
Created April 30, 2015 08:54
Result
public class Box<T> {
public let value: T
public init(_ value: T) {
self.value = value
}
}
public enum Result<T> {
case Success(Box<T>)
@kirsteins
kirsteins / Array
Created April 30, 2015 11:42
Array extensions and helpers
internal extension Array {
internal func get(index: Int) -> Element? {
return index >= 0 && index < self.count ? self[index] : nil
}
internal func at(indexes: [Int]) -> [Element] {
var result: [Element] = []
for index in indexes {
result.append(self[index])
}
@kirsteins
kirsteins / gist:7eb4b063cf67bd977479
Last active August 29, 2015 14:21
Echo Swift .gitignore
echo -e 'build/\n*.pbxuser\n!default.pbxuser\n*.mode1v3\n!default.mode1v3\n*.mode2v3\n!default.mode2v3\n*.perspectivev3\n!default.perspectivev3\nxcuserdata\n*.xccheckout\n*.moved-aside\nDerivedData\n*.hmap\n*.ipa\n*.xcuserstate\n.DS_Store\n\nCarthage/Build' > .gitignore
@kirsteins
kirsteins / gist:23d90ee8593c0517f658
Last active August 29, 2015 14:21
Drawing cache
private let drawingCache = NSCache()
public extension UIImage {
public class func imageForSize(size: CGSize, opaque: Bool = false, drawingBlock: () -> Void) -> UIImage {
assert(size != CGSize.zeroSize)
UIGraphicsBeginImageContextWithOptions(size, opaque, 0.0)
drawingBlock()
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
import ObjectiveC
import UIKit
var associatedObjectHandle: UInt8 = 0
class TextDidChangeHandler: NSObject {
unowned let textField: UITextField
let handler: (textField: UITextField) -> Void
init(textField: UITextField, handler: (textField: UITextField) -> Void) {
self.handler = handler
@kirsteins
kirsteins / ViewController.swift
Created March 11, 2016 17:37 — forked from kellyegan/ViewController.swift
Send emails with attachments in iOS using Swift
//
// ViewController.swift
// SendEmailWithAttachment
//
// Created by Kelly Egan on 3/17/15.
// Copyright (c) 2015 Kelly Egan. All rights reserved.
//
import UIKit
import MessageUI
/// Get the top most view controller in presentation chain
func topPresentedController() -> UIViewController? {
var topPresentedController = rootViewController
while let nextTopPresentedController = topPresentedController?.presentedViewController {
topPresentedController = nextTopPresentedController
}
return topPresentedController
}