Get Date Day, Month or Year in Swift 3.0

Just a quick update to show how to get Date Day, Month or Year in Swift 3

//Here I’m creating the calendar instance that we will operate with:

let calendar = NSCalendar.init(calendarIdentifier: NSCalendar.Identifier.gregorian)

     

 

//Now asking the calendar what month are we in today’s date:

let currentMonthInt = (calendar?.component(NSCalendar.Unit.month, from: Date()))!

       

 

//Now asking the calendar what year are we in today’s date:

let currentYearInt = (calendar?.component(NSCalendar.Unit.year, from: Date()))!

 

Hope this proves useful.

If you would have any suggestions on how to improve reach me @MarcMasVi in Twitter. 

 Marc

Blogging about developing an app: Denarius

Roughly two years ago I started blogging about the challenges of developing a new Mac app. I decided to do so after reading Brent Simmons great series on Vesper development. I thought it was a great way to 1. structure my thoughts and 2. help the community. 

It’s been one of the most fun times I’ve had in years. The kick I get out of creating something from nothing* is not easily found anywhere else.

Also worth mentioning is the Mac community: all fellow Mac developers I engaged with were very helpful and responsive, I’m extremely thankful for that. Thank you. 

Enough rambling, about the app I’ve been -indirectly- blogging about: It’s called Denarius and it’s a Personal finance Mac app. For years I controlled my personal finances with excel; existing Mac apps had a lot to offer but they required a lot of micromanagement as well.

My goal for Denarius was simple: the app should tell you all you need to know with minimal effort.

I hope you like it, you can know all about it here. And you can get in touch with me via Twitter at @MarcMasVi

Will update you on how the app does and what I will be working on next. 

 

Marc 

* leveraging the awesome work of Apple developers who worked on all the frameworks I use. 

Simple Animations on macOS (Swift 3)

If you’ve ever coded for iOS you’ll most likely be familiar with the UIView animateWithDuration function. Wouldn’t it be great if there would be an OSX equivalent?

Well, there sure is, just type the following:

        NSAnimationContext.runAnimationGroup({_in

            //Indicate the duration of the animation

            NSAnimationContext.current().duration = 5.0 

            //What is being animated? In this example I’m making a view transparent

            someView.animator().alphaValue = 0.0 

        }, completionHandler:{ 

            //In here we add the code that should be triggered after the animation completes.

            print(“Animation completed”)

        })

 

That’s it, you’re good to go! Questions / comments? I’m at @MarcMasVi 

 

Marc

 

Synchronizing Main and Background Core Data Threads (Swift 3)

Let’s say we have two different managedObjectContext (with one persistentStoreCoordinator). 

The first one is used across the app for most quick fetches:

var mainManagedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.mainQueueConcurrencyType)

 

        mainManagedObjectContext.persistentStoreCoordinator = coordinator

 

 

And the second, running in the background, for the queries that take a long time:

var backgroundManagedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType)

        backgroundManagedObjectContext.persistentStoreCoordinator = coordinator

 

When they are initialized both contain the same data. However once changes are applied to one of them -in this example the main- like adding, changing or deleting rows, the other will not know -in this example the background/private one-.

 

Screen Shot 2016 11 27 at 13 19 12

 

Therefore if we add objects to the main managedObjectContext, when we fetch the backgroundManagedObjectContext we will not get them. 

How do we fix this? Well first we have to save the changes of the objectContext that was modified. We do so by adding once we’ve finalized the changes:

       //Commit changes

        do {

            try managedObjectContext.save()

        }catch{

            print(“ERROR: Cound not save Core Data”)

        }

        

At this point we have saved the changes in the Persistent Store. We still have to ask the backgroundObjectContext to get them from the persistent store thought. For that we will use the automatic notification “NSManagedObjectContextDidSave”.

In the class where the backgroundThread is contained we will add an observer for the notification (note that we’re passing as object the context that was modified):

//Handle changes in core data for background thread

        NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.handleMainCoreDataChangeInBackgroundManagedContext(notification:)), name: NSNotification.Name.NSManagedObjectContextDidSave, object: managedObjectContext)

    }

And then we will add the function that will syncronize the backgroundThread, in this example: 

func handleMainCoreDataChangeInBackgroundManagedContext(notification: Notification){

        managedObjectContextBackgroundThread?.mergeChanges(fromContextDidSave: notification)

    }

That’s it! Now when you query the background thread you’ll get the same information as you have in the main one. 

You can read more about NSManagedObjectContext in Apple Docs

 

Questions / comments? I’m at @MarcMasVi 

 

Marc

 

 

Note: using two contexts is very powerful when used in combination with dispatchqueues

       DispatchQueue.global().async {

            //Query here in background context & save if applicable

            DispatchQueue.main.async {

                //Refresh UI & query main context & save if applicable

            }

        }

 

Are we (NSViewController view) the First Responder?

In some cases a class may want to know if it’s the first responder before triggering an action.

For instance if several loaded View Controller classes are listening for the same notification, how can they know which one should trigger the action? Well… they should ask their view. 

In this case a NSTableView class is asking if it’s currently the first responder:

        if (self.view.window?.firstResponder == self.tableView){

            triggerFunctionHere()

        }

And that’s it 🙂

Questions / comments? I’m at @MarcMasVi 

Marc

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:

         managedObjectContext?.undoManager = UndoManager() 

2. In the viewController where you should handle the undo operation just add:

      func undo(_ sender: AnyObject?){

  

        coreDataHelper.managedObjectContext.undoManager?.undo()

 

        refreshTable() //If applicable (for instance if you don’t use bindings)

        re-fetch the information and reload the table. This is what this

        function does in my code

 

      }

And that’s it, now you have full undo functionality! You’ll see how in the menus Undo no longer appears greyed out.

Questions / comments? I’m at @MarcMasVi 

Marc

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 fields named “gold” and being an integer.


 

We will now proceed to sort the array and store it in sortedGoldArray:

let sortedGoldArray = dwarfGoldBags.sorted { ($0.goldas Int) > ($1.goldas Int) } 

 

If instead of core data you have a multi-dimensional array it would be as easy as:

let sortArray = originalArray.sorted { ($0.[0] as Int) > ($1.[0] as Int) } 

 

In this case we’re comparing the values stored in the first value of the subArrays. 

Questions / comments? I’m at @MarcMasVi 

Marc

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, month: 1, day: 1, hour: nil, minute: nil, second: nil, nanosecond: nil, weekday: nil, weekdayOrdinal: nil, quarter: nil, weekOfMonth: nil, weekOfYear: nil, yearForWeekOfYear: nil)

            newDate = Calendar.current.date(from: newDateComponents)!

 

Hope this proves useful. If you would have any suggestions on how to improve you know where to reach me, @MarcMasVi in Twitter.

 

Marc

 

Testing launch experience for a Mac app

In iOS it’s quite easy, you just delete the app and all of it’s data is removed as well. But how do you do it in OSX? Well, you should remove:

~/Library/Application Support/<YOUR OSX APP BUNDLE NAME>/

for example, by default should be:

~/Library/Application Support/com.yourcompany.yourapp/

And this is it! I suggest you do a Project -> Clean Build folder (to see this option you will have to press Alt while in the Porject menu) afterwards, just to be sure. 

Questions / comments? I’m at @MarcMasVi 

Marc