Skip to content

Instantly share code, notes, and snippets.

View christopherkarani's full-sized avatar

Christopher Karani christopherkarani

View GitHub Profile
@christopherkarani
christopherkarani / SImpleDelegate.swift
Created September 28, 2017 14:23
SimpleDelegationPattern
protocol CallBackDelegate {
func somethingHappened()
}
class FirstClass {
var delegate : CallBackDelegate?
func doSomething() {
delegate?.somethingHappened()
}
}
@christopherkarani
christopherkarani / PathfiningAction.swift
Last active September 28, 2017 20:43
Make A SpriteNode Move along a certain Path
//: A SpriteKit based Playground
import PlaygroundSupport
import SpriteKit
extension CGPoint {
static func CenterOfScene(_ scene: GameScene) -> CGPoint {
return CGPoint(x: scene.size.width / 2, y: scene.size.height / 2)
}
}
protocol SomeProtocol {
var required: String { get }
var optional: String? { get }
}
extension SomeProtocol {
var optional: String? { return nil }
}
struct ConformsWithoutOptional {
let required: String
}
struct ConformsWithOptional {
let required: String
let optional: String?
}
@christopherkarani
christopherkarani / Date.swift
Created October 30, 2017 20:07
Date Convert Extension
extension Date {
func timeAgoSinceDate(date:NSDate, numericDates:Bool) -> String {
let calendar = NSCalendar.current
let unitFlags: Set<Calendar.Component> = [.minute, .hour, .day, .weekOfYear, .month, .year, .second]
let now = NSDate()
let earliest = now.earlierDate(date as Date)
let latest = (earliest == now as Date) ? date : now
let components = calendar.dateComponents(unitFlags, from: earliest as Date, to: latest as Date)
func estimatedFrame(forText text: String) -> CGRect {
// width is same as cell.texView width
//height is an arbitrary larg value
let size = CGSize(width: 200, height: 1000)
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let attributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)]
return NSString(string: text).boundingRect(with: size, options: options, attributes: attributes, context: nil)
}
@christopherkarani
christopherkarani / HandleKeyboard.swift
Last active November 1, 2017 20:08
Functionality for moving a textview Up or down in relation to the Keyboard
extension ChatController {
func setupKeyboardObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillShow(withNotification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillHide(withNotification:)), name: .UIKeyboardDidHide, object: nil)
}
@objc func handleKeyboardWillShow(withNotification notification: Notification) {
let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
let keyboardDuration = (notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue
containerViewBottomAnchor?.constant = -(keyboardFrame?.height)! // bottomAnchor + constant
UIView.animate(withDuration: keyboardDuration!) {
var numbers = [Int]()
for x in 1...100 {
numbers.append(x)
}
var num = [1,2,4,6,8,9,11,13,16,17,20]
func binarySearch(for searchValue: Int, withDatasoucre datasource: [Int]) -> Bool {
var firstIndex = 0
func isValidEmail(testStr:String) -> Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailTest.evaluate(with: testStr)
}