Hacking with Swift – Challenge 1

A little introduction: I am now switching to English for the next couple of articles as it seems there is little to no point in writing them in Italian for an activity that is completely taught and done in English. If I will be proven wrong I will gladly return to writing in Italian (and for that I mean: “if I will see some good amount of compatriots coming to the site and leaving there opinions on these articles”).

This series of articles will follow the 100 Days of Swift initiative promoted by Paul Hudson. You can find it here:

After completing the first project it is time to review what was learned.

  • Interface Builder’s document outline shows all view controllers in a storyboard.
  • UITableViewController inherits from UIViewController.
  • iOS automatically looks for an initial view controller to show when launching an app.
  • All iOS apps are built into a single bundle.
  • Interface Builder creates outlets as implicitly unwrapped optionals.
  • iOS reuses table view cells that have moved off the screen.
  • Storyboards contain the user interface layouts for our app.
  • @IBOutlet marks a property that is connected to something in Interface Builder.
  • Navigation bars are the gray bar across the top of our user interface.
  • Navigation controllers manage a stack of view controllers that can be pushed by us.
  • viewDidLoad() is called after our view has been created.
  • Auto Layout constraints must always be complete and non-conflicting.

These are just a few of the things learned.

The most important one, for me, was the FileManager part:

let fm = FileManager.default
let path = Bundle.main.resourcePath!
let items = try! fm.contentsOfDirectory(atPath: path)

The Challenge

We have to modify the project just completed with something that it is said to be in our grasp. Here is what we have to do:

  1. Use Interface Builder to select the text label inside our table view cell and adjust its size to something larger.
  2. In our main table view we have to show the list of image names in sorted order.
  3. Rather than show image names in the detail title bar, we are asked to show “Picture X of Y”, where Y is the total number of images and X is the picture’s position in the array.

1.

The first thing unclear to me is whether the size of the text inside of the label should be larger, the label itself should be larger or both. Regardless, I will try not to interpret too much what I read and go for the second one: make the size of the text label bigger.

Going to the Size Inspector in Interface Builder greets me with a lot of not editable fields.

Selecting any of the handles around the label is impossible and it does not let me edit the size of the label.

As far as I know every cell in a Table View has its own text label whose size is fixed so I will go on and change the size of the text. I am sure if someone will ever give a look at this he/she will be so kind to tell me what I am missing.

I tried with Font size 20.0 and it looks a bit too big to me but I will leave it like that by now.

2.

This was a possibly simple one. When we attach the name of the image to the proper cell it is already too late to sort the array.

My solution is to modify the array in the viewDidLoad() method after the loop has finished its course. Therefore the code for the loop looks something like this (the new line being the one with the blue highlight in the left side):

Screen Shot 2019-02-19 at 21.56.13

The .sort() method is one of the many option one has to manipulate arrays and it is described as follows from the Standard Library.

Screen Shot 2019-02-19 at 21.58.29

It seems that I could have called the sort(by:) method but in this case it was not necessary. The running app now looks as this:

Screen Shot 2019-02-19 at 21.59.53
Perfectly sorted!

It seems I could have called the sort() method even outside of the loop, as long as it is after the loop itself and inside the viewDidLoad() method.

3.

For the third and final challenge a bit more of thinking was needed.

In the current state of the app the title of the Detail View Controller is set in the DetailViewController.swift file itself.

The problem is that the array of images does not exist there, that is this view controller seems not to know virtually anything about that.

I therefore commented out the line title = selectedImage in that file and, in ViewController.swift, in the tableView(didSelectRowAt:) method, added a line under the vc.selectedImage one:

vc.title = “Picture \(indexPath.row + 1) of \(pictures.count)”

The +1 offsets the fact that the arrays start counting from 0.

Right or wrong this is the result:

Screen Shot 2019-02-19 at 22.16.00

As my Tai Chi instructor says:

There is no right or wrong! What is good today it will be not good in a week but today you did the best you could do!

I hope someone will see this and give a look at it in case I made some absurd mistakes!

All (educated) suggestions are welcome!

Thank you!


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

One thought on “Hacking with Swift – Challenge 1

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: