Book Image

Swift Cookbook

By : Cecil Costa, Cecil Costa
Book Image

Swift Cookbook

By: Cecil Costa, Cecil Costa

Overview of this book

Table of Contents (18 chapters)
Swift Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Starting a Swift project


Usually, starting Swift is something that is very straightforward; however, it is good to know what is going on in every step.

Getting ready

Before you start a project, make sure that you know your project name and in which folder it will be saved. Changing this kind of parameter can create problems after the project has been created. Once you have Xcode installed, you can open it from your application folder, from the Launch pad, or even from your dock if you have added Xcode on it. As I'm a very lazy person, I prefer the latter; it's faster for me to have it on my dock.

How to do it...

The first time you open Xcode, it's possible that it will ask to install some additional packages, so do it. Some of these packages are important, depending on the type of application you are developing, and some of them are necessary to have access for some devices, mainly the newest ones.

  1. Now, Xcode is asking you about the project you want to start or open. Check the option that says Create a new Xcode project.

    If, for any reason, this window is not shown to you, there is always the option to go to File (on the menu bar) | New | Project.

  2. The next step is to choose the type of project that you want to develop. To do this example, I will use a Single View Application for iOS, but I'll make comments if there is anything different on OS X applications, or for another type of project.

    The next dialog will ask you for some project information, one example being the programming language that you want to use. In this example, we will use Swift.

  3. Choose Swift as language and it will create the application with its delegate, with Swift code. Make sure that Core Data is unchecked to prevent having its code on the app delegate.

    You will also notice that Swift iOS applications now have no files called main.m, main.mm, or main.swift. OS X apps have a main.swift file, but it is smaller than the previous main.m file.

    As you should already know, the product name is your application name, the organization name is the proprietary of this software, and the organization identifier is the reversed Internet domain, for example, uk.co.packtpub instead of packtpub.co.uk.

  4. Note that now there is no checkbox for creating unit tests because by default, it is created for you using XCTest. If you don't want it, just remove the group from your project. I wouldn't remove it, it usually doesn't hurt.

  5. Now, it's time to choose a folder to store our project. Remember that during the development, you can add files, which will be stored in different locations. I don't recommend this kind of practice, but if you have to do so, try to have your project close to these files.

  6. I also recommend you to check the option to use a Git repository, except if you have a subversion repository, of course. Even if you are the only developer, it's important to have a version control system. Remember that we are humans, and sometimes, we make mistakes and so have to go back.

  7. Once you have the project created, press the play button to see it working. If it's the first time you have installed Xcode, it will show you a dialog asking you to enable the developer mode. Click on the Enable button if you have the administrator password.

  8. Ok, now you have your project up and running.

How it works...

Creating a project is not something difficult; you only need to pay attention at some steps. Make sure that you have selected Swift as the main programming language; otherwise, you will see a lot of stuff with Objective-C.

Pay attention to the folder where you will create your project. Xcode will create another folder with your project name, and inside of it, Xcode will create the project bundle, a folder with the source code. If you want to copy your project, make sure that you copy the folder that contains everything.

There's more...

If you want to work on a team that already started a project, you probably will clone the project using the Check out an existing project option. You will use a Git or a subversion repository, and you will have your code synchronized with the other members of the team. Xcode offers us the basic tools to work with a VCS (version control system); these are enough for 80 percent of our tasks.