Skip to content

Instantly share code, notes, and snippets.

View TheSwiftyCoder's full-sized avatar

Jay Cohen TheSwiftyCoder

  • UK
View GitHub Profile
@TheSwiftyCoder
TheSwiftyCoder / ProcessVideoWithWatermark.swift
Created August 7, 2017 11:45
Allows a UIImage to be placed full width and height of a video in Swift 3
func processVideoWithWatermark(video: AVURLAsset, watermark: UIImage, completion: @escaping (Bool) -> Void) {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
let outputPath = documentsURL?.appendingPathComponent("dfyVideo_\(Date().timeStamp).mov") // Needs random time
if FileManager.default.fileExists(atPath: video.url.absoluteString) {
do {
try FileManager.default.removeItem(atPath: video.url.absoluteString)
}
catch {
print ("Error deleting file")
@TheSwiftyCoder
TheSwiftyCoder / Swift3-Stop-Words-Array.swift
Created March 26, 2017 07:01
A collection of stop words for checking against in Swift 3. Handy when creating custom URL's
import UIKit
let stopWords = ["a","about","above","after","again","against","all","am","an","and","any","are","aren't","as","at","be","because","been","before","being","below","between","both","but","by","can't","cannot","could","couldn't","did","didn't","do","does","doesn't","doing","don't","down","during","each","few","for","from","further","had","hadn't","has","hasn't","have","haven't","having","he","he","'d","he","'ll","he","'s","her","here","here's","hers","herself","him","himself","his","how","how's","i","'d","'ll","'m","'ve","if","in","into","is","isn't","it","it's","its","itself","let's","me","more","most","mustn't","my","myself","no","nor","not","of","off","on","once","only","or","other","ought","our","ours","ourselves","out","over","own","same","shan't","she","she'd","she'll","she's","should","shouldn't","so","some","such","than","that","that's","the","their","theirs","them","themselves","then","there","there's","these","they","they'd","they'll","they're","they've","this","those","through","to","to
@TheSwiftyCoder
TheSwiftyCoder / UICollectionViewButtons.swift
Created February 12, 2017 10:22
Swift 3 UICollectionView navigation Buttons
@IBAction private func locationLeftButton(_ sender: UIButton) {
let visibleItems: NSArray = self.locationsCollectionView.indexPathsForVisibleItems as NSArray
let currentItem: IndexPath = visibleItems.object(at: 0) as! IndexPath
let prevItem: IndexPath = IndexPath(item: currentItem.item - 1, section: 0)
if prevItem.row != -1 {
self.locationsCollectionView.scrollToItem(at: prevItem, at: .right, animated: true)
}
}
@IBAction private func locationRightButton(_ sender: UIButton) {
@TheSwiftyCoder
TheSwiftyCoder / unableToDeallocateSend.swift
Created January 26, 2017 12:44
Unable to deallocate send right solution
fileprivate func createSettingsAlert(_ title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Settings", style: UIAlertActionStyle.default, handler: { (UIAlertAction) in
// Use the DispatchQueue.main.async {} block to handle any would be background thread methods.
DispatchQueue.main.async {
if let appSettings = URL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(appSettings, options: [:], completionHandler: nil)
}
}
@TheSwiftyCoder
TheSwiftyCoder / userDeviceChangeObserver.swift
Created January 24, 2017 13:39
Swift 3 Handling user device change
// Creates the notifcation constant
let notification = Notification.Name("NSSystemClockDidChangeNotification")
// Add this within your viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(handleTimeChange), name: notification, object: nil)
// Place this within the view controller where the above is added
@objc private func handleTimeChange() {
// Add anything you need in here
NotificationCenter.default.removeObserver(self, name: notification, object: nil);
@TheSwiftyCoder
TheSwiftyCoder / DeletingCoreDataObjects.swift
Created January 23, 2017 14:33
Deleting core data objects in Swift 3
fileprivate func getContext() -> NSManagedObjectContext {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
return appDelegate.persistentContainer.viewContext
}
func deleteTestObjects(entity: String) {
let context = getContext()
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: entity)
@TheSwiftyCoder
TheSwiftyCoder / SwiftyJSONExampleDataUse.swift
Created January 11, 2017 17:19
Gives an example how to pull out JSON data using SwiftyJSON
let currentWeather = JSON(weatherData) // From the API call returned as [String: AnyObject]
let current = currentWeather["current"]
// Dispatch queue prevents app errors when calling the API data from a background thread
DispatchQueue.main.async {
self.friendlyUserMessageLabel.text = current["condition"]["text"].stringValue
self.windDirectionLabel.text = current["wind_dir"].stringValue
self.tempDayLabel.text = "\(current["temp_c"].stringValue)°"
}
@TheSwiftyCoder
TheSwiftyCoder / textViewfunctions.swift
Created January 2, 2017 15:08
Swift textView component functions
func textViewDidBeginEditing(_ textView: UITextView) {
}
func textViewDidEndEditing(_ textView: UITextView) {
}