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 = …
Tag Archives: swift
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 …
Continue reading “Copy one or multiple NSTableView rows, Swift”
Easy Core Data CSV Exporter in Swift
A new app I’m working on has a Core Data back end and I wanted to implement a quick way to export all the entities into a CSV file and dump it into the Desktop. Here are the snippets to do it: 1. Make sure you get the NSObjects you want to export (in this case …
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 …
Continue reading “Switching NSViewControllers from NSWindowController”
Random NSColor or UIColor in Swift 2
Yup, not much else to add. Here it is: let randomColor = NSColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1.0) For iOS developers out there just change NSColor to UIColor. Marc PS: Drand48 will create a random number between 0 and 1.
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 …
Continue reading “Get NSDate Day, Month or Year in Swift 2.0”
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 …
Evaluate string width and return CGFloat (Swift, OSX)
This morning I’ve been working in a Swift function to detect the width of a given text and return a CGFloat. I’ve not found that many examples on the web for OSX, so here you have what I’ve done in all of its glory: func evaluateStringWidth (textToEvaluate: String) -> CGFloat{ letfont = NSFont.userFontOfSize(NSFont.systemFontSize()) …
Continue reading “Evaluate string width and return CGFloat (Swift, OSX)”
CSV Parser Swift 2
One of the features I’m adding to an upcoming app is the ability to import CSV files. For that purpose I looked around for code already available (and found several options, see below), however none of them compelled me enough so I decided to create my own. The main objective was to make it as …