Weekly Swift 2, getting somewhere

Time flies, it’s been two weeks since I started my daily Swift adventure and I only missed one day. Pretty impressive I’d say. The second week of the daily Swift was a very practical one. It was all about looking through Arto App and building the components I’d need to actually build this app. Building it will be a part of the daily Swift.

Layout and Constraints

A big part of creating a beautiful feed of content seems to be understanding the UITableView. More specifically, understanding UITableViewCells. These Cells are the core of what a user will see and interact with and giants like Twitter, Facebook and Instagram all implement some custom version of a UITableViewCell.

Making my own cells appeared very hard at first. I didn’t understand what would go where, and how would I make it auto-resize itself to fit the content I put into in. After digging around on the web I found a video tutorial and a blog post that explained in part how to do this. Then I started experimenting with LayoutConstraints. These Constraints are extremely powerful and surprisingly simple to write. I’ve been getting warnings every time I used them but everything looks and works fine so I’m not sure how serious these warnings are. An example:

"V:|-10-[myObject]-10-|"

The text above defines a Constraint in Visual Format Language (VFL) and it says that on the vertical axis(V) there should be a 10 point margin (-10-), then myObject appears and then there’s another 10 point margin (-10-). The width of myObject in this case will be the container’s width minus two times a 10 point margin.

Delegation

In order to load data from the Arto API I wanted to have an object in my application that would manage this. I decided to use delegation as a callback style for this object. The implementation of this is actually pretty simple. In the same file as the one where I defined my API class I wrote a protocol. A protocol is defined by naming it and specifying methods that should be implemented by the implementer of this protocol.

Then, whenever the object finished loading some data I call:

self.delegate?.didReceiveData(data)

The fact that there’s a question mark after self.delegate  tells the compiler that there might not be a delegate. If the delegate isn’t there, everything after the question mark is ignored.

So all I needed to do to use this, is make an ArtoAPI instance in my TableViewController, implement the ArtoAPIDelegate protocol and use the data that gets passed to didReceiveData . Easy enough, right?

Bonus: A header image effect

There’s some apps that have this very nice expanding header effect when you pull down the content below it. I have recreated this effect and it actually was surprisingly easy.

Expanding header example

Putting it together

At the end of the week I’ve spent some time putting together the parts. Combining the UITableView, UITableViewCell, ArtoAPI, loading images with Haneke and more. A picture says more than a thousand words so here’s a gif that shows the end result.

Arto Feed Example

In conclusion

The second week of the daily Swift was all about working towards creating and iOS version of Arto App. I’m still trying to decide whether week three should keep pushing forward or if week three should be about learning more small things like an off-screen menu, blurring images, using shadows, implementing POST requests to push data to a server and more of that. If you can’t wait to find out, make sure to follow me on Twitter or check out my Github regularly. Thanks for taking the time to read this and if you have any tips, comments or feedback be sure to let me know.

This week's repositories

Weekly Swift 1, warming-up

It's been my goal to learn how to build apps for a bunch of years now. I’ve picked up some books on Objective-C, tried building some things but lost interest really quick every time. The first time I actually went through with building something was when I was graduating from college. Shortly after that Swift came out and I wanted to learn it. But, once again, with no real goal except just learning Swift I quickly lost motivation to actually do something with it.

But then something happened, I realized that motivation is fleeting. It’s not reliable, one day I could be super motivated and the next day I was too tired to care. So I set out to become more disciplined. And part of this would be learning Swift. I decided to start something called Daily Swift. The idea is to build a (sometimes very) small and simple project in Swift. I have decided to not use Storyboards / Interface Builder because I feel like most pros out there prefer not using it. The projects I build should usually be very bare bones and exactly what I’m missing in Swift resources.

A lot of resources on Swift that I have found are either too complex, or they rely on Interface Builder to work. I feel like the things I’m going to push out are a lot more simple and don’t rely on Interface Builder. If you're like me, a developer who is struggling with these storyboards and overly complicated examples you might want to follow along. And, more importantly you might have some feedback for me or have a subject you'd like me to look into. Please let me know on Twitter.

In my first week I’ve noticed that there’s really tons of magic in Apple’s UIKit framework. They have pretty good documentation for it but sometimes I did have to really force myself to read it all. When you just glance over everything you will surely miss a very important detail and nothing will work and there will be errors all over the place. Also, the use of ? and ! is just confusing in Swift. The ? tells the compiler that something is either an instance of a class or nil. And the ! seems to promise the compiler that you know what you’re doing and the variable (or constant) you’re using is not nil. When I have a better mental model of this I will write about it.

Also, I’m amazed with how much you can learn about Swift in just a single week. I’ve learned how to use Alerts and Actionsheets. I learned about TableViews, TabBars and NavigationControllers. And more interesting, I learned how to add images from an online resource to a TableView without blocking the main thread. (Blocking the main thread by just grabbing the images would make for very choppy scrolling.) And even more interestingly I learned how to load images with HanekeSwift thanks to a tip from @mwildeboer.

My goal for now is to be able to build a Swift version of Arto App. I built this app as an assignment during my minor and I've always imagined it as an iOS app. All the JSON APIs are there already so I’ll be using them as input for some examples and eventually I will bundle them together into a project that would be the iOS version for Arto.

Thanks for reading and if you want to follow my journey you can do so on Twitter and Github.