Basic init rules in Swift

I kept wondering whether you’re supposed to call the parent init method before or after your code and I found a basic answer: init() { // Put values into your instance variables and constants super.init() // Other initialization code, such as calling methods, goes here }  

Published
Categorized as hints 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”) }  

Notifications in iOS

Posting a Notification First, create a global key before the class definition of the class that is posting the notification Post the notification

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

Send Mail with Attachment in iOS

if MFMailComposeViewController.canSendMail() { let data = prepareCSVData() let mailController = MFMailComposeViewController() mailController.mailComposeDelegate = self mailController.setSubject(“Data Export”) mailController.setMessageBody(“Attached is a CSV file containing your data.”, isHTML: false) // Add attachment if let data = data { mailController.addAttachmentData(data, mimeType: “text/comma-separated-values”, fileName: “headaches.csv”) presentViewController(mailController, animated: true, completion: nil) } else { NSLog(“No data”) } }  

Scroll to the Bottom of a Collection View

private func scrollToBottom() { let lastSectionIndex = (collectionView?.numberOfSections())! – 1 let lastItemIndex = (collectionView?.numberOfItemsInSection(lastSectionIndex))! – 1 let indexPath = NSIndexPath(forItem: lastItemIndex, inSection: lastSectionIndex) collectionView!.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Bottom, animated: false) }  

Add a UITableView to an Existing ViewController in Xcode

Quick and dirty checklist for adding a table view to an existing view controller: Embed existing view controller in a NavigationController Drag a UITableView object into the view controller in the storyboard Make the UITableView the same width and height as the view controller and set the constraints to all four sides with no margins… Continue reading Add a UITableView to an Existing ViewController in Xcode