Swift: Create P12 Keystore with OpenSSL
Swift: Create P12 Keystore with OpenSSL avatar

To create a P12 keystore with Swift using OpenSSL, use the following function:

Swift: Enumerate / Iterate Enum
Swift: Enumerate / Iterate Enum avatar

In Swift, you cannot iterate over an enum by default. (This means an enum is not enumerable!).

This feature can be added by introducing a protocol, which inherits from RawRepresentable:

This can be further simplified to:

Note: This protocol can only be used on enums, which inherit from Int

(Taken from this thread.)

Swift: Synchronized
Swift: Synchronized avatar

In Swift, the synchronized keyword does not exist. Instead you can use

 

Swift: Deactivate Logging in Production
Swift: Deactivate Logging in Production avatar

To deactivate logging in production, place the following function in your code (preferably in AppDelegate.swift outside any class):

Setup for View Controller Tests
Setup for View Controller Tests avatar

To perform full unit testing of a view controller in Swift the definitions and settings in the storyboard need to be loaded. Each view controller class CONTROLLER is connected to a view controller in the storyboard identified by a unique IDENTIFIER. This relationship can be used to instantiate the view controller from the storyboard with all necessary data. Just insert the folloing code at the top of your XCTestCase class.

View Controller Test: Alert
View Controller Test: Alert avatar

Testing alerts in Swift is a bit tricky because it is not possible to directly access the handler of an UIAlertAction. First, you need to introduce a member variable in the view controller for the UIAlertAction, which can be overwritten by a mock. Next, you need to extend UIAlertAction by a function, which receives the handler and creates the action. This function can then be overwritten in UIAlertActionMock to store the handler and call it inside the test. Tough shit. In the following, a full example is shown:

View Controller:

Test Class:

View Controller Test: Segue (reverse)
View Controller Test: Segue (reverse) avatar

Sometimes a SEGUE cannot be triggered by a function defined in the view controller but only via the storyboard. In this case, a reverse test can be done by directly performing the segue and checking if the right TARGETVIEWCONTROLLER is added:

View Controller Test: Passing Arguments When Segue Is Performed
View Controller Test: Passing Arguments When Segue Is Performed avatar

To test if an argument is passed to another view controller when performing a segue use the following function:

View Controller Test: Segue
View Controller Test: Segue avatar

To test if an ACTION of a view controller CONTROLLER triggers a certain SEGUE use the following function: