Xcode 9.1 Documentation Issues
Xcode 9.1 Documentation Issues
#xcode
Table of Contents
About 1
Remarks 2
Versions 2
Examples 3
Get Started 3
Pro Tip 8
Examples 10
Xcode 8 and up 10
Manually 11
Examples 12
Running Tests 12
Remarks 16
Examples 16
Examples 19
TargetConditionals 19
Introduction 21
Examples 21
Chapter 7: Debugging 26
Examples 26
Breakpoints 26
Chapter 8: Playgrounds 31
Examples 31
Examples 34
Projects overview 34
Create a project 34
Remarks 39
Examples 39
Examples 40
Installation 41
Recommendations 41
Usage 42
Credits 45
About
You can share this PDF with anyone you feel could benefit from it, downloaded the latest version
from: xcode
It is an unofficial and free Xcode ebook created for educational purposes. All the content is
extracted from Stack Overflow Documentation, which is written by many hardworking individuals at
Stack Overflow. It is neither affiliated with Stack Overflow nor official Xcode.
The content is released under Creative Commons BY-SA, and the list of contributors to each
chapter are provided in the credits section at the end of this book. Images may be copyright of
their respective owners unless otherwise specified. All trademarks and registered trademarks are
the property of their respective company owners.
Use the content presented in this book at your own risk; it is not guaranteed to be correct nor
accurate, please send your feedback and corrections to info@[Link]
[Link] 1
Chapter 1: Getting started with Xcode
Remarks
Xcode is an integrated development environment for macOS which supports the development of
native apps for macOS, iOS, watchOS, and tvOS. Xcode is the successor to NeXT's Project
Builder and PBX. (In fact, Xcode's project manifest files are still named with the .pbxproj
extension.)
Xcode releases include stable versions of the clang C/C++/Obj-C compiler, the Swift compiler, the
LLDB debugger, and iOS/watchOS/tvOS simulators. Xcode also includes Interface Builder, as well
as tools for viewing and editing 3D models and scenes, image assets, and more.
Versions
1.0 2003-09-28
2.0 2005-04-04
3.0 2007-10-26
4.0 2011-03-14
5.0 2013-09-18
6.0 2014-09-17
7.0 2015-09-16
7.1.1 2015-11-09
7.2 2015-12-08
7.2.1 2016-02-03
7.3 2016-03-21
7.3.1 2016-05-03
8.0 2016-09-13
[Link] 2
Version Release Date
8.1 2016-10-27
8.2 2016-12-12
8.2.1 2016-12-19
8.3 2017-03-27
8.3.1 2017-04-06
8.3.2 2017-04-18
8.3.3 2017-06-05
Examples
Get Started
[Link] 3
[Link] 4
an existing version on your machine. You can also install Xcode from a direct download to get
more control over which versions you have.
Each copy of Xcode includes command line tools (clang, xcodebuild, etc.). You can choose which
ones are invoked by the commands in /usr/bin.
Or you can manage versions from the command line using xcode-select:
[Link] 5
# Print the currently selected version
$ xcode-select --print-path
/Applications/[Link]/Contents/Developer
$ clang --version
Apple LLVM version 7.3.0 (clang-703.0.29)
Target: x86_64-apple-darwin15.4.0
Thread model: posix
InstalledDir:
/Applications/[Link]/Contents/Developer/Toolchains/[Link]/usr/bin
$ clang --version
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.4.0
Thread model: posix
Many developers like to customize the font, text, and background color of their IDE's. You can do
this in Xcode by opening the app preference pane, either by going to XCODE->Preferences, or by
pressing ','
With the preference pane open you can click on the 'Fonts and Colors' tab.
[Link] 6
From here you can change the source AND console background and font colors. There are many
pre-made color and font schemes provided with Xcode. You choose these from the list on the left
(Basic, Chalkboard, etc). You can find and download more online (like here for example).
To further customize any theme, you can customize any of the types listed in the right pane (Plain
Text, Comments, Documentation Markup, etc). For example, say I really want my 'Numbers' to
show up in my code. So I change the font to 'American Typewriter' at 24 px, the color to a greenish
color, and set the line highlighting to red:
[Link] 7
Now in my text editing, I can really see my numbers:
Now you can customize the look and feel of the 'Source Editor' and 'Console' to your hearts
delight!
Pro Tip
Many developers like to theme their IDS dark (light text, dark background). In Xcode, you can only
do this for the 'Source Editor' and the 'Console'. However, the Navigation (left side), Debug
(bottom), and Utility (far right) sections are non-customizable. There are two work arounds to this.
[Link] 8
First (kind of tricky, is to leave the IDE light themed (Light background, dark text) then invert the
screen colors all together. This will make everything dark, but colors in the simulator and in the
rest of the system are now wonky. The second work around is to hide The Navigation, Debug, and
Utility areas when not in use. You can toggle these areas quickly using the following commands:
Navigator : 0
Debug Area : Y
Utility : 0
[Link] 9
Chapter 2: Certificates, Provisioning Profiles,
& Code Signing
Examples
Choose the right code signing approach
If you are just starting a new project, it's important to think about how you want to handle code
signing.
If you are new to code signing, check out the WWDC session that describes the fundamentals of
code signing in Xcode.
To properly code-sign your app, you have to have the following resources on your local machine:
On the Apple Developer Portal it's also required to have a valid App ID associated with your
provisioning profile.
You should avoid clicking the Fix Issue button (There is an Xcode plugin that disables the button),
as it sometimes revokes existing certificates, and with it the provisioning profiles.
Unfortunately you can't specify the name of the provisioning profile in Xcode 7. Instead you can
specify the UUID of the profile, which changes every time the profile gets re-generated (e.g. when
you add a new device).
To work around this issue, check out [Link] on how to pass a provisioning profile to
Xcode when building your app.
Xcode 8 and up
[Link] 10
Apple improved code signing a lot with the release of Xcode 8, the following has changed:
• No more Fix Issue button, instead all code signing processes run in the background and
show the log right in Xcode
• You can now specify the provisioning profile by name, instead of the UUID (Check out
[Link] for more information)
• Improved error messages when something goes wrong. If you run into code signing errors
you should always try building and signing with Xcode to get more detailed error information.
(Check out [Link] for more information)
Manually
You can always manually create and manage your certificates and provisioning profiles using the
Apple Developer Portal. Make sure to store the private key (.p12) of your certificates in a safe
place, as they can't be restored if you lose them.
You can always download the certificate (.cer) and provisioning profile (.mobileprovision) from the
Apple Developer Portal.
If you revoke your certificate or it expires, all associated provisioning profiles will be invalid.
With match you store your private keys and certificates in a git repo to sync them across
machines. This makes it easy to onboard new team-members and set up new Mac machines. This
approach is secure and uses technology you already use.
Getting started with match requires you to revoke your existing certificates.
[Link] 11
Chapter 3: Command Line Tools
Examples
Running Tests
xcodebuild \
-workspace [Link] \
-scheme "MyScheme" \
-sdk iphonesimulator \
-destination 'platform=iOS Simulator,name=iPhone 6,OS=9.1' \
test
xcodebuild \
-project [Link] \
-scheme "MyScheme" \
-sdk iphonesimulator \
-destination 'platform=iOS Simulator,name=iPhone 6,OS=9.1' \
test
-destination 'platform=iOS,id=REAL_DEVICE_UDID'
-destination 'platform=iOS,name=IPHONE NAME'
To list all available schemes for the project in your current directory
xcodebuild -list
Example output
[Link] 12
Unit
Build Configurations:
Debug
Release
If no build configuration is specified and -scheme is not passed then "Release" is used.
Schemes:
Themoji
ThemojiUITests
Units
Cleaning and compiling code for iPhone, on project MyProject for schema Qa:
contains the information about how to package and sign the application, for
[Link]
development builds use:
[Link] 13
<plist version="1.0">
<dict>
<key>teamID</key>
<string>xxxxxxxxxxx</string>
<key>method</key>
<string>app-store</string>
<key>uploadSymbols</key>
<true/>
</dict>
</plist>
• compileBitcode
• embedOnDemandResourcesAssetPacksInBundle
• iCloudContainerEnvironment
• manifest
• method
• onDemandResourcesAssetPacksBaseURL
• teamID
• thinning
• uploadBitcode
• uploadSymbols
To get a more information about each of the parameters run xcodebuild --help
uses the system default Xcode version (set via xcode-select) to locate and execute
xcrun
command line tools from the Xcode application bundle, e.g., llvm-cov.
# Execute xcodebuild
# /Applications/[Link]/Contents/Developer/usr/bin
xcrun xcodebuild [parameters]
# Use Xcode's version of git, e.g., if you have installed a newer version
# /Applications/[Link]/Contents/Developer/usr/bin
xcrun git [parameters]
xcode-select -p
[Link] 14
Reset to the default version of Xcode
sudo xcode-select -r
[Link] 15
Chapter 4: Creating Custom Controls in
Interface Builder with @IBDesignable
Remarks
It became much easier to create custom controls in Interface Builder with the introduction of the
@IBDesignable and @IBInspectable directives in Swift. Developers can now build rich, complex, fully
animated controls using just a few extra lines of code. I'm surprised by how many developers have
yet to fully embrace this feature, and I frequently find that adding just a few of lines of code to
existing classes can make them so much easier to work with.
Note that these features are also available in Objective-C and are a great way of breathing life into
old classes. The syntactic equivalents in Objective-C are IB_DESIGNABLE and IBInspectable, but
for now I'll be concentrating on examples in Swift.
Examples
A Live-Rendered Rounded View
This is such a common requirement in iOS development, and it was always something that had to
be done purely in code (or using images - yuck!). Now it's incredibly easy to preview his kind of
thing in Interface Builder, there's absolutely no excuse for not using it.
import UIKit
@IBDesignable
class MyRoundedView: UIView {
To use this class, add it to your project and then open the storyboard in IB and create a normal
UIView of a decent size. Give it a background colour so you can see it, then navigate to the
Identity Inspector in the right-hand Utilities panel and change the class type in the drop-down to
MyRoundedView.
[Link] 16
When you do this you should see a third label appear beneath "Class" and "Module" that says
"Designables", and it should say "Updating" for a moment before changing to "Up to date". This
means that Xcode has recompiled your code for MyRoundedView successfully.
Now you can open the Attributes Inspector and you should see (maybe after a short pause) a new
section at the top of the pane with the heading "My Rounded View" and a new attribute labelled
"Radius" with the value 8 (because that is the initial value we set in the code). This has appeared
in the Attributes Inspector because we marked it as @IBInspectable.
You can now change this to another number and you should see the rounded view's corner radius
update in real-time!
[Link] 17
Read Creating Custom Controls in Interface Builder with @IBDesignable online:
[Link]
ibdesignable
[Link] 18
Chapter 5: Cross-Platform Development
Examples
TargetConditionals
The system header TargetConditionals.h defines several macros which you can use from C and
Objective-C to determine which platform you're using.
- (void)doSomethingPlatformSpecific {
#if TARGET_OS_IOS
// code that is compiled for iPhone / iPhone Simulator
#elif TARGET_OS_MAC && !TARGET_OS_IPHONE
// code that is compiled for OS X only
#else
// code that is compiled for other platforms
#endif
}
7.0
When using the iOS 9.1, tvOS 9.0, watchOS 2.0, OS X 10.11 or newer SDKs:
iOS Watch TV
Macro Mac iOS Watch TV
simulator simulator simulator
TARGET_OS_MAC 1 1 1 1 1 1 1
TARGET_OS_IPHONE 0 1 1 1 1 1 1
TARGET_OS_IOS 0 1 1 0 0 0 0
TARGET_OS_WATCH 0 0 0 1 1 0 0
TARGET_OS_TV 0 0 0 0 0 1 1
TARGET_OS_SIMULATOR 0 0 1 0 1 0 1
TARGET_OS_EMBEDDED 0 1 0 1 0 1 0
TARGET_IPHONE_SIMULATOR 0 0 1 0 1 0 1
7.0
[Link] 19
Macro Mac iOS iOS simulator
TARGET_OS_MAC 1 1 1
TARGET_OS_IPHONE 0 1 1
TARGET_OS_EMBEDDED 0 1 0
TARGET_IPHONE_SIMULATOR 0 0 1
[Link] 20
Chapter 6: Customizing Xcode IDE
Introduction
This is collection of different tips and tricks, to customize and improve your Xcode IDE
Examples
Open Terminal in current Xcode project folder
Here is example how to assign hotkey + +⌃+ +T to open Terminal app in current project folder.
#!/bin/bash
2. Make script executable: open Terminal at script folder and run chmod +x your_script_name.sh
[Link] 21
6. Choose script, which you create previously by clicking at the Choose Script... twice.
If your script is grayed, be sure, that you run chmod +x on your script file
[Link] 22
7. Assign hotkey (for example + +⌃+ +T) to your behavior and rename it
[Link] 23
Now you can open terminal in project folder with one hotkey.
This is only one example of using Xcode behaviors, but you can create any script and launch any
app with it.
In the same way as in Open Terminal in current Xcode project folder example, you can add clear
of derived data folder with hotkey.
Create custom behavior (follow the steps in Open Terminal in current Xcode project folder). But
use another script.
Script text:
#!/bin/bash
rm -rf $HOME"/Library/Developer/Xcode/DerivedData/"
[Link] 24
Read Customizing Xcode IDE online: [Link]
ide
[Link] 25
Chapter 7: Debugging
Examples
Breakpoints
In xcode developers can pause/break the execution of running app and can examine the state of
program.
[Link] 26
So here we placed breakpoints on line no 21 and 38; when execution reaches at line 38 Xcode
[Link] 27
paused execution and shown green line on that line.
Debug Gauges gives us an glimpse of CPU usage, Memory usage and at bottom the execution
stack with Threads and function names. We can know which stack or sequence of functions lead
execution to this line of break.
Variables View gives all the details of states and values of all variables in the scope of breaded
line. We can see their values, memory addresses, properties of instances and their details.
Console can be used to print value of any variable that is in scope. Using PO command we can
achieve this.
Configure Breakpoint:
We can even have more control on breakpoints.
[Link] 28
Reveal in Navigator takes us to Breakpoint navigator where all the breakpoints from project are
listed as File Navigator.
Edit Breakpoint is something we should used more often for detailed debugging. We can configure
breakpoints using this function. We can conditions and actions to breakpoints as:
As shown in image, that breakpoint will be paused only if path != nil. If this condition is true then
po _routeStartLocation action is executed and mentioned earlier po will print value of
_routeStartLocation on console.
As recently Apple rolled out iOS11 and Xcode-9, we can now debug apps on devices without
connecting devices to Xcode through USB.
We can take advantage of wireless debugging feature added to this Xcode-9.
[Link] 29
(Image courtesy: Surjeets' SO post)
4 Then disconnect your device from USB cord, make sure iPhone/iPad/iPod device and Mac
running Xcode are in same wireless network.
5 In Xcode you will see this devices listed and you can directly run your app on that device.
We can perform all the operations with Xcode on that device same as if it is connected using USB;
except that we can not see logs if app is run using Xcode, put it in background and suspended in
background state and we launch it again. This is possible with USB debugging.
NOTES:
1 We have to use Xcode-9, iOS 11 running on device
2 Both device and Mac should on same wireless network
[Link] 30
Chapter 8: Playgrounds
Examples
Getting Started with Playground
• First option: From Xcode welcome screen, select the first option (Get started with a
playground).
2. Name your playground and select the platform (iOS/macOS/tvOS), then click Next.
[Link] 31
3. On the next screen, choose where you want to save your playground, then click Create.
Using Playground it is easy to see that happens inside loops or objects while the change is
happening.
For example, in the code below, the value of x will change from 1 to 4.
import UIKit
for x in [1, 2, 3, 4] {
x
}
(1) Clicking on the eye symbol on the right will give us a quick look.
(2) Clicking on the circle next to it will open show the Latest Value below the line.
(3) Right click on the view added will show a drop down menu with Latest Value, Value History
and Graph
[Link] 32
Adding Images, Static Data, Sounds, etc. to a Playground
1. If the Project Navigator is hidden, choose View > Navigators > Show Project Navigator (1)
2. There are several ways to add files
• Drag your resources to the Resources folder or
• Select the Resources folder and choose File > Add Files to "Resources" or
• control-click the Resources folder and choose Add Files to "Resources"
3. Use your resource. For example let i = UIImage(named: "[Link]")
[Link] 33
Chapter 9: Projects & Workspaces
Examples
Projects overview
Xcode projects are used to organize source files, library dependencies, and other resources, as
well as the settings and steps required to build the project's products. Workspaces are groups of
projects and other files.
Create a project
You can create a New Project (N) from a number of built-in templates:
[Link] 34
1. Toolbar (top)
2. Navigator (left)
3. Editor (center)
4. Inspector (right)
5. Variables View (lower-middle left)
6. Console output (lower-middle right)
7. Library (lower right)
[Link] 35
[Link] 36
.) to stop execution.
Click & hold to see the other actions, Test (U), Profile (I), and Analyze (B). Hold down modifier
keys option, shift, and ⌃ control for more variants.
[Link] 37
One of the things that can really boost your productivity while writing the code is effectively
navigating the workspace. This also means making it comfortable for the moment. It's possible to
achieve this by adjusting which areas of workspaces you see.
The buttons on the top of the navigation and assistant areas are not that big and are a bit hard to
click with the mouse pointer. That's why there are helpful and easy to remember shortcuts that let
you switch between different tabs or hide the area altogether.
You can switch between different panels in the navigation area by holding the (Command) button
and pressing on different digit keys from 1 to 8 or 0. The 0 key toggles the presence of the
navigator. Here's a list of shortcuts for your convenience:
1. +1 - File navigator;
2. +2 - Symbol navigator;
3. +2 - Search (also reachable through + +F);
4. +4 - Warnings, errors and static analysis messages;
5. +5 - Tests available in the project;
6. +6 - Debug session panel;
7. +7 - Breakpoints;
8. +8 - Report/action history navigator;
9. +0 - Show/Hide the navigator panel;
You can switch between different panels in the inspector area by holding and and pressing
different digit keys from 1 to 6 (depending on the currently active editor: whether it's code,
storyboard, xib or other type of resource). Pressing 0 while holding these two buttons will hide the
inspector area.
So if you are editing a storyboard and need more visible space just tap + 0 and + +0 to get extra
pixels for the canvas.
While switching panels on either side mostly depend on the combination of or and , the
bottom search fields are activated by holding and and pressing either j for navigation area
search bar or k for library area search bar.
Activating the navigation search area can help you narrow the list of items displayed in navigator
area depending on which navigator is active (filter files in the file navigator, simbols in the symbol
navigator, tests in test navigator, etc).
Activating the inspector panel will help you filter the list of either file templates, code snippets,
objects or media resources. Try using this search field when you have storyboard open and you
quickly need to find a UINavigationItem or UITableViewCell components!
Speaking of library, you can switch between library panels (File templates, code snippets, object
and media libraries) ⌃+ + and respective digit keys: 1 through 4.
[Link] 38
Chapter 10: Xcode 8 features
Remarks
This only works with projects using Swift 3+
Examples
Color and image literals
Xcode 8 will automatically recognize any images you’ve got in an Asset Catalog and offer them up
as a suggestion inside of a UIImage initializer.
So you could basically declare a new variable and then add an asset name that you have added to
your asset catalog. For example let img = dog. img does now contain the image of dog that´s in the
asset catalog.
Under the hood it’s creating code that looks like this: #imageLiteral(resourceName: "[Link]"). But
inline in the source editor, you’ll just see the file name of the image.
Note that you need to click on the instellisense suggestion so that you see a thumbnail of
the image in the code and then the image name.
[Link] 39
Chapter 11: Xcode Tips
Examples
Reuse code snippets in Xcode
You can save your code snippets for use later simply by drag and drop. For eg: if you have an
NSLog statement that used for so many places somewhere else in the project, then you can save
the NSLog statements to code snippets library.
[Link] 40
[Link] 41
• XcodeColors - Colored console logs, e. g. using CocoaLumberjack
• FuzzyAutocomplete - Type "NSog" and still get NSLog autocompleted
• BuildTimeAnalyzer - Set -Xfrontend -debug-time-function-bodies under Other Swift flags in the
build settings and optimize your Swift build time
in the build settings and optimize your Swift build time
Of course there are many more and some are so good, Apple already implemented them into
Xcode 8 (FuzzyAutocomplete and VVDocumenter for example)
Usage
[Link] 42
[Link]
Click install on any package you want installed and afterwards restart Xcode again. 43
2. On your Environment Variables set OS_ACTIVITY_MODE = disable
[Link] 44
Credits
S.
Chapters Contributors
No
Getting started with Anh Pham, Community, Jbryson, jtbandes, Md. Ibrahim Hassan,
1
Xcode Undo
Certificates,
2 Provisioning Profiles, KrauseFx
& Code Signing
Command Line Ali Beadle, David Snabel-Caunt, Fabio, Idan, Jens Meder,
3
Tools KrauseFx
Creating Custom
Controls in Interface
4 Echelon
Builder with
@IBDesignable
Cross-Platform
5 J F, jtbandes
Development
Customizing Xcode
6 Vitaliy Gozhenko
IDE
7 Debugging D4ttatraya
Projects &
9 Eimantas, jtbandes
Workspaces
[Link] 45