Streaming File Reader in Swift

If you need to read a very large file you’ll want to stream it so you’re not loading the entire thing into memory at once. Here’s a snippet for doing that.

Play Music in iOS

Here’s a utility to play background music for iOS in Swift: First, import AVFoundation. Then add: var backgroundMusicPlayer: AVAudioPlayer! func playBackgroundMusic(filename: String) { let resourceUrl = Bundle.main.url(forResource: filename, withExtension: nil) guard let url = resourceUrl else { print(“Could not find file: \(filename)”) return } do { try backgroundMusicPlayer = AVAudioPlayer(contentsOf: url) backgroundMusicPlayer.numberOfLoops = -1 //… Continue reading Play Music in iOS

Verify Checksums

Navigate to where the file is located (probably the Downloads folder) and enter the appropriate command in the console. sha-256: shasum -a 256 file.iso sha-1 shasum -a 1 file.iso md-5 md5 file.iso

Published
Categorized as snippets Tagged

Generic Alert Method

Here is a generic alert method that you can place in a global functions/variables file that checks for an existing alert first. func showAlert(withTitle title: String, message: String, viewController: UIViewController) { if viewController.presentedViewController == nil { // Prevent multiple alerts at the same time let localizedTitle = NSLocalizedString(title, comment: “”) let localizedMessage = NSLocalizedString(message, comment:… Continue reading Generic Alert Method

Get Timestamps

This is just a random method I’m no longer using and wanted to get rid of but didn’t really want to throw it away in case I want to reference it someday. static func getTimestamps(forEntry entry: Entry?) -> [String: Int] { let calendar = NSCalendar.currentCalendar() let formatter = NSDateFormatter() formatter.dateFormat = “yyyy-MM-dd HH:mm:ss” var since… Continue reading Get Timestamps

Trim whitespace from a string in Swift

let whitespaceSet = NSCharacterSet.whitespaceCharacterSet() let trimmedString = myTextField.text!.stringByTrimmingCharactersInSet(whitespaceSet) if trimmedString.characters.count == 0 { print(“myTextField is blank”) }  

Add Share Button to iOS 9 App

@IBAction func export(sender: UIBarButtonItem) { if let data = prepareCSVData() { let filename = getDocumentsDirectory().stringByAppendingPathComponent(“data.csv”) data.writeToFile(filename, atomically: true) let url = NSURL(fileURLWithPath: filename) let activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil) if let popoverController = activityViewController.popoverPresentationController { popoverController.barButtonItem = sender } presentViewController(activityViewController, animated: true, completion: nil) } else { NSLog(“Unable to prepare data”) } } The… Continue reading Add Share Button to iOS 9 App