This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SampleSaleArticleViewController.swift | |
// aSample | |
// | |
// Created by Matthew hammond on 09/11/2020. | |
// | |
import UIKit | |
import Buy | |
import SnapKit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getSnapshotImage() -> UIImage { | |
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0) | |
self.drawViewHierarchyInRect(self.bounds, afterScreenUpdates: false) | |
let snapshotImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return snapshotImage | |
} |
NewerOlder