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: "")
        let alert = UIAlertController(title: localizedTitle, message: localizedMessage, preferredStyle: .Alert)
        let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
        alert.addAction(action)
        
        viewController.presentViewController(alert, animated: true, completion: nil)
    }
}

 

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 = NSDate()
    var until = NSDate()
    
    if let entry = entry {
        if let createdAt = entry.created_at {
            // get date portion only of createdAt
            let entryDateComponents = calendar.components([.Day, .Month, .Year], fromDate: createdAt)
            
            // set the time to midnight and the last second
            let entryDateBegin = "\(entryDateComponents.year)-\(entryDateComponents.month)-\(entryDateComponents.day) 00:00:00"
            let entryDateEnd = "\(entryDateComponents.year)-\(entryDateComponents.month)-\(entryDateComponents.day) 23:59:59"
            
            since = formatter.dateFromString(entryDateBegin)!
            until = formatter.dateFromString(entryDateEnd)!
        }
    } else {
        let currentDateComponents = calendar.components([.Day, .Month, .Year], fromDate: NSDate())
        let currentDateBegin = "\(currentDateComponents.year)-\(currentDateComponents.month)-\(currentDateComponents.day) 00:00:00"
        let currentDateEnd = "\(currentDateComponents.year)-\(currentDateComponents.month)-\(currentDateComponents.day) 23:59:59"
        
        since = formatter.dateFromString(currentDateBegin)!
        until = formatter.dateFromString(currentDateEnd)!
    }
    
    let sinceTimestamp = Int(since.timeIntervalSince1970)
    let untilTimestamp = Int(until.timeIntervalSince1970)
    
    return ["since": sinceTimestamp, "until": untilTimestamp]
}