# This Wikibook

This is a guide and reference book of Swift programming language for beginners. Its goal is to give a picture of Swift and its technologies and where to start learning.It aims to show a basic overview of the concepts needed to create a basic application, but also to provide a reference for each concept, in where it is possible to find more specific information. This book is divided in sections:
* Basic Concepts: The language is described and main concepts of the Swift architecture are defined simply.
* Programming Environment: Is the introduction of the practical side and the description of the environment (Xcode) and suggestions for beginners to start programming.
* Tricky Section: The authors findings about Autolayout, upload to the AppStore and more.

# Basic Concepts
## What is Swift?

Here is a simple characterisation of the language:
* Is a programming language created by Apple as a substitute for ObjectiveC.
* First introduced at Apple's 2014 Worldwide Developers Conference.
* Designed to work with Cocoa and Cocoa Touch for Apple products.
For further information you can follow this link What is Swift?

## Main differences between Swift and Objective-C

* Syntax: Swift is more similar to high level languages such as Ruby or Python.If you are not familiar with this languages please start by reading this Quick Reference Guide.
* Generics: Functions that can work with any parameters. It is one of the most powerful elements and it is worth reading.
* Memory allocation: Swift doesn't need a separate memory allocation method to invoke like Objective-C does.
* Optionals: Are a nice alternative to Forced unwrapping of variables. It is recommendable that you read at least the first section of the documentation of optionals
* Swift is compatible with most of Objective-C libraries.

## Swift Navigation Architecture (Windows and Views)

Views are used to present the application in the screen, but these views are controlled in a hierarchy defined by Apple. To understand this, first you need to understand the components of this architecture. The following concepts are the basic knowledge that you would need to start developing, but at the end of every description there is a link to the full documentation of every term in case you need further information. Storyboard
* A storyboard is where you can create and modify graphically the flow of the program between the screens and create new elements. Storyboards
ViewControllers
* These are classes that manage the data and visual appearance of the screen shown by the application. When the UI is shown, the content is managed by one VC or more, that is why VC's are called the SKELETAL FRAMEWORK of the iOS apps.
* The programmer can create and customize VC classes to manage the application behaviour.
* The first View Controller of the app is called the Root View Controller.
* View Controller)
Navigation Controllers
* Controls the stack of VC for the navigation of the app and its flow.
* Contains and manages a set of View Controllers, each VC contains and manages a set of views; the purpose of the NC is to coordinate the navigation between the VC's.
* Navigation Controller
In summary, this hierarchy concept can be explained as follows: One Navigation Controller controls many View Controllers, and one View Controller can control many Views. Other concepts involved in navigation are:
Tab Bar Controller (TBC)
* Helps control the navigation of your application by managing ViewControllers directly.
* As the navigation controller, it has a stack of VC's and presents them independently and can be controlled directly by the TBC
* TabBars
Segues (pronounced "seg-way")
* The ways to connect and handle a transition from one screen to another are called Segues.
There are different types of segues used in XCode 6: →Push segue: Adds the new ViewController to the navigation stack, the new VC will become part of the Navigation Controller →Modal segue: The first VC presents the second VC, not including it in the Navigation Controller stack. To dismiss it, it needs the parent VC to take care of it. Apple has a short and understandable description of segues. Please take a look at it before you start an application to make the right choice of segue for your program.

# Programming

Swift is a new language, therefore it needs the latest update to run properly. Before you start, make sure you have this:
* A Mac with either Mavericks or Yosemite
* Xcode 6

## XCode

XCode is the developer toolset for Mac, iPhone, iPad, and Apple Watch. With this tool, you would be able to create and manage a Swift project, and also to run your application in a simulator.
It is a must that you learn how the pieces of an Xcode Project work together, but this comes with experience and interaction with XCode. Here is a nice graphical and written explanation):
* Navigator area: Where your project files and settings are displayed.
* Editor area: Center area where the files are shown.
* Utility area: Where tools and identities of the objects can be modified.
* Toolbar: Where the run button and the activity viewer are.
XCode Project Structure:
* XCode does not impose a defined structure to the applications code tree. But when you create a new project, three default folders are created: Your main application folder, the test folder and the product folder.
* You can also create Groups to organise your work or use folders to keep your program environment clean.

## Start Programming

