Validating View Controllers with Quick and Nimble

On arriving  UI automated testing in XCode 7 developers had a great tool for automatically validate our apps.  But as our test suite starts growing we start facing some inconveniences.

By using accessibility interface you cannot validate view controller attributes, for instance validate if you got the right number of cells in a collection. Another problem that I faced was at time of refactoring test cycles took to long.

I strongly believe in testing, but dealing with such inconveniences  pushed me to search for another iOS test frameworks. After watching some videos and hearing about them in some conferences I decided to investigate about validating view Controllers with Quick and Nimble.

The aim of this post is just show how to setup Quick and Nimble test frameworks and play a little bit with them.

Quick and Nimble

Quick is a BDD testing frakework that is almost the same as XCTest, but with the additional focus on describing your intent and why you are testing parts of your code.

Nimble allows you to express expectations using a natural, easily understood language.

Environment

I have found some difficulties downloading sample projects with the framework integrated. So at the time of writing this posts the XCode that I am using is 7.3.1 and the Podfile content is the following:

View controller undertest

The view controller under test shows a collection of Gremlins with a search bar that will allow to filter them by name.

Simulator Screen Shot 31 Jul 2016 00.33.15

 

Test are implemented as a regular UN test, so adding a new test is just adding a new UN test file:

projfiles

untest

As was mentioned before test are implemented as BDD test structure changes a little bit but do not get scared. Lets start  testing the view controller title:

First thing to do is import Quick and Nimble and extend our test class from QuickSpec. beforeEach is the chunk of code that will be execute before each test. Tests itself are implemented by specifying with it.

Another important issue is the call controller.beginAppearanceTransition before each test, this execute viewcontroller lifecycle calls before executing test.

Now lets launch test:

redtogreen

But remember we have to move from red to green. Now, just set the expected title string name and re-run the tests.

The first impression that you experiment with this framework is how easy and fast is execute a battery of test. An how easy is get into view controller internal attributes and methods for validating what is going on.

Let’s test that collection view can show all the expected gremlins

The view controller by default will show a hardcoded set of gremlins for better understanding, but that is not the real case scenario. For validating that the first that we do is just validate that the number of items that can see the collection view are 13, and afterwards we inject an array of 3 items for validating that the new number of items that can see the collection view now is different:

Lets look for more gremlins

But the view controller also allows us search gremlins by name, now lets validate that this functionallity is really working:

An important issue that I want to mention at that point is that or view controller hides the search bar when there are no items. This is very easy to do with this framework, just check .hidden attribute from search bar and that’s all!.

You can download the sample project used in this post here.

Conclusions

With Quick and Nimble you can run a test suit very quickly and this is great for refactoring. On the other hand is direct to have access to view controller internal stuff, this will save you lines of code and execution time on UI testing. So Quick and Nimble is a great alternative to unload test stuff from UI testsuite.

gizmo

Useful links

Model your classes with Argo JSON parser

The aim of this post is just basic introduction about how to use Argo framework for feeding your model classes from JSON data.

What is Argo.

[From https://github.com/thoughtbot/Argo]

Argo is a library that lets you extract models from JSON or similar structures in a way that’s concise, type-safe, and easy to extend. Using Argo, you won’t need to write validation code to ensure that incoming data is of the right type, or to make sure required data fields aren’t turning up empty. Argo uses Swift’s expressive type system to do that for you, and reports back explicit failure states in case it doesn’t find what you’ve told it to expect.

Argo

Argo is the Greek word for swift and the name of the ship used by Jason, son of Aeson, of the Argonauts. Aeson is the JSON parsing library in Haskell that inspired Argo, much like Aeson inspired his son Jason.

Codoapods. First things first.

In the Podfile you will have to add two frameworks, Argo itself and Curry that is being used for saving effort on initializing structs and classes.

 

Parsing structs

In this section through different examples I will explain how to parse the most common JSON structures.

Example 1. Most Basic JSON structure.

The content of the json file is the following:

The modeling struct is the following:

Just a few comments

  • As you can see with curry you save up call the initializer passing all class attributes.
  • <^> is for marking the first attribute, the rest are marked with a <*>
  • <| is for parsing a regular attribute and <|? for the optional ones.

For parsing just:

  • “person” is just the filename that contains the json.
  • JSONFromFile is a helper method that retrieves the json from the file. At the end of the post you will find a like for downloading whole sample project.

Example 2. 1-n attributes.

The content of the json file is the following:

The modeling struct is the following:

And this one:

Just a comment:

  • <|| is for parsing a collection of items

For parsing just:

Example 4. Another way of parsing JSON file.

The content of json file is the following:

The modeling struct:

For parsing the struct just:

As you can see the way of calling parser in that case is different but the result is exactly the same.

Parsing classes

For parsing classes I am based on the last struct example, but for clear understanding I will show an example.

This is the json content:

The modeling class:

In deep the idea is quite close than with structures, but in case of classes is carried out in a different way.

Finally for calling the parser:

Conclusion

I hope that throught all those examples you will not find any difficulty for understanting how to do a basic parsing. Please take a look at the library documentation for deeper understanding. You can download the sample project from here.

Usefull links

 

Custom uitableview cell in Swift

The aim of this post is to show how to build a Custom SWIFTed TableViewCell view cell. I have found several shallow examples with the default cell. But soon you will discover that will not fit a real sample project situation. I have found several difficulties from moving to Swift, that took me more time than I expected and that pushed me also to write this post.

The custom table view cell

I have just created a new Single View Project Application  in the default view controller  and added a new NSObject class that extends from uitableviewcell.

Captura de pantalla 2015-07-10 a las 18.54.54

 

Captura de pantalla 2015-07-10 a las 18.56.24

 

The storyboard

I have added a table view controller and a tableview cell with an image view and a label.

Captura de pantalla 2015-07-10 a las 19.02.50

Now is time to setup the custom table view cell. Firstly the  new  class associated to the cell:

Captura de pantalla 2015-07-10 a las 19.07.15

And secondly set the cell identifier:

Captura de pantalla 2015-07-10 a las 19.07.40

The View Controller

Now it is time for connecting the storyboard with the source code. Lets start off with the main view controller:

I have mapped the tableview to tbvData, and added the minimum uitableview delegates. The table view is filled with 3 elements contained in two arrays (items and images).

The table view cell

In the custom cell I have added an image view and a label as an example. This is the code that  configures the cell:

 

Finally

Captura de pantalla 2015-07-11 a las 14.58.34

You can get also find the code here

NSUserDefaults best practices

Following previous post and observing in many projects how NSUserDefaults settings are handled I have decided to write this NSUserDefaults best practices.

If you look for information about how to set/get any value in the default storage you will get this:

and for getting the value:

Easy, Is not it? The problem appears when there are many attributes deal with with the sets and gets scattered along the code and you want set break points for trapping when this attribute is being accessed.

Proposal

What I propose is to wrap this functionality in a single class that concentrates all the attributes that we want to get/set from the default storage system.

This is the declaration .h:

Its implementation .m:

The class is implemented as a singleton and for each attribute are overwritten set (and get) methods. In that way in a unique place the attribute is set (and get) into (from) the storage system.

And its usage:

The access to the attribute is more clean and in a unique class file we have all the system storage functionality concentrated. You can download the project used for writing this post here.

 

 

 

 

Integrating FontAwesome on watchkit

The aim of this post is integrating FontAwesome on watchkit extension target. Font Awesome gives you scalable vector icons. This font has many advantages. First, a would dare to say that you will find the icon that you were looking for in its catalog. Second, that if you do not need more than one color, instead of placing an image resource you will be placing a character. Could y0u  figure it out how much space are you saving up?. And third, that you will not have problem on scaling because it is a vector image.

The base project

For setup the base project just follow the instructions from a previous post. You have to add the 3 necessary files for integrating font awesome, at the end of this post you will find the link to the project on GitHub.

Integrating FontAwesome on watchkit

 

And be sure that NSString+FontAwesome.m file is ready for the following targets.

targetsase

 

For both targets, be sure that:

1. In project properties>watchkitFontAweSome extension>BuildPhases>Copy Bundle Resources is included FontAwesome.ttf.

addfonttotarge

 

2. Add the following key array in the Info.plist file:

Fonts provided by application > Item 0 String FontAwesome.ttf

infoplist

 

You could also edit the source code and add the following xml snippet:

 Repeat the two steps for the other target.

Let’s code

Add a text label in the storyboard where we will print some icon with the new font:

interfacecontro

 

Link the label to an IBOutlet in InterfaceController.m

iboutlet2

Add the following snippet for adding some text to the label:

In my case I have decided to put it on the willActivate method from InterfaceController.m. Basically what I am doing here is setting an attributed text, where fa-cloud-upload is the name of the icon that I am interested in showing. Finally, I am setting FontAwesome for the first character of the string, the rest of the string is presented with the default font.

Run the target extension.

targetextension

 

 

If you did not missed any step you should have to see the following in the simulator:

demofont

As you can see the fa-cloud-upload symbol has been shown at the beginning of the text. You can also download this project from Github.

A simplified framework creation tutorial

The aim of this post is provide a simplified framework creation tutorial. This post is targeted to those who want to create a static library (framework) in iOS and never did it before.

Getting started

Start off by creating a new Cocoa Touch Static Library project.

A simplified framework creation tutorial

 

 

A simplified framework creation tutorial

 

Next step is add some basic code to the library. This is the header file:

… And that the implementation:

 

A simplified framework creation tutorial

 

The framework (library) has not been generated yet, you will see the libSimplifiedFramework.a in red.

This is an important issue, before compiling select the target SimplifiedFramework and the device iOS Device. Otherwise it will appear the message that says that compilation is successful but the library binary will not be generated.

A simplified framework creation tutorial

 

For generating the library binary, go to Xcode menu Product>Archive

A simplified framework creation tutorial

Now you should have to see that library is in black, that means that library has been successfully created.

A simplified framework creation tutorial

Universal libraries

Reached to that point the library is generated, but for a concrete platform. The target now is generate a multi-platform library. For doing that you have to create a new target

A simplified framework creation tutorial

 

 

A simplified framework creation tutorial

 

 

A simplified framework creation tutorial

 

 

A simplified framework creation tutorial

 

 

Select universalSimplifiedFramework target and in its project options add a new build phase.

A simplified framework creation tutorial

 

Now is time to add the script that will generate the multiplatform library, this script is extracted from another article that explains with more detail how to create a library.

Replace in Step 1, SimplifiedFramework by your project name.

A simplified framework creation tutorial

 

For building just select Product>Archive from Xcode menu:

A simplified framework creation tutorial

 

And check in the logs if all was fine.

A simplified framework creation tutorial

Client app

So as to check if the proper if all the process was carried on well, we will create a new app that will use the library. For doing that we will add a new target:

Go to Xcode menu and select File > New > Target :

A simplified framework creation tutorial

 

 

A simplified framework creation tutorial

 

 

A simplified framework creation tutorial

 

 

Once the app has been created, it is time to setup the project options that the app is going to use the library:

 

A simplified framework creation tutorial

 

Select SimplifiedApp target and in General section, unfold Linked Frameworks and Libraries and select add:

A simplified framework creation tutorial

A simplified framework creation tutorial

 

The following code will call the function defined in the library. Place that code in the view controller class:

And run the app target for any device, in my case I have used iPhone6 device.

A simplified framework creation tutorial

As you can see in the last screen shot the client library app is calling a function from the library without major problems.

Conclusion

As I said at the beginning of the post, this is a simplified vision of how to create a library. From that point you can find very interesting articles about how to create frameworks in Xcode, like:

Ernesto GarcíaCreating a static Librfary in iOS Tutorial.

Sam DaviesHow to Create a Framework for iOS