Retain Cycles in Swift

Retain cycles is a very interesting topic.

What picked my brain about retain cycles in Swift though is the actual absence of the warning you get when capturing self strongly inside of a block unlike when writing purely in Objective-C. Let me demonstrate what I am talking about. Let’s assume we are building a simple calculator app that only supports 4 mathematical operations: add, subtract, multiply, and divide. Let’s also assume that in place of a regular method, we will write a block that will perform the calculation for specified operation and set the text of the UILabel to the resulting value.

First of all, let’s declare an enum to keep track of all supported operations:

Continue reading “Retain Cycles in Swift”

Retain Cycles in Swift

It’s The Little Things That Matter…

Since I started at Code Fellows as a Sr. iOS Engineer / Instructor a few month ago, I decided I am going to write an article a week on topics I find interesting when it comes to Swift, Objective-C, or iOS in general.

This week I would like to simply take the time to appreciate the awesomeness of Swift – but not just any version of Swift, I would like to talk specifically about Swift 2.0.

Granted, it’s the second major release of the language (although some can argue, and rightfully so, that Swift 1.2 was somewhat a major release), it’s already on par with Objective-C which, as of writing this article, is stable at version 2.0… Let me get off track for a second please. Every time I mention Objective-C, I feel a bit nostalgic. I started developing in Objective-C years ago… days before ARC and all of the out-of-the-box features we, iOS developers, enjoy today (thank you Apple). Objective-C will always be part of my developers vocabulary.

Anyway, Swift 2.0…

Continue reading “It’s The Little Things That Matter…”

It’s The Little Things That Matter…

Practical Protocol-Oriented Swift

In the beginning Swift protocols felt a bit off when compared to protocols in Objective-C. The fact that I can easily declare an optional method in Objective-C with

is one of many examples. Even though there is a way to somewhat replicate that same behavior in Swift by prepending @objc in front of your protocol declaration, the entire idea of having @objc inside of your Swift class is somewhat weird to me personally. Not that there is anything wrong with it since Swift is using Objective-C runtime anyway.

Swift is a Protocol-Oriented programming language and until Swift 2.0 it was a bit unclear how that really played into a greater scheme of things. However, after the announcement of Swift 2.0 and the ability to extend protocols in Swift 2.0, it became much clearer.

Continue reading “Practical Protocol-Oriented Swift”

Practical Protocol-Oriented Swift

Magic Numbers

Magic numbers term refers to using numbers directly in your code. The assumption among some developers is that each number should have a variable name assigned to it. However, the general rule is that only unclear or repeating numbers should be assigned to a constant for readability and maintainability. There is a difference.

A good example would be the greatest number in the whole world – 3.14159265358979323846. You don’t want to use this number each time you need it in your code. Assigning it to a constant would be a much better solution. Your developer buddies will love you to death for doing that.

Continue reading “Magic Numbers”

Magic Numbers