Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Swift Cookbook
Swift Cookbook

Swift Cookbook: Over 50 hands-on recipes to help you create apps, solve problems, and build your portfolio of projects in Swift

eBook
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Swift Cookbook

Chapter 1. Getting Started with Xcode and Swift

In this chapter, we will cover the following topics:

  • Installing Xcode from the App Store
  • Downloading the Xcode image
  • Starting a Swift project
  • Using Swift project options
  • Creating a conditional code
  • Adding a developer account
  • Compiling from the command line
  • Using Swift as an interpreter
  • Adding a control version system to an existing project

Introduction

In this chapter, we will learn the basics of creating a project with Swift. Even if you have already created your own project, it is worth reading the recipes of this chapter. You will learn how to interact with Xcode, how to test your code from the command line, and at the end, we will review the basics of this language.

Before downloading Xcode, you have to know that you can't use Swift with any version of Xcode; it must be 6.0 or higher. To install the version 6 of Xcode, you must have at least OS X Mavericks (OS X 10.9), so meet these requirements before installing it.

Installing Xcode from the App Store

The first way of installing Xcode is by downloading it from the App Store. The advantage of this method is that you will be warned about updates, and the system requirements will be checked before the download starts.

Getting ready

To download any program from the App Store, you must have an Apple ID; it's free and it won't take long to set up.

How to do it...

  1. To download Xcode from the App Store, just open App Store from your dock or your applications folder.

    How to do it...

  2. The first time you open the App Store, it will ask you for your Apple ID details (email and password). After opening this application, just search for xcode in the textbox, which is located on the upper-right corner of this application.
    How to do it...
  3. Make sure that you are installing Xcode from the right vendor (Apple); sometimes, we get results that lead us to think that they are what we want but they aren't.
  4. Once you have found the Xcode app, just click on the Install button, and the next step is to go for coffee, or you can call a friend, because Xcode is 2.2 gigas, which means that it will take a while to download, so take a break now.
  5. To check if Xcode has finished the installation, you just need to open the Applications folder or the Launchpad app, then have a look to see whether there is any progress bar under the Xcode icon.

How it works...

Like any other application that you will install from the App Store, you only need to open the App Store application, search for it, and install it.

There's more...

If you buy a new computer, you will see that Xcode will be offered to be installed into your new machine. This is because Apple keeps track of the applications you've already installed.

Downloading the Xcode image

The second way of installing Xcode is by downloading an image from the Apple Developer Center; this step is not free, it's only available to members of the Apple Developer Program (which costs approximately $99 a year) or for people who work in companies that are members of this program.

Getting ready

For this recipe, you will need to have 2.2 gigabytes of free space besides the space stored by the installed Xcode, but I will assume that you won't have this problem.

The advantage of downloading a DMG file is that you can save it onto a DVD as a backup (you never know when this version of Xcode will be removed from the App Store). Also, if you work in a team, it is very important to have every member working with the same Xcode version. Also, if you want to install any beta version of Xcode, it will only be available through the Apple Developer Center.

How to do it...

To download the Xcode image, follow these steps:

  1. The first step is to open your web browser; go to http://developer.apple.com, log in where it says Member Center. If you are a member of more than one team, it will ask you which account you want to use. Choose the most appropriate one.
  2. After this, you will see a table with some options, choose iOS Dev Center or Mac Dev Center.
    How to do it...

    Tip

    You can download Xcode from iOS Dev Center, even if you are going to use it for OS X development or vice versa

  3. After downloading the DMG file, double-click on it and drag the Xcode icon into your Application folder. Remember that you need the administrator's permission to copy files into the Applications folder.
    How to do it...
  4. You can also install Xcode onto a different path like your home directory, but I wouldn't recommend this if it's not necessary.

Tip

Don't search for download swift on your search engine as there is another programming language that is also called Swift, which has nothing to do with Apple devices.

How it works...

The DMG file ensures that you can always have a backup of this Xcode version, so if for any reason, you have problems with future versions of Xcode, you can install the previous version. It is also possible to have more than one Xcode version installed onto your computer.

There's more...

The Apple Developer Center is a good website to get Xcode resources. Here, you can find videos, guides, code samples, and addons.

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.
    How to do it...

    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.
    How to do it...

    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.

    How to do it...
  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.
    How to do it...
  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.
    How to do it...
  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.

Using Swift project options

