Hacking with Swift – Learning Project 16

Day 68 / 100

I know, I am hopelessly late in the program but I really cannot do what Paul says, one hour a day and that’s it, it is just not enough for me. The curriculum is really too hard for people who do not come from a technology background to be done in 100 or so hours.

It needs more time and I am doing every day minimum 1.5-2 hours and still, I am lagging behind.

The program for Day 60 is MKMapView, MKAnnotation, CLLocationCoordinate2D, where MK stands for MapKit and CL could be CoreLayer or CoreLocation as far as I know.

Setting up

Let’s create a new Single View iOS App and, in the Storyboard, embed the view controller in Navigation Controller (Editor > Embed In > Navigation Controller). Then drag a Map Kit View from the Object Library down on the canvas and make it stretch to occupy all our screen. Use the Editor > Resolve Auto-Layout Issues > Add missing constraints command to automatically add all the necessary constraints.

Up and running with MapKit

Let’s create an outlet from the map view called mapView and set the view controller to be the delegate of the map view. Also import MapKit in ViewController.swift.

Now create a new Cocoa Touch Class, subclass of NSObject called “Capital”. Make it conform to the MKAnnotation protocol and give it three properties: an optional string title, a CLLocationCoordinate2D coordinate and an info string. Set the initialiser and go back to ViewController.swift. For the curious like me here is a taste of what the MKAnnotation protocol looks like:

MKAnnotation protocol's description
MKAnnotation protocol’s description

Inside viewDidLoad initialise five capital cities as in the given code. Then add a call to mapView.addAnnotations([]) and, inside those square brackets, add the five cities.

Annotations and accessory views: MKPinAnnotationView

First of all make the ViewController class conform to MKMapViewDelegate (we have already done this in the storyboard but this is necessary for code completion to work). Second, be sure that the annotation parameter is a Capital object, otherwise just exit from the method. Third, define a reuse identifier “Capital” (or whatever best fits) and dequeue it from the list of the map-view’s unused views. Fourth, if there is not an annotation found, create a new one using assigning the result of the call to MKPinAnnotationView(annotation:reuseIdentifier:) to the annotation view and allow it to show the callout. The canShowCallout property is a Boolean value indicating whether the annotation view is able to display extra information in a callout bubble. Fifth, create a new detail disclosure type button and assign it to the right callout accessory view property of the annotation view. Sixth, if a view can be reused, it should be updated to use the annotation passed in to the method.

This last part is not completely clear to me. Where did we set the identifier to be “Capital” elsewhere? Apparently nowhere. So how is the code connecting to an annotation that doesn’t exist? Also, we are not calling this method anywhere so I really do not understand how this is working and, sadly, I just have to copy the code and move on. Fine…

At the end of the method call the return annotationView line.

Regardless of whether you understood this last part or not (it seems that this is not important and that to become a programmer you just need to be a mindless automated being that just repeats things without any creativity in mind) move on and add the mapView(annotationView:) method.

Make sure that the view’s annotation is of type Capital. Store the title and info in two constants and create an alert controller to show the title as title and the info as message.

You can find the code for this project here.

Please don’t forget to drop a hello and a thank you to Paul for all his great work (you can find him on Twitter) and don’t forget to visit the 100 Days Of Swift initiative page.

Thanks for reading!

Till the next one!


If you like what I’m doing here please consider liking this article and sharing it with some of your peers. If you are feeling like being really awesome, please consider making a small donation to support my studies and my writing (please appreciate that I am not using advertisement on my articles).

If you are interested in my music engraving and my publications don’t forget visit my Facebook page and the pages where I publish my scores (Gumroad, SheetMusicPlus, ScoreExchange and on Apple Books).

You can also support me by buying Paul Hudson’s books from this Affiliate Link.

Anyways, thank you so much for reading!

Till the next one!

Published by Michele Galvagno

Professional Musical Scores Designer and Engraver Graduated Classical Musician (cello) and Teacher Tech Enthusiast and Apprentice iOS / macOS Developer Grafico di Partiture Musicali Professionista Musicista classico diplomato (violoncello) ed insegnante Appassionato di tecnologia ed apprendista Sviluppatore iOS / macOS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: