Get Updates By E-mail 

Try Catch in Swift – A Quick Guide To Basic Error Handling With Try, Try? & Try!

  •  
  •  
  •  
  •  
  •  

f you’re new to Swift then you’ve likely noticed the three different types of Try statements – try, try! And try?

In order to understand the different try statements we first need to know how to declare errors, Swift provides a nice way for doing this using enums like in this example:

// declare an error
enum CorrectError: Error {
// specific case for error, nice way to create many different errors easily
case Wrong
}

Functions throw errors in Swift by using the throws keyword which is placed before the return arrow (the ->)  like the throwing function in this example:

// simple function that throws error
func isRight() throws -> Bool {
throw CorrectError.Wrong
// return statement never executes
return true
}

In order to utilize a throwing function one of the 3 different try statements.

The try keywords  is used within the do -> catch statement like below, typically you’ll use this when you want to catch different types of errors:

do {
// try isRight function
let answer = try isRight()
// never printed because of thrown error
print("it is right")
} catch CorrectError.Wrong{
// this gets printed because of error
print("Wrong")
}

The try! keyword expects a normal type so  function being executed must not throw any error at all or the program will crash with a fatal error as nil is returned.

// will cause program to crash because of error thrown
let answer = try! isRight()
// never executed because of error
print ("is right")

The try? keyword  returns an optional type so if the function throws an error this value is nil.

// sets answer to nil because of thrown error
let answer = try? isRight()
if answer != nil {
// won't print because of thrown error
print("is right")
} else {
// prints because of thrown error
print ("Wrong")
}

As you can see Swift provides a nice syntax for handling errors. This is a vast improvement over error handling in the original release of Swift, and in Objective-C.


  •  
  •  
  •  
  •  
  •  

Swift On Windows – A Native Solution For Learning Swift For Windows Users

  •  
  •  
  •  
  •  
  •  

There are a number of solutions for learning to program with Swift on Windows, and one of the easiest is the open source project Swift for Windows. 

With Swift for Windows you don’t have to worry about using Docker or Ubuntu for Windows, and the solution is completely free.

Swift for Windows provides the compiler, a nice interface so you don’t have to worry about changing any paths or using the command line directly and provides support for Swift 4 (currently 4.1.3) and the Foundation classes.

Swift For Windows Interface

You can find Swift for Windows here.

Now as far as IDE’s go there’s the free Microsoft Visual Studio Code which provides a nice lightweight solution.  The vscode-swift-syntax extension can be used for improved code highlighting.

Improved Swift Code Syntax Highlighting Extension

You can download Microsoft Visual Studio Code here.

One caveat you might run into when using Swift for Windows is a conflict with another current Mingw, you can find the solution to this issue here.

That solution is to simply rename the file “x86_64-w64-mingw32-gcc.bat” to “x86_64-w64-mingw32-gcc” found in \mingw64\bin.

While this is certainly not a complete solution for Swift development (no iOS, Mac support, not all classes available) it can be a quick way to get started if you are interested in learning the Swift programming syntax.


  •  
  •  
  •  
  •  
  •  

HGPlaceholders – Swift Component For Easy UITableView And UICollectionView Placeholders

  •  
  • 2
  •  
  •  
  •  

HGPlaceHolders is an open source component submitted by Hanza allowing you to easily set up placeholders for your UITableView’s and UICollectionView’s.

The library contains a number of default placeholders for common states such as loading, error, no result, and no connection states. You can easily change the images in these to quickly customize the different views or create fully custom states.

This animation from the readme shows HGPlaceHolders in action:
HGPlaceholders

You can find HGPlaceholders on Github here.

A nice library for easily displaying placeholders in UITableView and UICollectionView’s.


  •  
  • 2
  •  
  •  
  •  

DesignableButton – Create Highly Customized UI Buttons In Interface Builder And Swift

  •  
  • 2
  •  
  •  
  •  

DesignableButton is an open source library from Fraser Scott-Morrison allowing you to easily create highly customizable buttons with interface builder and Swift.

DesignableButton allows you to choose through a wide selection of options in interface builder and preview the option selections. You can also set up a Swift extension with all the styles easily. Common needs such as round buttons, and buttons with images above them are very easy to create.

This animation from the readme shows off some DesignableButton design in interface builder:

DesignableButton

You can find DesignableButton on Github here.

A nice library for easily creating custom buttons.


  •  
  • 2
  •  
  •  
  •  

ARKit-CoreLocation – A Library For Easily Using CoreLocation Location Data With ARKit

  •  
  • 2
  •  
  •  
  •  

ARKit-CoreLocation from Andrew at Project Dent that integrates iOS 11’s augmented reality library ARKit, with CoreLocation for GPS based location data.

With ARKit you can easily place objects within an AR world using real-world coordinates, and an example using MapKit is included demonstrating this feature.

There is also functionality included for using CoreLocation to improve AR accuracy.

This animation from the readme shows the library in use:

ARKit-CoreLocation

You can find ARKit-CoreLocation on Github here.

A great library for working with ARKit.


  •  
  • 2
  •  
  •  
  •