Xcode projects comes with lots of options. Here, we will know some of them, mainly the Swift-specific ones.

Getting ready

To do this recipe, just create a new project, as shown in the previous recipe.

How to do it...

Follow these steps for changing some Swift options:

  1. Once you've created a project, click on the navigator project icon or press command + 1 if you prefer a keyboard shortcut, then click on your project icon (the first icon). Now, click on Build Settings.
    How to do it...
  2. Look for Embedded content contains swift code; In this case, we will select NO, but of course, if you know that there is any extra content created with Swift, you should select YES.
    How to do it...
  3. Go to the General tab and scroll down; you can see where you can add the embedded binaries.
    How to do it...
  4. Now, look for Optimization Level. Here is where you tell the compiler how much time it should expend trying to make your code faster or compressing it. Usually, when we are developing (debug mode), we set for no optimization (-O0); however, when we are going to create the final product (release mode), we will usually set an optimization level such as-Os, which means fastest and smallest.

    Note

    Sometimes, with Objective-C, when you used to set a high level of optimization, the debugger used to loose some of the variable values. I haven't seen this phenomenon with Swift yet, but it's good to have it in mind.

  5. Another important option is Import paths. This tells Swift where it should look for Swift modules. If you are linking your project with external libraries, you may need to specify where the module.map file is. If you have more than one path to search, you need to set them one per line. If you have different paths for debug and release, you can still use variables such as $(CONFIGURATION) or $(TARGET).

Tip

You can use absolute or relative paths, but I would give preference to the relative ones.

How it works...

Changing settings is something that you have to do mainly when your project starts growing. There are some options that you set differently for debug and release configurations.

There's more...

Xcode has a lot of configuration settings; showing all of them would be out of the scope of this book. I recommend that you at least look at some of them, mainly if you want to work with big projects. My main recommendation here is: do not change your settings without synchronizing with the other members of your team (mainly with the project manager). If you cause a conflict with the VCS, it could be hard work to fix it.

Creating conditional code

Usually when we are developing, we have some cases where we would like to have different pieces of code according to our needs. For example, let's imagine that we would like to compare the performance of some functions written by us with some equivalent functions that were created on a third-party library. In this case, we can create some macros for using only our functions or for using only the third-party functions, allowing us to have the same application working in two different ways.

In this recipe, we will show how to create a log according to a platform and we can also enable or disable it if the execution is being affected by the excess of logs.

Getting ready

Create a new project called Chapter 1 Conditional Code, as shown earlier, and let's code a little bit.

How to do it...

To create a conditional code, follow these steps:

  1. After creating a new project, let's create a new file by navigating to File | New | File.... Now, choose Swift File and name it CustomLog.swift.

    Tip

    Don't save your files on a different folder from the project; this will give you problems in the future.

  2. Now, add the following code:
    func printLog(message: NSString){
        #if VERBOSE_LOG
            #if os(OSX)
                let OS="OS X"
            #else
                let OS="iOS"
            #endif
            
            #if arch(arm) || arch(arm64)
                let devicetype = "Mobile device"
            #elseif arch(x86_64) || arch(i386)
                let devicetype = "Computer"
            #else
                let devicetype = "Unkown"
            #endif
            
            NSLog("%@ on a %@ - %@", OS, devicetype, message)
        #endif
    }
  3. Now, go to the viewDidLoad method of your view controller, and add a call for this function, like this:
    printLog("Hello World")
  4. Try pressing play now; what do you see? The answer is—nothing! The reason is that the compiler knows nothing about the macro VERBOSE_LOG, which means that this macro is interpreted as false and the only thing that is created is an empty function.
  5. Now, go back to your project build settings, search for other swift flags, and add -DVERBOSE_LOG, as shown in the following screenshot:
    How to do it...
  6. Click on play again and you will see the log message.

How it works...

Currently, the Swift compiler has two defined macros: os() and arch(). The first one can receive OSX or iOS as argument, and the second one can receive x86_64, arm, arm64 and i386. Both macros will return a Boolean value. You can also create your own macro, defining it on your build settings.

The block that is evaluated as true will be compiled, and the other blocks will not be compiled; this way you can have code that calls OS-specific functions.

I would like to emphasize, mainly for those developers who are used to working with C projects, that the Apple documentation leaves a very clear message that Swift has no preprocessor; it only uses a trick on compilation time, so you can't use macros as we used to do on C or even on Objective-C. The only thing you can do is watch to see whether they are set or not.

