Defer Statement: How to use it in Swift!

Here is a hidden gem in Swift called defer. Defer allows you to defer (no surprises there!) execution of a block of code until the end of the current scope is reached.

func updateImage() {
    defer { print("Did update image") }
    print("Will update image")
    imageView.image = updatedImage
Above code prints the folowing :
// Will update Image
// Did update image

The most common use case seen around is opening and closing a context, for example when handling access to files. A FileHandle requires to be closed once the access has been finished. You can benefit from the defer statement to ensure you don’t forget to do this.

Source: Defer usage in Swift – Swift Programming – Medium

Best Practices: Structuring Your Swift Code!

As your project grows in size, it’s essential that your code is structured well so that future maintenance doesn’t become a pain.

If there is one thing that you can to take from this article then it’s this quote below:

Whenever you touch a part of the code base, you should try to leave it better than how you found it

Check out the article below by John Sundell who provides some great tips to organize your code base which will pay big dividends down the road.

Source: Structuring Swift code — Swift by Sundell

Migrating from iOS to Mac : Build an macOS app in Swift

If you are an iOS developer and have been looking forward to Apple opening up the gates to port your iOS apps to MacOS, then it’s going to be a long and painful waiting game. But fret not, check out this great tutorial from Cory Bohon where he explains some core differences between the two platforms and builds a simple Mac App that you can download and explore.

Source: Migrating from iOS to Mac — Part II: Build an macOS app in Swift

Make Your iOS App ‘Feel’ Better: A Guide Over Taptic Engine & Haptic Feedback!

If you have watched the wonderful WWDC 2017 talk about designing sound for apps then you will know how much impact sound can have on the app’s user experience. With the release of the iPhone 6S and 6S Plus, Apple introduced Taptic Engine which provides tactile sensations in the form of vibrations (called haptic signals.) and along with it came a suite of API’s that developers can harness to make big usability improvements. Check out the article linked below which goes in depth into the API.

Source: Make Your iOS App ‘Feel’ Better — A Guide Over Taptic Engine & Haptic Feedback

iOS 11: Location Permissions and avoiding the Blue Bar of Shame!

Apple has always been known to be a flag bearer for user privacy and the evidence for it is now even clearer with iOS 11. With iOS 11 if an app collects location data in the background, then a big ugly flashing blue bar shows up over the status bar.

If you have an app or planning to develop one that continuously scans user location, then check out the article linked below that details how to avoid it.


Source: Location Permissions in iOS 11 and avoiding the Blue Bar of Shame

NSObject: The Inheritance Curse!

Here is an article that provides a whimsical take on the evil that is NSObject. If you use classes that inherit from UIKit classes then you directly or indirectly inherit from NSObject.

There are ways to stop this inheritance madness in your classes by marking them as final so it cannot be subclassed. It allows some compiler optimizations, and also makes the code more consistent by removing features that can be achieved in a more Swifty way like code reuse with protocol extensions, generics or composition and polymorphism with protocols.

Read on at the link below to find out more tips:

Source: The Inheritance Curse – Swift Programming – Medium

Dev Tools: Introducing Paralayout – An easy to use alternative to Autolayout!

Apple has made tremendous improvements to Autolayout (Apple’s constraint-based system for specifying UI) since the last few years. While technically impressive, in practice it’s more difficult and takes longer to implement especially if you have to modify and animate the layout dynamically or if you want to performance tune the UI. Square’s Cash iOS team have embraced a different philosophy that prioritizes development speed, correctness, readability, maintainability, and performance and they call it Paralayout, a 100% Swift open source library to programmatically code layout rules.

Read on at the link below to find out more.

Source: Introducing Paralayout – Square Corner Blog – Medium

Tutorial: The Complete Understanding of Swift Delegate and Data Source!

If you are new to Swift development, understanding the delegation pattern can be a difficult topic to wrap your head around. Delegate and Datasource patterns are used quite liberally in Swift especially if you are using components like TableViews and CollectionViews and hence it’s imperative that you master the topic. 

Check out the tutorial linked below to get you started on the subject:

Source: The Complete Understanding of Swift Delegate and Data Source

iOS 11: Stack View Custom Spacing!

When Apple introduced StackViews in iOS 9 it quickly became everyone’s favorite control because it was easy to use and also due to the fact that it required fewer constraints to get it going. But for all its power there was one piece missing which was the ability to set a variable spacing between arranged subviews. All this is changing with iOS 11 which introduces a small tweak to allow variable spacing among its arranged subviews. The only caveat is that it can only be done programmatically and not through interface builder.

Read on at this tutorial link below to learn how to implement one:

Source: Stack View Custom Spacing

Swift 4: Ultimate Guide to JSON Parsing!

With Swift 4 came built in support for JSON parsing with a refreshingly easy to use API. There have been a number of great libraries for JSON parsing in the past but now that Swift 4 has built-in support for it, the use of 3rd party libraries has become questionable. The new JSON parsing API is super easy to adopt and also provides the customization required to encode and decode complex scenarios.

Check out this linked article below to learn more:

Source: Ultimate Guide to JSON Parsing with Swift 4 – Ben Scheirman

Tutorial: Getting Started with URLSession!

The URLSession class provides an API for downloading content via HTTP by offering a rich set of delegate methods to support authentication & to give your apps the ability to perform tasks in the background. If you have been using URLConnection in the past or have been using a third party library like Alamofire, then you should take a second look at the much improved URLSession class.

Check out this excellent tutorial written by the folks at Raywenderlich to find out more about URLSession.

Source: URLSession Tutorial: Getting Started

Port: Your iOS App to macOS!

If you are an iOS developer and are thinking about developing apps for macOS but felt daunted, then its time to think again. Your iOS development skills will come to great use when you are developing apps for macOS and the best part is that you can reuse a significant amount of the code between the 2 platforms and only having to rewrite the portions that are platform-specific.

Check out the article linked below that takes you through the process in detail:

Source: Porting Your iOS App to macOS