Apple has a comprehensive documentation with tutorials included on their website. For starting out, here is a tutorial where basic concepts are explained along with images, examples, and concept definitions. The tutorial can be found here. It is a good way to start, but if you continue their series of tutorials, it would take a long time and can be boring and unnecessary. One of the most useful methods to learn a new programming language is to take a look at the examples available on any tutorial, as most of them are To-do List implementations that show how to connect Segues and return to the original View controller to display the information.
Jack Watson's Series of tutorials explain Swift's features, step-by-step, for simple applications. And hopefully a much quicker read!

## Adding elements to your project

Adding Libraries: For built libraries, Xcode has a 5 step procedure
Adding Files: Using Xcode you can just drag the files from the Finder Window and drop the into your project folder in the Xcode Navigation Area.

## Configuring for development

For programming in Xcode and running your app in the simulator, it is not necessary to have a certificate but you have to create an account anyways. But when you want to test the app in your own device, a developers certificate is needed and also you need to you install an associated provisioning profile on the device to enable the app to run. As it is specified in the site, the iOS Developer Program allows you to install your app on up to 100 iPad, iPhone or iPod touch devices.

## Configuring for deployment

Configuring your application for deployment is one of the trickiest parts of the development process, which is why you should follow and read every step in this guide. This process requires payed license for deployment. The developer license cannot be used to upload an application to the store; you must have at least one registered license for development on a device to be allowed to upload an app.

# Tricky Section
## Auto Layout

With Apple releasing more mobile products with different sizes of screen, the need for an implementation with just one setting for all screens is increasing.
The auto layout is a tool developers have been avoiding, but it is actually not that difficult:
First a quick introduction of Size Classes: The screen, views and view controllers in the Storyboard have Size Classes. This correspond to the orientation and screen of the target device, there are three values for the horizontal and vertical orientation:
* Regular Height and Regular Width
* Compact Height and Compact Width
* Any Height and Any Width
When developing for Any height and Any width will let you support all screen sizes. But relative constraints are needed to set the location of the elements in the View. Now, if you decide you don't want to develop for different devices please go to the next section. Otherwise, enable auto layout and start using its features! Proper guide is given by Ray Wenderlich . On this you may want to scroll down the page until you reach "Auto Layout to the rescue!", there is where the real explanation begins.

## Communication between View Controllers (Design Patterns)

One of the most confusing issues of Swift is passing the information through the ViewControllers, therefore here are three easy methods to do it:
* Singleton: A single intense of this class is created during the program run, it can be accessed using just the instantiation of the class in any other class. Information like web communication and login can be stored here.
* Observer (delegate): Is one of the most used patterns in Swift, a View Controller is set up as the observer, and when the observing class needs to handle back data, it could be a notification or a variable, the observing class calls an observer method. This is used usually when the View Controller that has the information is a child from the observer ViewController that needs the information.
* Send the data or object when calling the VC: Is the easiest way, when a Segue is called, the prepareForSegue method is called and the next View Controller can be accessed. This is used when one or few variables present in one View Controller are needed in the next one. The code would be as follows:

## Manage of Segues

Segues are the responsible for the program flow, therefore you should pay attention to the correct way to handle them:

### Create-Call segues

Segues can be called programmatically or using XCode´s assistant, segues are created using Xcode assistant, all you need is to select the origin View, press the ctrl key and drop the line that shows in the second View, then a popup menu will show and you can select the type of segue you want. By clicking on the segue, a name can be given to it on the Utility Area.
* Programatically, that segue can be called like this: (If you use this approach, the parent View Controller is the responsible for dismissing the child View Controller and not the Navigation Controller)
* From the Xcode assistant, a segue can be assigned to start depending on an action in an element of the view, for example, if instead of selecting a whole view, you select a button, press the ctrl key and drop the line on the next View Controller, the segue will be called when the button is pressed.

### Dismiss a Segue

This is an important step if you want to take care of your applications memory usage because it will pop out the View from the stack, releasing the memory the View Controller was holding. The dismiss action can be handled by the NC or programmatically.
* By the Navigation Controller: Generally a regular "Show" segue can be closed automatically by pressing the back button the appears in the navigation bar in the top of the screen, the NC closes the View Controller.
* By code: It is necessary to pop out the ViewController form the stack to be shown. An example of a call to pop all the View Controllers is the following:

# Next Steps

So, now that you know the basics, you can start implementing application using popular features like the camera or QR Code Readers. In the next table you can find nice tutorials about this topic and more.

## Other worth to see Web Sites

| Topic | Description | link |
| Graphical User Interface | Here concepts from the elements of a view, to auto layout are covered | link |
| Long term tutorial | This is a well known, free and for students, tutorial recently written for Swift | link |
| Camera tutorial | Describes how to take control of the camera in Swift | link |