There's more...

If you need, you can use the operators &&, ||, and ! as shown here: #if arch(arm64) && os(iOS), but you can't use any kind of comparator operator such as ==, <, and so on.

If you are interested in knowing more options that you can add to other Swift flags, check out the Compiling from the command line recipe in this chapter.

Adding a developer account

Usually, Apple tries to make the developer's life easier by improving Xcode and creating tools, but there is an exception when we talk about certificates. If you want to test your app on a physical device (iPhone, iPad, or iPod), you need a certificate. If you would like to upload it onto the App Store, you also need this certificate.

The idea of a certificate is to protect your code from malicious code or from being modified after being signed, but this idea has a price. To get a certificate, you will need to be enrolled on the Apple Developer Program.

Getting ready

I will assume that if you continue with this recipe, you are already enrolled on this program. Let's recycle the previous project; open it, and let's start.

How to do it...

Follow these steps to add an Apple developer account:

  1. Once you've opened the project, click on the project navigator, then click on the combobox that shows our project, and select the target Chapter 1 if it's not selected yet.
  2. Now, have a look at the option called Team. In the case of programming a Mac application, this combobox is enabled only if you select the signing option to Mac App Store or Developer ID.
  3. Usually, the team option starts with None selected. Click over this combobox and select Add Account.
    How to do it...
  4. After selecting to add an account, Xcode will ask for your Apple Developer Program login data (e-mail and password). If you don't have it, you have the option to join the program.
    How to do it...
  5. Once you've added this, you are supposed to use your account and run your app. If you have a device attached to your Mac, you can go to the Window option on the menu bar, and then you can select the Devices option.

    Your device should appear on the dialog, Xcode could take a while to read the device's symbols. In case of having this device attached for the first time, you will see that you will have to ask to change the status of this device to developer mode.

  6. When you get the green light, it means that your device is ready to be used for development; now, go back to your project and change from the simulator to your device.

    If the device is enabled, but not listed by Xcode, it could mean that you have to decrease the iOS Deployment Target, which can be found on the project setting, under the Info tab.

    How to do it...

Tip

Lowering the iOS Deployment Target to the minimum value is an idea very common among programmers to cover the maximum sort of devices. Doing this will prevent your development from using new features. Check out the features that you need first, and then change your iOS Deployment Target.

How it works...

Signing a code is something done for security; the main restriction is that you must be up to date with the Apple Developer Program. Apple allows having up to 100 devices per account.

There's more...

Sometimes, the certificate gives us some headache; take care if it asks you to revoke your certificate; you may have to create a new one on the Apple Developer Center, and if you are working on a team, you may have to wait for the administrator's approval.

There are a few times that you need to change the code signing option on the build settings; it happens mainly when you get code from another organization ID.

Testing your code on a device is something that is very useful; it's where you can test the real user experience. Whenever you have some low-level code, such as assembly code or something written in C language that uses type sizes or byte orders, it's good to test your project on a device. Remember that Apple's devices have CPUs based on ARM and ARM64, which are different to the Intel CPU that is used on Mac computers.

Compiling from the command line

I know that nowadays a lot of users and even developers think that using the command line is something from the past. The reality is that even today a lot of tasks that can be done from the command line, mainly automations tasks such as continuous integration, must be done using the command line.

This recipe will show you that it's not difficult, and better than this, you will have a better understanding of the concept about what Xcode does behind the scenes.

Tip

If you've never worked with a command line, I would suggest you read a book about it; Linux Shell Scripting Cookbook, Packt Publishing, is a good one in my opinion, even knowing that some commands are Linux specific.

Getting ready

Open a Finder window using the key combination command + Shift + U or open your Launchpad and click on the others folder. Here, you can see an icon called Terminal, open it and you should see a window similar to following one:

Getting ready

How to do it...

Here is how you can compile from the command line:

  1. Just type xcode-select -p; this should give to you one path, for example, /Applications/Xcode.app/Contents/Developer, If you don't have more Xcode versions installed in your machine, you shouldn't worry about the path, it will probably be right. If for any reason you have more than one Xcode installed on your machine, you will need to change it by typing xcode-select -s /Applications/XCODE VERSION.app/Contents/Developer.

    Note

    Remember that switching Xcode is a task that can only be done by an administrator, and it will affect every user.

  2. Now, go to your project directory and type xcodebuild -target "Chapter 1" -configuration Debug. After this, you will see lots of commands on screen, but the most important message is the last one that should be ** BUILD SUCCEEDED **; which means that the project was built without errors.

