Xcode 7 Essentials - Second Edition - Brett Ohland - E-Book

Xcode 7 Essentials - Second Edition E-Book

Brett Ohland

0,0
34,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Step up your iOS development with the power and wealth of features of Xcode 7

About This Book

  • Updated for the latest Xcode 7 release, this is the first book that will show you how to use all the new features available in Xcode 7 to their fullest
  • This guide to will get you up and running with the entire workflow to develop Apple apps without previous Xcode experience
  • Packed with plenty of tips on how to use Xcode with Swift 2 and other existing Apple frameworks to develop robust apps at a rapid pace

Who This Book Is For

If you are a novice programmer who is familiar with the concepts of object-oriented programming, but have little to no knowledge of Swift, Xcode, or Apple's Cocoa APIs, then this book is for you. Starting with an introduction to the basics of Xcode and the Swift programming language, you will learn all of the skills that are essential to build an app and submit it to the App Store

What You Will Learn

  • Get an introduction to Xcode and get to know how to navigate and use the tool
  • Build playgrounds to learn and explore the environment
  • Create an adaptive UI with the interface builder that will help your app to scale through a wide range of devices
  • Understand Table, Collection, and Stack views and find out how they can be implemented
  • Create custom components for your application through frameworks and live previews
  • Exploit Xcode's advanced features to elevate your debugging and testing capabilities
  • Run your application on the physical device and submit it to the App store

In Detail

Apple's Xcode technology is making the development curve smoother than it has ever been with its easy-to-develop features and enhancements. With the latest release of Xcode 7, Apple has also added great support for Swift development.

This book will introduce you to all the new features of Xcode 7 and demonstrate how Swift programming can be much easier, faster, and simply better with Xcode!

Even if you're starting with just a little knowledge of Swift or Xcode, you will learn the basics of the language as well as the tool. You will then use this knowledge to create simple applications and will learn how to debug and optimize your code. At the end of this book, you would have learned enough to build, run and submit your very own application to the App Store.

Style and approach

This is a step-by-step guide, full of practical and real world examples, to help you get to grips with Xcode with ease. A demo project is also provided at the end of the book, where you can test your skills.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 228

Veröffentlichungsjahr: 2016

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Xcode 7 Essentials Second Edition
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Introduction to Xcode
Requirements and getting Xcode
Features of Xcode
What's new in Xcode 7?
Sideloading
Swift 2
Playgrounds
Testing
Crash logs
Summary
2. A tour of Xcode
Starting Xcode
Creating a new project
Choosing a template
Choosing the project options
Setting the project properties
Xcode overview
The toolbar area
The navigator area
The editor area
The Standard editor
The Assistant editor
The comparison editor
The utilities area
The debug area
Development languages
Summary
3. Playgrounds
Before we start
The storyboard workspace
Learning Swift
Working with constants and variables
Value types
Working with strings
Working with numbers
Working with Booleans
Adding different types
Collection types
Array type
Creating arrays
Accessing items
Modifying arrays
Loops and iteration
Dictionary type
Creating dictionaries
Accessing items
Modifying items
Loops and iteration
Set type
Creating sets
Accessing, modifying, and iterating through a set
Set operations
The Any and AnyObject types
Optionals
Unwrapping optionals
if let
guard let
Unwrapping multiple optionals
Optional chaining
Tuples
Accessing items
Conditional statements
The if statement
The switch statement
The guard statement
Loops
For and for in
While and repeat-while
Breaking or continuing
Functions and closures
Returns
Parameters
Closures
Classes and structures
Classes versus structures
Extending classes and structures
Enumerations
Error handling
Propagating errors
Handling errors, do-catch and try?
do catch
Try?
Asserting errors
Defer statements
Summary
4. Interface Builder
Model View Controller
Understanding Interface Builder
Views and View Controllers
Adding elements to your view
Adaptive UI
Size Classes
Auto Layout
Debugging Auto Layout
Tweaking values
Pinning
Relative positioning
Adding more views
Navigation controllers
Connecting views and controllers
Subclassing UIViewController
Understanding subclassing
Creating connections, outlets and actions
Different elements in different Size Classes
Layout previews
Managing connections
Adding gesture recognizers
Summary
5. Table, Collection, and Stack Views
Plain views versus Table, Collection, and Stack views
Static table views
Dynamic table views
Data sources
Delegates
Bringing it all together
Collection views
Starting the example
Creating a custom UICollectionViewCell
Stack views
What about OS X?
Summary
6. Custom Controls
Introducing Custom Controls
The basics
Starting an example project
Creating a framework
Creating a class
Properties
IBDesignable and IBInspectable
Advanced drawing with CALayer
Debugging Custom Controls
Summary
7. Debugging
Breakpoints
Listing breakpoints
The debug area
The variables view
The console area
The debug navigator
Quick Look
Debugging the view hierarchy
Dealing with crashes
Summary
8. Testing Your Code
Test-diven development
Testing in Xcode
The test navigator
Test classes
Test methods
A simple calculator example
Assertions
Writing our first real functional unit test
Performance tests
UI tests
Running tests and collecting results
Code coverage
Summary
9. Sideloading, Optimizing, and Submitting Your App
iOS simulators
Simulating different devices
Simulator limitations
Sideloading
Getting an Apple ID
Using an Apple ID with Xcode
Registering a device
The last steps
Optimizing your app
App thinning
Slicing
Bytecode
On-demand resources
Submitting your app to the App Store
iTunes Connect
Uploading a build
Submitting your app for review
App review
App rejection
Approval — ready for sale
Summary
A. Everything Else
TestFlight
Beta App review?
Submitting your app
Installing TestFlight builds
Submitting for final sale
Crash logs
Storyboard references
Creating a new Storyboard
Refactoring existing views
Summary
Index

Xcode 7 Essentials Second Edition

Xcode 7 Essentials Second Edition

Copyright © 2016 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: January 2015

Second edition: February 2016

Production reference: 1020216

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78588-901-1

www.packtpub.com

Credits

Authors

Brett Ohland

Jayant Varma

Reviewer

Jeffrey Huang

Commissioning Editor

Wilson D'souza

Acquisition Editor

Sonali Vernekar

Content Development Editor

Kirti Patil

Technical Editor

Naveenkumar Jain

Copy Editor

Vikrant Phadke

Project Coordinator

Nidhi Joshi

Proofreader

Safis Editing

Indexer

Hemangini Bari

Production Coordinator

Shantanu N. Zagade

Cover Work

Shantanu N. Zagade

About the Authors

Brett Ohland develops software, likes to tinker and fix things (especially bikes), and keeps himself and others caffeinated. He has well over a decade of experience of converting keystrokes and staring at the ceiling into functional applications for clients.

He started his career by building Flash websites when those were still an amazing thing, transitioned to web development when everyone was loving JavaScript, and then moved on to iOS development so that he could build his dream photography app. Throughout this period, Brett has worked for major advertising agencies, consultancies of all sizes, a stock photo agency, two guys in a basement, and himself for a while. Several teaching opportunities have allowed him to share his knowledge at start-up incubators and even a university.

Currently, he writes about his technological discoveries and interests at ampersandsoftworks.com when he isn't riding his bike, chasing his toddler around the house, or pulling a shot of espresso.

Jayant Varma is a technophile with a career spanning more than two decades and was introduced to computing in the days of 8-bit computers and Z80 chips. While managing the IT and telecom department at BMW's dealerships in India and Oman, and Nissan in Qatar, he worked extensively on Windows, AS/400, and Unix. His love for traveling inspired him to work and travel to several countries. He is currently based in Australia.

Jayant's technological journey began as a Microsoft technologies developer and then diversified. His focus is now on Apple and mobile technologies. He has a master's degree in business administration and IT from James Cook University, Australia. He also lectured at James Cook University and coordinated the onshore and off-shore teaching of Linux/Unix administration. Jayant has worked closely with the Australian Computer Society (ACS) and Apple University Consortium (AUC) on workshops and projects.

