Undo functionality in Core Data (Swift 3)

If you’re developing a Core Data based application for Mac, wouldn’t it be great if you could add undo support? Well, turns out that is a couple of lines away. Follow the simple steps: 1. Make sure your managedObjectContext has an undo manager, without it Core Data can’t keep track of the changes:     …

Sorting Multi Dimensional Array (Swift 3)

Hello all,  This one I’ve found specially useful when working with Core Data fetched arrays.  Let’s say that we want to sort the result of a fetch request based on the value of the field “gold”, we would therefore do the following: let dwarfGoldBags = … //Contains the result of a fetch request with one of the …

Dates & Components in Swift 3.0

The way we work with dates has changed in Swift 3, the change makes it’s way simpler then before. Let’s look at an example: //CreateDateFromComponents (1st January 2017)             var newDate = Date()             let newDateComponents = DateComponents(calendar: Calendar.current, timeZone: nil, era: nil, year: 2017, …

Scheduled notifications in macOS

Notifications are a very useful addition to macOS applications, when done right can help inmensly.  Here’s how you create scheduled and immediate notifications. Let’s start with the immediate ones: func triggerNotification() -> Void {     let notification = NSUserNotification()     notification.title = “titleOfNotification”     notification.informativeText = “whateverTextYou WantToAdd”     notification.soundName = …

Copy one or multiple NSTableView rows, Swift

So here’s a simple yet tricky one: you’ve created your NSTableView but now you would like to allow a user to copy to the clipboard one, or a couple of rows. How do you do it? 1. Implement the function copy (func copy(sender: AnyObject?){}), do not confuse with the method for duplicating an object.   2. Get the …

Switching NSViewControllers from NSWindowController

This has taken quite some time to crack as there was not that much available information online.  I wanted a very simple way to change the NSViewController of a window on the fly by using code.  The solution I ended up with is quite easy, on the NSWindowController I load the default NSViewController by typing …

Get NSDate Day, Month or Year in Swift 2.0

Looking for Swift 3.0 version? -> Click here  Well, having recharged batteries after vacation I’m back into coding! And yet again I was faced with another simple problem: how to get the day, month or year from a date.  This is the simpler solution I’ve found in Swift 2:   //Here I’m creating the calendar …

Dates & Components in Swift 2.0

Another topic that has been changed in Swift 2.0 is how to work with dates. I’m digging into this topic for my upcoming app and I’ve found that most documentation on the web refers to Swift 1.2. So here are some snippets to create dates using components: //CreateDateFromComponents (1st January 2016)        var …