How it works...

When you type a command, your system will look for this command using the paths specified by the PATH variable. You can check the directories included in your PATH variable by typing echo $PATH . By default, the directory /usr/bin is included.

This directory contains Xcode commands, such as xcodebuild. When you want to use commands from other Xcode version, you need to use xcode-select to overwrite these files to use the ones according to the version you want.

Once you have set it, you can compile your project. As your project is a set of lots of files such as source codes, images, and so on, it would be hard work if we had to do every single action (compiling, copying files, code signing, and so on) one by one. This is the reason it's easier to ask Xcode to do it by himself using the command xcodebuild.

The xcodebuild command has a lot of parameters, so you can specify the configuration to be Debug or Release, the target you want to compile, as many other options. Type xcodebuild -help to get a list of options.

Tip

The -help argument is very common on Xcode commands. Try to use it when you have any doubt.

There's more...

Another good feature about the xcodebuild command is that it shows the commands that are being used with all its arguments. So, you can appreciate that when you compile an Objective-C project, Xcode uses the clang compiler, but when you have a Swift project, Xcode uses the swiftc command. Type swiftc -help with its full path to check its options and use them into the build options of other Swift flags.

Keep in mind that xcodebuild is going to look for a file called project.pbxproj, which is inside your .xcodeproj directory. This file contains information of every file, settings, and steps to create a project; in case of wrong syntax or wrong references, xcodebuild and the Xcode IDE won't compile the project at all. In addition to this fixing, this file could be a hard work. Because of these reasons, I wouldn't change this file manually, and also, I would try to avoid conflict with the Version Control System.

Using Swift as an interpreter

Like some other script languages, you can use Swift with its interpreter on the command line. Sometimes it's very useful, mainly when you want to test code but you don't want to create a new playground.

Getting ready

Open a terminal window as it was shown in the previous recipe.

How to do it...

Follow these steps for using Swift as a command line interpreter:

  1. The first step is to find where the Swift command is; even if you have used the xcode-select command, it is possible that the Swift command is not accessible from your PATH variable. So, you can localize your Swift command using find /Applications/Xcode.app -name swift -a -type f. In my case, I got /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift. However, current versions of Xcode have the Swift command at /usr/bin. If it is necessary, add this directory to your PATH variable with the command export PATH="$PATH:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/". At this moment, we can enter into the Swift interpreter just typing swift.
  2. If you want to use Swift from the command line, sometimes, it's a good idea to have this PATH variable set permanently. To make this, we need to add the previous command into our .profile file, such as echo 'export PATH="$PATH:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/" ' >> $HOME/.profile && chmod +x $HOME/.profile. From now on, if you restart your computer, it won't be necessary to look for the Swift path and set the PATH environment variable again.
  3. Now, let's enter into our Swift command line and type the following code:
    var dividend = [3,2,1,0]
    var divisor = 6
  4. You will see a message showing the content of these variables after typing each of them. Now, type the following loop code:
    for i in dividend { 
          println("\(divisor) / \(i) = \(divisor / i)")  
    }
  5. Now, you can see that we will receive the following result:
    6 / 3 = 2
    6 / 2 = 3
    6 / 1 = 6
    Execution interrupted. Enter Swift code to recover and continue.
    Enter LLDB commands to investigate (type :help for assistance.)
  6. As you can see, the last option failed because we can't divide by 0 and that's a fast way we can test some code, using the command line. Most of the time, we will test using a playground, but sometimes using the command line is much faster.

How it works...

Calling the Swift command gives you the possibility to test your code or even use Swift as a scripting language. The highlight here is that you need to know where your Swift command is; the command line helps you to find it.

There's more...

Most of Swift's options and swiftc options are common; it means that if there is something that you would like to test before compiling, you can do it.

Adding a control version system to an existing project

It's very common starting a project without any version control, and with the passage of time, we change our idea and decide to add one.

Unfortunately, Xcode doesn't give this option to us and we have to do by hand. I hope this option will be added on Xcode soon.

Getting ready