He authored the book Learn Lua for iOS Game Development, Apress, and has also been a technical reviewer on several titles. As a founder, consultant, and developer at OZApps (www.oz-apps.com), he helps organizations and individuals integrate technology into their businesses and strategies. He also conducts training sessions and workshops, and writes blogs to share his knowledge with the community.

About the Reviewer

Jeffrey Huang is an iOS developer and App maker. He has one published app on the iOS app store. He also has a YouTube channel that teaches iOS and electronics (https://www.youtube.com/user/JeffreyApp). Recently, Jeffrey also spearheaded a new initiative, Chicago Technology for Kids, to promote fun technology learning among kids.

www.PacktPub.com

Support files, eBooks, discount offers, and more

For support files and downloads related to your book, please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

Why subscribe?

Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

Free access for Packt account holders

If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.

Preface

Welcome to Xcode 7 Essentials. Developing apps for desktop and mobile computers has never been easier for anyone to do. The technical and commuting requirements are such that practically anyone can make and release an app to millions of users. With well over a billion iOS devices and nearly half a billion Macs in people's homes and businesses, there is a real market for any piece of software, no matter how specialized. Apple has fostered this market by continually updating Xcode, their free development tool, to keep it modern, easy to use, and powerful.

This book will be covering Xcode 7, the current version of the app, as well as the Swift programming language. The book starts with a basic tour of Xcode, takes you through many of the features of making the app, and ends with how to submit your app to the app store of your choice using a combination of theory and step-by-step guides.

What this book covers

Chapter 1, Introduction to Xcode, introduces Xcode and shows you how to find, install, and set it up for use.

Chapter 2, Tour of Xcode, takes a deeper look at the tool itself. Here, you become familiar with the interface and its features.

Chapter 3, Playgrounds, introduces Playgrounds, an interactive environment for learning and experimenting with the Swift programming language. This chapter will then walk you through many of Swift's features and its syntax to help you express yourself in code.

Chapter 4, Interface Builder, covers Interface Builder and Storyboards, Xcode's tools for creating user interfaces by dragging and dropping elements on a screen.

Chapter 5, Table, Collection, and Stack Views, uses the knowledge gained in the previous chapters and shows you how three advanced view types will let you build complicated-looking layouts quickly and easily.

Chapter 6, Custom Controls, shows you how you can create custom, live-updating controls for your applications, and how to easily share them across projects as a framework.

Chapter 7, Debugging, shows you the tools that Xcode provides to help you become a great detective and exterminator of bugs and crashes in your code.

Chapter 8, Testing Your Code, introduces the concepts behind test-driven development and the unit test and UI testing tools that Xcode provides to help you write great code.

Chapter 9, Sideloading, Optimizing, and Submitting Your App, shows you how to run your app on a physical device, how it can be optimized, and the steps necessary to submit it to the app store of your choice.

Appendix, Everything Else, covers more advanced topics. These include distributing beta versions of your app using TestFlight, collecting and reading crash reports submitted to you by those testers, and how to simplify your user interfaces with Soryboard references.

What you need for this book

To be able to run the code and work through the chapters step by step, you will need the following:

An Apple computer running OS X 10.10.5 or higherXcode 7.x installed on this Apple computer

Who this book is for

This book is aimed at a novice programmer who is familiar with the concepts of object-oriented programming, but has little to no knowledge of Swift, Xcode, or Apple's Cocoa APIs. Starting with an introduction to the basics of Xcode and the Swift programming language, they will learn all the skills that are essential for building an app and submitting it to the app store.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/Xcode_7_Essentials_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.

Chapter 1. Introduction to Xcode

Tools are important. Finding the correct tools for the right situation is even more important. As a developer, you are overwhelmed by decisions on which platform, programming language, and tool to use. This book is all about Xcode. It's the development environment that Apple gives to developers (for free!) to create software on their four platforms (OS X, iOS, watchOS, and tvOS). We will be working with Xcode 7 and the features it offers. We will see how to use them to write code, build user interfaces, debug, run on our devices, and distribute our apps.

In this chapter, you will learn these topics:

How to find, install, and launch XcodeSome features of XcodeWhat's new in Xcode 7

Xcode allows you to build native applications that will run on, and use the features of, the current versions of iOS, OS X, and watchOS. This means that it will work to create software for iOS 9.x, Mac OS X 10.11 (El Capitan), and watchOS 2.x. All new releases of Xcode ship with the ability to support one major OS revision of the past. In this case, Xcode 7 supports iOS 8.1, Mac OS X 10.10 (Yosemite), and watchOS 1.1. This backwards compatibility is included to aid developers who maintain older software. Apple recommends that all new applications begin their life using the most current version of their operating systems in order to get access to their latest APIs.

Requirements and getting Xcode

The primary requirement for running Xcode 7 is to have an Apple computer running at least OS X 10.10.4 (Yosemite), and it should have at least 4 GB of free space available. Xcode is a resource-intensive application that is designed for use on higher screen resolutions. As such, it's recommended that you use the Pro line of Apple computers or the newer 5k iMac with at least 8 GB of RAM.

The tool itself can be found on the Mac App Store. Simply search for Xcode and then select its icon from the search results list to be taken to the app details page. Once there, you can simply select the grey Get button to install the application. This method of installation greatly simplifies setup app updates. The App Store will simply keep it up to date automatically, just like any other app installed in this way. Reinstalling the app is as simple as pressing the Get button again anytime in the future.

Tip

If you like to work with beta software, you can also consider installing a beta version of Xcode from the Apple Developer Portal. Because Apple doesn't allow apps built with beta software on the App Store, this is recommended only if you are an advanced user.

Here is a screenshot of the Xcode installer on the App Store:

The Xcode page on the Mac App Store, showing the Get button highlighted

On the first run, Xcode will ask you to download some of the optional components and utilities. These can be found under the Developer tools menu. Other components, utilities, documentation, and simulators can be downloaded via the preferences window:

The Downloads section of Xcode's settings window

Without any additional downloads or setup, you have the ability to build, test, and run OS X applications as well as build and test your iOS applications within a simulated iOS device. One new feature of Xcode 7 is that by using an Apple ID, you now have the ability to side-load any code onto your own iOS device (with some limitations). Previously, this was possible only with a paid Apple Developer account.

Releasing an app on the App Store still requires an Apple Developer account. This account will also offer access to pre-release software, access to the Apple Developer Forums, and two incident support tickets with Apple (per year).

Features of Xcode

Xcode is an all-inclusive IDE (short for Integrated Development Environment), and Apple gives you all the tools and features that you will need to create your app within this one piece of software. From within this environment, you will be able to write, run, and debug your code as well as lay out and test your user interface:

The Xcode environment is made up of the following components:

Editor: Xcode offers an editing environment that includes code autocomplete and syntax highlighting for code written in Swift and Objective-C. Other programming and markup languages are supported with syntax highlighting only (JavaScript, Ruby, C, AppleScript, XML, and JSON for example).File View: Xcode also features file viewers for code, images, media, and data models.Interface Builder: It features an Interface Builder that allows you to create interfaces for your apps using a simple drag-and-drop interface.Debugger: Xcode features a debugger based on LLDB. It allows you to set breakpoints and console access to debug the current state of your application.Versioning: Xcode includes support for version control using Git.Unit Testing: Xcode includes features that show code coverage as well as the ability to run automated testing for developing with the use of continuous integration practices.

What's new in Xcode 7?

Every year, Apple uses their Worldwide Developers Conference to introduce their new technologies, platforms, and tools to developers. This year, the keynote primarily focused on updates to iOS 9, OS X 10.11 El Capitan, and watchOS 2. Bundled with these announcements were updates and improvements to Xcode that will allow developers to work with these new updates. The major features announced for Xcode are sideloading apps, updates to the Swift language, updates to playgrounds, new app optimization, new testing features, and the ability to collect crash logs from users.

Sideloading

Since Apple began officially supporting third-party apps in iOS with iOS 3.1, they have required that developers purchase an Apple Developer account in order to load their apps onto physical hardware. In Xcode 7, Apple removed this requirement, and any developer with a free Apple ID can now load their own applications onto their devices.

Developers will still require an Apple Developer account if they wish to interact with the advanced features of iOS (Apple Pay, Game Center, In-App Purchase, iCloud, Passbook, and Push notifications), as well as upload their apps to the iOS App Store to make them available for purchase.

Swift 2

Since its introduction in 2014, the Swift language has been evolving aggressively. Originally developed secretly by a small team, this language has grown tremendously while being open to the public. Each point release wasn't backwards compatible with previous versions and required any software to be converted to the most current version for development to continue.

Apple surprised many developers at WWDC in 2015 by saying that the next major release of Swift, 2.0, would be available with the official release of Xcode 7. While Swift 2 isn't source compatible with Swift 1, Xcode offers a migration tool to help update any old code to the current version.

The language now includes integrated error handling with the ability to throw and catch errors in a safe way, a new guard statement to allow early returns from methods, a defer statement to allow blocks of code to run at a later time, and a lot of general cleanup of keywords for clarity.

Apple followed through on their promise and open-sourced the Swift language in December 2015. More information about where to find it as well as how to get involved with the development of the language can be found at http://swift.org.

Playgrounds

Introduced in Xcode 6, Playgrounds were presented as Apple's version of a REPL (Read-Evaluate-Print loop) tool to allow quick prototyping, experimentation, and discovery. In Xcode 7, you're able to generate formatted comment blocks, include embedded resources, and see results inline. It will also include support for all the new features of Swift 2.0 and the modern Objective-C:

Testing

Testing your code has been a major focus for Apple in recent versions of Xcode. Apple updated the Test Navigator to better navigate through your tests and introduced a Testing Assistant to make it easier for your tests to work together. The biggest addition in Xcode 7, however, are tools that allow User Interface testing. Previously, your unit tests could only interact with models, objects, and controllers that you had written for your application. Now, with UI testing, you're able to simulate a user tapping, swiping, and interacting with your app.

Crash logs

Applications crash. This inevitability has caused the need for third party tools to be created in order to collect and collate the logs from these events and present them to you, the developer. In Xcode 7, Apple will collect these logs on your behalf and download them automatically to make them available to you. A developer can then look into these crashes and be immediately shown the offending lines of code that caused the crash in the first place.

Summary

As a company of developers, Apple understands the need for good tools and comprehensive workflows. This year, they released a new version of Xcode that included important new features (Sideloading, UI testing, and integrated crash logs) as well as refinements of current technologies (Swift, Playgrounds, and so on). In our next chapter, we will take a detailed look at Xcode, its components, and its UI.

Chapter 2. A tour of Xcode

In the previous chapter, we looked briefly at what Xcode is, what it will allow you to do, and what's new in the latest version. In this chapter, we're going to take a deeper look at the tool itself. You will become familiar with the interface, its features, shortcut keys, and so on.

In this chapter, you will learn these topics:

Launching XcodeThe steps for the creation of a new projectA detailed overview of the Xcode workspaceThe supported development languages

Starting Xcode

Xcode is installed in the root/Applications directory on your Mac. You can launch it from the Finder, Launchpad, or Spotlight search. On launch, you will be greeted with the launch screen, which will give you two options to create a new Playground: Create a new Xcode project or Check out an existing project.

On the right-hand side of the screen, a list of previously opened projects will be shown for quick access, as shown here:

The Welcome to Xcode screen

Note

Xcode will remember and automatically reopen any project or file windows that had been open previously.

Creating a new project

After you have selected the Create a new Xcode project option, you will be guided via a wizard through the steps necessary to create your first app.

Choosing a template

Xcode supports the creation project on three distinct platforms: iOS, watchOS, and OS X. Each type has a selection of templates for Application,