Skip to content

Instantly share code, notes, and snippets.

View christopherkarani's full-sized avatar

Christopher Karani christopherkarani

View GitHub Profile

Contributing

When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change.

Please note we have a code of conduct, please follow it in all your interactions with the project.

Pull Request Process

  1. Ensure any install or build dependencies are removed before the end of the layer when doing a
@christopherkarani
christopherkarani / README-Template.md
Created December 7, 2017 11:38 — forked from PurpleBooth/README-Template.md
A template to make good README.md

Project Title

One Paragraph of project description goes here

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

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)
}
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
@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!) {
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 / 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)
struct ConformsWithoutOptional {
let required: String
}
struct ConformsWithOptional {
let required: String
let optional: String?
}
extension SomeProtocol {
var optional: String? { return nil }
}
protocol SomeProtocol {
var required: String { get }
var optional: String? { get }
}