To do this recipe, let's create an empty project called Chapter1 Git; however, this time before we save the project, uncheck the option Create Git CGRepository on.

Getting ready

How to do it...

Follow these steps for creating a local repository on an existing project:

  1. Open a Finder window again and use the shortcut command + Shift + U, or open the utilities folder from your Launchpad. Now, open the terminal and go to your project folder.
  2. Change your folder using the terminal by typing cd followed by a white space. Now, without closing your terminal, open the Finder window, then go to your project folder, drag the folder from the title bar to your terminal window, and then press the Enter key.
    How to do it...
  3. Now, just type the following commands:
    git init
    git add .
    git commit -m "Initial commit"
  4. Now, you can open your project and notice that Xcode already recognizes the VCS. If you want to be sure of it, modify a file, save it with command + S and check that you have the letter M on the right side of your file on the project navigator.
    How to do it...
  5. Once you are happy with your changes, you can deliver them by right-clicking on AppDelegate.swift, then go to the Source Control option and select Commit AppDelegate.swift.
    How to do it...
  6. Then, a dialog asking for a description will appear, write about your modification as comment, and click on commit 1 file.

How it works...

Unfortunately, if you forgot to add a Git repository to your project, Xcode doesn't provide any mechanism to add it to your project, so you have to add it by hand. Opening the command line allows you to use Git from the command line and Xcode detects that this feature has been added. Some versions of Xcode can only detect that the version control has been added when you open your project, so if you've done all steps and Xcode hasn't detected it, try closing and opening Xcode again.

Tip

Xcode offers you some features to work with Git and SVN, but they are very limited. If you need more commands from your VCS, you can use them from the command line, or use an external tool for it.

There's more...

Even if you are not going to work as part of a team, I recommend that you use a version control system. When developing with Swift or other languages, you sometimes need to rollback or compare the current code with the previous versions of it, mainly when you have a new bug.

If you would like to know more on this topic, check out the book Git Version Control Cookbook, by Packt Publishing.

Left arrow icon Right arrow icon

Description

If you are an experienced Objective-C programmer and are looking for quick solutions to many different coding tasks in Swift, then this book is for you. You are expected to have development experience, though not necessarily with Swift.
Estimated delivery fee Deliver to Malta

Premium delivery 7 - 10 business days

€32.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Apr 30, 2015
Length: 392 pages
Edition : 1st
Language : English
ISBN-13 : 9781784391379
Vendor :
Apple
Category :
Languages :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Malta

Premium delivery 7 - 10 business days

€32.95
(Includes tracking information)

Product Details

Publication date : Apr 30, 2015
Length: 392 pages
Edition : 1st
Language : English
ISBN-13 : 9781784391379
Vendor :
Apple
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 125.97
Swift Cookbook
€41.99
Swift Essentials
€41.99
Mastering Swift
€41.99
Total 125.97 Stars icon
Banner background image

Table of Contents

12 Chapters
1. Getting Started with Xcode and Swift Chevron down icon Chevron up icon
2. Standard Library and Collections Chevron down icon Chevron up icon
3. Using Structs and Generics Chevron down icon Chevron up icon
4. Design Patterns with Swift Chevron down icon Chevron up icon
5. Multitasking Chevron down icon Chevron up icon
6. Playground Chevron down icon Chevron up icon
7. Swift Debugging with Xcode Chevron down icon Chevron up icon
8. Integrating with Objective-C Chevron down icon Chevron up icon
9. Dealing with Other Languages Chevron down icon Chevron up icon
10. Data Access Chevron down icon Chevron up icon
11. Miscellaneous Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(1 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
William C Turner May 26, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a book that I was waiting for. I had preordered it. When it was released I was not disappointed. Like all the books I published by Packt Publishing, it is well written, and has an excellent presentation. The subject matter is presented in an understandable manner with excellent examples and code that really works. With a new language like Swift, there is a real need for training that addresses real world examples. This is such a book. Cecil Costa knows his subject and more importantly he knows how to present it in clear examples that are complex enough to have meaning. The book is fascinating and I found I learned something with nearly every page. The book will also serve as a reference book that I know I will go back to many times for clear examples of best practices. I highly recommend this book for anyone wishing to develop apps for IOS using Swift. It is reasonably price and tremendous value for money.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact [email protected] with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at [email protected] using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on [email protected] with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on [email protected] within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on [email protected] who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on [email protected] within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela