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.
Category: snippets
Read a File From Xcode Playgrounds
If you need to have some data in an external file and read it in playgrounds, here’s how to do it in Xcode 11.3 using Swift 5.1.
How to Reverse a Number Mathematically
Here’s a simple method for getting the reverse of a positive integer.
Create a Circular Array with the Modulo Operator
If you need to continue looping over an array after you’ve reached the end, you can use the modulo operator to start the index back at 0.
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
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