Skip to content

Instantly share code, notes, and snippets.

View KyleGoslan's full-sized avatar

Kyle Goslan KyleGoslan

View GitHub Profile
@KyleGoslan
KyleGoslan / Demo.swift
Created November 9, 2020 18:42
SnapKit Scroll View Example
//
// SampleSaleArticleViewController.swift
// aSample
//
// Created by Matthew hammond on 09/11/2020.
//
import UIKit
import Buy
import SnapKit
@KyleGoslan
KyleGoslan / Array+Extension.swift
Created February 2, 2018 13:48
Swift Array Extension to merge unique values into an existing array. Elements must be conform to the Equatable protocol
//The array passed to the function is the array of object that will be omitted from the final array. Important if your merging an array of objects where the Equatable property may be the same but others may differ.
extension Array where Element: Equatable {
public mutating func mergeElements<C : Collection>(newElements: C) where C.Iterator.Element == Element{
let filteredList = newElements.filter({!self.contains($0)})
self.append(contentsOf: filteredList)
}
}
@KyleGoslan
KyleGoslan / DataSnapsot+Extension.swift
Last active September 28, 2018 11:32
Initilize objects from DataSnapshot that conform to the Codable protocol
import Firebase
extension DataSnapshot {
func toObject<T:Codable>() throws -> T {
var newValue = value as! [String: Any]
newValue["uid"] = key
let data = try! JSONSerialization.data(withJSONObject: newValue, options: .sortedKeys)
return try JSONDecoder().decode(T.self, from: data)
}
@KyleGoslan
KyleGoslan / SKTileMapNode+Extension.swift
Created March 2, 2017 10:28
Method that loops through each tile, with a callback that return the col, row and tile definition.
extension SKTileMapNode {
func loopThroughTiles(completionClosure: (_ col: Int, _ row: Int, _ tile: SKTileDefinition?) -> ()) {
for col in 0..<numberOfColumns {
for row in 0..<numberOfRows {
completionClosure(col, row, tileDefinition(atColumn: col, row: row))
}
}
}
@KyleGoslan
KyleGoslan / String+Extension.swift
Created February 22, 2017 11:04
Random string
extension String {
static func randomString(length: Int) -> String {
let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let len = UInt32(letters.length)
var randomString = ""
for _ in 0 ..< length {
let rand = arc4random_uniform(len)
@KyleGoslan
KyleGoslan / UIImageView+Extension.swift
Created February 20, 2017 11:12
UIImage View extension to load image from web url
extension UIImageView {
public func imageFromServerURL(urlString: String) {
URLSession.shared.dataTask(with: NSURL(string: urlString)! as URL, completionHandler: { (data, response, error) -> Void in
if error != nil { return }
DispatchQueue.main.async(execute: { () -> Void in
let image = UIImage(data: data!)
self.image = image
@KyleGoslan
KyleGoslan / CalculateTime.swift
Created February 8, 2017 22:24
Calculate time to move between two points given an arbitrary speed
func getDuration(pointA: CGPoint, pointB: CGPoint, speed: CGFloat) -> TimeInterval {
let xDist = (pointB.x - pointA.x)
let yDist = (pointB.y - pointA.y)
let distance = sqrt((xDist * xDist) + (yDist * yDist));
let duration = TimeInterval(distance/speed)
return duration
}
@KyleGoslan
KyleGoslan / UIImage+Extension.swift
Created November 27, 2016 21:47
Resize UIImage Swift 3
import UIKit
extension UIImage {
func resizeWith(percentage: CGFloat) -> UIImage? {
let imageView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: size.width * percentage, height: size.height * percentage)))
imageView.contentMode = .scaleAspectFit
imageView.image = self
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
@KyleGoslan
KyleGoslan / DataLoaderProtocol
Created August 3, 2016 21:03
Protocol that loads some JSON from a file in the main bundle (Uses SwiftyJSON).
import Foundation
import SwiftyJSON
protocol DataLoader { }
extension DataLoader {
func loadJSON(fileName: String, type: String = "json") -> JSON? {
let filePath = NSBundle.mainBundle().pathForResource(fileName, ofType: type)
if let data = NSData(contentsOfFile: filePath!) {
let json = JSON(data: data)
@KyleGoslan
KyleGoslan / UIView+Extension.swift
Created July 26, 2016 10:15
UIView extension that returns a UIImage of the view.
func getSnapshotImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0)
self.drawViewHierarchyInRect(self.bounds, afterScreenUpdates: false)
let snapshotImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return snapshotImage
}