IntroductionWelcome| 00:00 | (music playing)
| | 00:04 | Hi! I'm Bill Weinman, and welcome
to Xcode 4 Essential Training.
| | 00:08 | In this course, I'll show you how to
use one of the most powerful and flexible
| | 00:13 | development environments
available today.
| | 00:15 | I'll explain how the new single
Window interface is organized.
| | 00:18 | I'll show you the various Editors
available: the standard editor, the assistant
| | 00:23 | editor, and the version editor.
| | 00:25 | I'll explain how to use Interface
Builder to create your user interface, using
| | 00:30 | both the traditional NIB files, and the
new storyboard's feature that aggregates
| | 00:35 | multiple scenes using segues
and transitions.
| | 00:39 | I'll also cover the various utility
inspectors, auto layout, compiler options,
| | 00:44 | debugging tools, workspaces, and
static analysis tools that Xcode provides.
| | 00:51 | So, let's get started with
Xcode 4 Essential Training.
| | Collapse this transcript |
| Using the exercise files| 00:00 | The exercise files for this
course are included with your basic
| | 00:03 | lynda.com membership.
| | 00:05 | If you're a lynda.com member, you
have access to the exercise files used
| | 00:09 | throughout this title.
| | 00:10 | Copy the exercise files to a location
where you can find them on your system.
| | 00:15 | Make sure it's a location that
doesn't have any spaces in the path.
| | 00:18 | Some of Xcode's tools do not work
properly if there are spaces in the file names.
| | 00:23 | I've copied them to
the Desktop on my system.
| | 00:26 | For each of the lessons, you want to make a
working copy of the Xcode project involved.
| | 00:31 | There are two ways you can do this.
| | 00:33 | You may either use Command+C
and Command+V to copy and paste.
| | 00:37 | I'll demonstrate here.
| | 00:39 | I'm going to take this project here,
TestBedOSX, and I'm selecting the topmost
| | 00:44 | folder of the project.
| | 00:45 | There's other stuff inside, but
I'm selecting that whole folder.
| | 00:48 | I'm pressing Command+C on my keyboard,
and then I select an empty area in this
| | 00:53 | folder, and press Command+V,
and that makes a copy.
| | 00:58 | You'll notice that the name of the copy
is the same as the name of the original,
| | 01:01 | with a space, and the word copy.
| | 01:05 | You don't want that space in
there, so you need to rename it,
| | 01:08 | and there's a couple of
ways you can do that.
| | 01:10 | You can either select it, and press
Return or Enter on your keyboard, and that
| | 01:14 | will give you an opportunity to edit it,
and rename it, or you can select it, and
| | 01:19 | then pause, and select it again with
your mouse, and you get the same result.
| | 01:24 | So, I'm going to rename this, and
I'm going to name it TestBed-working.
| | 01:30 | So, it's important that you rename the
copy, so there's no space in the file name
| | 01:34 | for the project folder.
| | 01:36 | By creating a working copy, it
becomes easy to reset for the next lesson by
| | 01:40 | simply erasing the working
copy from the file system.
| | 01:43 | After making a working copy, you may
open the project by double-clicking on the
| | 01:48 | .xcode project file.
| | 01:49 | It's this one here: .xcodeproj.
| | 01:53 | That file actually contains
the project itself.
| | 01:57 | So, I'll double-click on that, and
that opens Xcode with that project.
| | 02:02 | It's easy for Xcode to get confused by
this process, because we may delete a
| | 02:06 | project, and then in the very next
lesson, create a new copy with the same name.
| | 02:11 | So, once you've opened the project in
Xcode, it's a good idea to select Clean
| | 02:15 | from the Product menu.
| | 02:18 | Essentially, this tells Xcode to start
over by building all the parts of the
| | 02:22 | project from scratch, so Xcode's tools are
in sync with the current state of the project.
| | 02:27 | This is an easy thing to do, and it will
save you a lot of headache in the long run.
| | 02:32 | When you're done with each lesson, you'll
want to erase your working copy of the project.
| | 02:37 | Be sure to quit Xcode first. I'm pressing
Command+Q on my keyboard to Quit Xcode,
| | 02:43 | and then navigate to that top
folder where it says -working, and press
| | 02:48 | Command+Backspace key.
| | 02:50 | On some keyboard, it's labeled Delete,
but it's the one on main part of the
| | 02:54 | keyboard, a couple of rows
above from the Return key.
| | 02:58 | This actually moves the entire folder
and all of its contents to the trash,
| | 03:02 | effectively deleting it.
| | 03:05 | The first time you run some code from
within Xcode, you should get a dialogue
| | 03:09 | asking to turn Developer mode on.
| | 03:12 | If you have not turned on Developer mode,
you'll get a message every time you run
| | 03:16 | your code, or run a debugger, or some
action that requires permissions, and the
| | 03:21 | message will say something like
Developer tools access need to take control of
| | 03:25 | another process for debugging to
continue; type your password to allow this.
| | 03:30 | You'll get a message that it
sounds something like that,
| | 03:32 | and if you haven't selected Developer
mode that first time, it's not really
| | 03:37 | going to give you that opportunity
to permanently select Developer mode;
| | 03:41 | it'll just keep asking for your password
over and over, and that's kind of annoying.
| | 03:45 | So, here's what you can do about that.
| | 03:47 | I'm going to make a working copy of
TestBedOSX, and I'm going to go ahead and
| | 03:51 | rename that to -working, and
I'm going to open it in Xcode.
| | 03:57 | And then, from the Window menu,
I'm going to select the Organizer.
| | 04:02 | Within the Organizer, you select the
Devices tab, and your device, which will
| | 04:08 | probably say My Mac, like that, and
here you have a button that either says
| | 04:12 | Enable Developer Mode, like mine does.
| | 04:14 | I'm going to press that, and type my
password, and now Developer mode is enabled.
| | 04:21 | If it's already enabled, you'll see a
button that says Disable Developer Mode,
| | 04:25 | and you don't need to do anything.
| | 04:27 | So, I'll Close that Window with Command+W,
I'll Quit Xcode with Command+Q, and
| | 04:32 | I'm going to erase this
working copy right there.
| | 04:36 | The exercise files are here to make your
learning experience easier, and more powerful.
| | 04:41 | Take your time,
| | 04:42 | experiment a lot,
and happy learning.
| | Collapse this transcript |
|
|
1. Getting StartedWhat is Xcode?| 00:00 | Xcode is an Integrated
Development Environment, or IDE.
| | 00:04 | It includes editors, compilers, and
other tools designed to facilitate the
| | 00:09 | development of software for Apple's
devices, including Macs, iPhones, and iPads.
| | 00:15 | Xcode, like OS X, is based on work
done at NeXT computer as part of the
| | 00:20 | NeXTSTEP operating system.
| | 00:22 | The project builder IDE was
rewritten, and for a while it was called PBX.
| | 00:28 | It's been called Xcode since
the 10.3 Panther release of OS X.
| | 00:33 | The current version of Xcode
features a single window interface.
| | 00:36 | You may have multiple tabs per window,
you may open multiple windows, or you may
| | 00:41 | open editors in separate windows, but
the application is designed to operate
| | 00:45 | best as a single window interface.
| | 00:49 | Xcode includes a suite of editors for
editing source code, XML, Plists, and
| | 00:53 | Graphical User Interfaces.
| | 00:55 | It has excellent support for version
control, using either the Git or Subversion
| | 01:00 | version control system.
| | 01:01 | Xcode provides a consistent user
interface for both systems, with minor
| | 01:06 | differences necessitated by
the different technologies.
| | 01:10 | For compiler and language support,
the LLVM compiler infrastructure is
| | 01:14 | included with Xcode.
| | 01:15 | LLVM, along with the Clang front-end,
provides excellent support for syntax
| | 01:21 | highlighting, static
analysis, and other IDE features.
| | 01:25 | An older version of GCC is also provided,
but it is deprecated for new projects.
| | 01:31 | LLVM and Clang Language Support
include C, C++, Objective-C, and the
| | 01:36 | hybrid Objective-C++.
| | 01:37 | It is also possible to develop for a
number of other languages with various
| | 01:42 | modifications and plug-ins.
| | 01:43 | Xcode is a rich and powerful environment
for developing code for use on Macs and
| | 01:49 | Apple mobile environments.
| | 01:51 | To get the most out of this essential
IDE, it's worth taking the time to learn
| | 01:56 | about its various tools and features.
| | Collapse this transcript |
| Installing Xcode| 00:00 | If you have a modern Mac running OS X,
| | 00:03 | 10.7 or later, you may
download Xcode from the Mac App Store.
| | 00:07 | If you're running an earlier version of
OS X, you may need to get Xcode from your
| | 00:12 | OS installation disk, or download
it from the Apple developer Web site.
| | 00:16 | Xcode is typically
distributed from the App Store,
| | 00:21 | and so I'm running the App Store here on
this Mac, and I just come up here to the
| | 00:25 | Search box, and I type in Xcode, press
Enter, and you see there it is, and it
| | 00:30 | says that it's free.
| | 00:32 | I can just click there to download it,
or I can go and look at the product page
| | 00:36 | for Xcode, and we can see the description
of it, and its ratings, and all of that.
| | 00:41 | And I can just press this button here
that says Free, and it will switch Install
| | 00:45 | App, and I install the app, and then I
need to sign in with my App Store ID, and
| | 00:52 | then it will start downloading.
| | 00:54 | Xcode is a very large file, about 1.6
gigabytes, so expect it to take awhile to download.
| | 01:00 | So, when the installation is finished,
you'll see the button over here will
| | 01:05 | change to say Installed, and then
we can run Xcode for the first time.
| | 01:09 | So, I'm going to bring up the Finder, and
select Applications, and scroll down here
| | 01:15 | to the bottom, and you'll see that Xcode
is there at the bottom of the list, and
| | 01:20 | I'm going to go ahead
and drag that into my dock.
| | 01:22 | I'll go ahead and select it, and
we'll run Xcode for the first time.
| | 01:26 | Now, the first time you install Xcode,
it might have some system components to
| | 01:32 | be installed; there might be more
things in this list here, and you might not
| | 01:36 | see the list at all.
| | 01:38 | But you go ahead and press Install
if you see it, and type your password.
| | 01:44 | This can take awhile if it has to
download files from Apple's servers.
| | 01:51 | So, when that's done installing, we'll
just go ahead and press this Start Using
| | 01:54 | Xcode button, and we get
the Welcome to Xcode screen.
| | 01:59 | The first thing we want to do, though, is
we want to come up here to the Preferences;
| | 02:03 | you can press Command+Comma, like
in most Mac applications, or just select
| | 02:07 | Preferences from the Xcode menu.
| | 02:10 | And I want to run over here to the
Downloads, and because I had installed Xcode
| | 02:15 | previously on this machine, it
shows all of these as already installed.
| | 02:20 | You'll want to make sure you at least
install the documentation sets you're
| | 02:24 | going to need, so I have selected
iOS, and OS X, and the Xcode doc set, and
| | 02:30 | those are installed here.
| | 02:32 | If they're not already installed,
you'll see a little Install button, like
| | 02:35 | this Legacy Libraries.
| | 02:36 | I'm not going to install the Legacy
Libraries, because I don't actually need
| | 02:39 | them on this machine.
| | 02:40 | And under Components, you'll want to make
sure that you select the iOS simulators
| | 02:45 | that you need, and
especially the Command Line Tools.
| | 02:48 | There are some parts of Xcode that will
not work right if the Command Line Tools
| | 02:52 | are not installed, so I just
always go ahead and install them.
| | 02:55 | It doesn't take up a lot of space, it
doesn't take up a lot of time, and it
| | 02:59 | makes things work better, and occasionally,
we will even use them from the command line.
| | 03:03 | Other than that, you might want to come
over here into Fonts & Colors, and it
| | 03:07 | will start with this Basic theme. I've
created a special Presentation theme, and
| | 03:12 | I went ahead and pressed this plus
button, and it says Duplicate, and then I
| | 03:15 | duplicate it, and once it's duplicated,
you can rename it by just clicking on it
| | 03:20 | the second time, and it
will allow you to rename it.
| | 03:23 | You will normally just go
ahead and use the Basic one.
| | 03:26 | If you want to create your own Theme,
you'll go ahead and duplicate one of
| | 03:30 | these existing ones, and work from
there, but for most purposes, you're just
| | 03:34 | going to leave this alone.
| | 03:36 | Behaviors will be covered
separately in another movie.
| | 03:40 | Text Editing has some options.
| | 03:42 | We definitely want to Show Line numbers.
| | 03:44 | You can have it Show a Page guide.
| | 03:46 | Sometimes that's useful if you have
standards within your organization for how
| | 03:50 | wide your columns are
supposed to be in your code.
| | 03:53 | And the rest of this, you're
probably going to want to just leave alone.
| | 03:56 | Indentation, I just leave the defaults.
| | 03:59 | Four spaces for tab and indentation
is pretty standard, and the rest these
| | 04:03 | defaults will probably
work just fine for you.
| | 04:07 | Key Bindings, again,
I would leave the defaults.
| | 04:10 | You'll notice that even with the defaults,
there are a few conflicts. That's okay.
| | 04:15 | If you have certain keystrokes that
you'd like to have work in certain ways, you
| | 04:19 | can certainly customize this.
| | 04:22 | And again, with Locations, I would
leave the defaults on these, unless you have
| | 04:26 | very specific requirements.
| | 04:27 | So, we'll go ahead and close this, and
Xcode is now installed. Xcode is free.
| | 04:35 | It's easy to install, especially if
you're running the latest version of OS X.
| | 04:39 | Simply download Xcode from the App
Store, and install the Command Line Tools, and
| | 04:43 | documentation from the
Preferences, and you're good to go.
| | Collapse this transcript |
| Exploring the Xcode user interface| 00:00 | Current versions of Xcode provide a
powerful single window interface with all
| | 00:05 | the essential elements to develop
an application in one organized view.
| | 00:10 | Let's take a close look at the
layout of this single window interface.
| | 00:12 | Keep in mind that I'll cover each of
these areas in much more detail as we
| | 00:17 | go through the course.
| | 00:19 | For now, this is just an overview.
| | 00:21 | At the top of the window is the toolbar.
| | 00:24 | On the left side of the toolbar, you'll
find controls for building, running, and
| | 00:28 | debugging your code.
| | 00:30 | There's a status display in the
middle, and view selectors on the right.
| | 00:35 | On the left is the navigation area.
| | 00:37 | Here you'll find navigators for files,
symbols, and debugging information.
| | 00:42 | The navigation selector bar
allows you to select from the available
| | 00:46 | navigators, and the filter bar at the
bottom allows you to filter and search
| | 00:51 | within the navigator.
| | 00:53 | The large area in the middle
is the editor area.
| | 00:55 | This is where you'll do most of your work.
| | 00:58 | The breakpoint gutter at the left includes
line numbers, breakpoints, and other
| | 01:03 | indicators related to the
code in the editor.
| | 01:06 | The focus ribbon allows you to
focus on the structure of your code.
| | 01:10 | We'll see this in action later.
| | 01:12 | The jump bars are used to
select the content of the editors.
| | 01:15 | We'll explore this feature later as well.
| | 01:19 | You'll find the debug area under the
editor area. It's split into two sections;
| | 01:23 | the debug log on the left,
and the console on the right.
| | 01:28 | There's a debug bar across the top of
the debug area, and separate selectors
| | 01:32 | above the debug log and the console.
| | 01:36 | The debug bar is used to control the
debugger, and the selectors control the
| | 01:40 | content of the debug log
and the console.
| | 01:44 | The utility area is on the
right side of the window.
| | 01:48 | The top part of the utility area
is the inspector pane, which contains
| | 01:51 | information and options for various
types of files that may appear in the editor.
| | 01:56 | Its selector bars selects
different views in the inspector.
| | 02:00 | The bottom part of the utility area is
the library pane, and its selector bar
| | 02:05 | selects different types
of available libraries.
| | 02:09 | As you can see, there's a lot
going on here. That's okay;
| | 02:12 | we'll get into the details later.
| | 02:14 | For now, this is a handy overview of
the single window interface in Xcode.
| | Collapse this transcript |
|
|
2. Editing CodeExploring the editors| 00:00 | If you're using Xcode,
you're probably writing some code.
| | 00:03 | Xcode has a few different
editors for dealing with code;
| | 00:06 | mostly they work like
different views of a common editor,
| | 00:10 | but in Xcode terminology,
there are three different editors.
| | 00:12 | Here I am in Xcode, and I have opened a
working copy of TestBedOSX, and you'll
| | 00:18 | notice that I have on the left-hand
side the navigator pane, and then in the
| | 00:22 | middle there is the editor, and on the
right-hand side is the utility pane.
| | 00:27 | I'm going to close the utility pane,
so I'm pressing Option+Command+0.
| | 00:32 | Alternately, you can do that by pressing
this button here, and you see when I
| | 00:36 | press it, it toggles up back and forth,
or I can press Alt+Command+0, and that
| | 00:41 | actually toggles it as well.
| | 00:43 | So, I'm going to leave it
off to the side there.
| | 00:46 | I can do the same, if I like, with the
navigator pane by just pressing Command+0,
| | 00:51 | and now I've got the editor
as the full screen.
| | 00:53 | What we have up at this point is
what's called the standard editor, and the
| | 00:58 | standard editor shows
just one file at a time.
| | 01:02 | And so, if I want to see a different
file, I can come up here in the selector,
| | 01:06 | and I can click on the file, and I get
a list of the files that are available
| | 01:11 | in my project, and I could open a different
one if I like, or I can go back to the same one.
| | 01:16 | So, this is the standard editor, and
we're going into a lot more detail on the
| | 01:20 | editors and how they work
throughout this chapter.
| | 01:22 | If I want to bring up the assistant
editor, I can press the assistant editor
| | 01:26 | button up here, and that will bring
up the counterpart file, or one of the
| | 01:32 | counterpart files if there
are multiple possibilities,
| | 01:35 | but in most cases, you're working on a .m
file, which is mostly what you're going to do,
| | 01:39 | it will bring up the .h file,
or vice versa.
| | 01:42 | Now, I can go back to the standard editor here
using the selector, or I can use the keyboard.
| | 01:47 | If I want to see the assistant editor,
I press Command+Option+Return key, and it
| | 01:54 | brings up the assistant editor.
| | 01:55 | If I want to go back to the standard
editor, I just press Command+Return key, and
| | 01:59 | that brings up the standard editor.
| | 02:01 | So Option+Command+Return,
I've got the assistant editor,
| | 02:04 | and Command+Return, I've
got the standard editor.
| | 02:08 | The third editor is the version
editor, and you can bring this up with
| | 02:13 | Shift+Option+Command+Return, or you can
use the selector up here. So, all three of
| | 02:19 | these are available: standard,
assistant editor, and the version editor.
| | 02:25 | Now, the version editor is
actually really convenient.
| | 02:28 | If I come back here in the standard editor,
I just make a simple change to this file;
| | 02:32 | I'll just add a comment here, and
I'm pressing Command+S to Save, and
| | 02:41 | Shift+Option+Command+Return to bring
up the version editor, and you'll notice
| | 02:46 | that here it has a very handy view in
its default view that shows me I inserted
| | 02:51 | this. The previous version didn't have it.
| | 02:53 | This version has it. It shows a little
highlight here between lines 18 and 17,
| | 02:58 | and here between 17 and 20, it shows
what is inserted, with a nice graphic that
| | 03:04 | gives us an idea of where
it is, and where it was added.
| | 03:08 | So, those are the different editors
available for editing code in Xcode.
| | 03:11 | I'll be showing you each of
these editors in more detail in the
| | 03:14 | following lessons.
| | Collapse this transcript |
| Using the Standard editor| 00:00 | The standard editor is the
foundation of the code editors in Xcode.
| | 00:03 | Here I have open a working copy of
the TestBedOSX project, and it's opened in
| | 00:11 | Xcode, and I'm just going to use Option+
Command+0 to toggle the utility pane, so
| | 00:16 | we have a little bit more
room here on this limited screen.
| | 00:20 | If you're also working on a small screen,
you can also hide the navigator pane
| | 00:24 | with Command+0, and now you have
just the editor on your screen.
| | 00:29 | If you're working with a really big
screen, you don't need to do that.
| | 00:32 | Now, I've actually done some
large projects on a small screen.
| | 00:36 | I used a 13-inch MacBook Air for a
couple of years, and so this was actually a
| | 00:40 | common way for me to work.
| | 00:42 | Just using the standard editor, and
with the utility pane and the navigator
| | 00:46 | pane hidden, and this really kind of
allows you to really focus on the code
| | 00:50 | that you're writing.
| | 00:52 | If you want to navigate between your
different files, you can use the jump bar;
| | 00:56 | that's this area up here at the top.
| | 00:58 | And if you select this folder, you can
switch between your different folders,
| | 01:03 | or if you select the file name, you can switch
between the different files in your project.
| | 01:11 | And if you select this over here,
where it says No Selection, you could
| | 01:14 | actually jump to particular methods in your
file, or particular areas in your file, like that.
| | 01:21 | This area over here to the left where
there's the line number, this is called
| | 01:25 | the breakpoint gutter, and in fact,
I can set a breakpoint right here.
| | 01:30 | I just point to this area just to the
left of the line number, and click, and
| | 01:35 | it sets a breakpoint.
| | 01:36 | And now, when I run my code, so it's
compiling, and running, and you'll see that it
| | 01:45 | will stop at that breakpoint, and
there's where the execution is.
| | 01:50 | I set the breakpoint at the top
of a method, and so it stopped at the
| | 01:54 | beginning of that method, and if I
want to clear this breakpoint, I can
| | 01:59 | right-click on it, or Control+Click on it,
and I say, Delete Breakpoint, and that will
| | 02:05 | delete it completely.
| | 02:06 | Or if I wanted to, I could just temporarily
disable it, or do different things.
| | 02:10 | We will cover breakpoints a
little bit more in debugging.
| | 02:13 | And so I'm just going to stop
the execution by pressing Command+Period,
| | 02:16 | and you'll notice that it left
my navigator open, and it left it with
| | 02:21 | the debug navigator, and I can just
select the file navigator, and I can get rid
| | 02:25 | of it again with Command+0. Also in
the breakpoint gutter, you have the line
| | 02:29 | numbers. I find that very useful.
| | 02:32 | And then this little area over here, and
when I highlight it, you'll see that it
| | 02:36 | highlights different sections of my
code. Scroll up here a little bit;
| | 02:39 | you see where I've got a
little bit of nested here.
| | 02:42 | So, it will highlight just that
for area, or the entire method.
| | 02:46 | This is called the focus ribbon, and it
can be used to help you to focus in on a
| | 02:51 | particular piece of code, if you like.
| | 02:52 | The other thing I want to show you really
briefly here in the standard editor --
| | 02:58 | and again, the standard editor is
the basis for all of the code editors,
| | 03:02 | and so these features are all available
in all of the different code editors --
| | 03:06 | it has very sophisticated code completion.
| | 03:08 | We'll see more of this as we
go along throughout the course.
| | 03:10 | I just want to give you a taste of it.
| | 03:12 | If I come up here at line 30, and
open up another line, I'm just going to
| | 03:16 | start typing some code.
| | 03:17 | I'll type a left bracket, and I start
typing the word self, and as I get two
| | 03:23 | letters in, you notice that it brings
up this selector, where it has got all the
| | 03:27 | different things to start with se, and
of course, I want self, so I can select
| | 03:31 | that, and I can press Return, and
you'll notice that it even gives me this
| | 03:36 | closing bracket there.
| | 03:37 | Or, if I like, I can start typing
this again, and I can type se, and I can
| | 03:43 | just press the Tab key without ever taking
my fingers off the home row on my keyboard.
| | 03:48 | That's typically the way that I do it,
because usually if that first selection
| | 03:52 | isn't the right one, I can type in a
few more letters, and type tab, and
| | 03:56 | that's easier for me than taking my
fingers off the home keys, and going to the
| | 04:00 | up and down arrows.
| | 04:01 | Now, you'll notice it has
got that right bracket there.
| | 04:03 | We'll get to that in a moment.
| | 04:05 | I'm going to type the spacebar here,
and I'm going to put in a messageString
| | 04:09 | call, and you'll notice that messageString is
a method that's defined right here down below.
| | 04:13 | So, if I start typing it, you'll
notice that it gives me that as one of the
| | 04:17 | choices, even though it's
defined right there in my code.
| | 04:21 | And so I can press Tab, and I can press
Tab, and you can see it stops in the camel
| | 04:26 | case there, and now it's highlighting
this NSString. So, it's actually telling me
| | 04:30 | what sort of an argument is expected here.
| | 04:33 | So, I can just start typing a string, and
you'll notice it's got the closing quote,
| | 04:39 | and the closing bracket, and
they're grayed out a little bit.
| | 04:43 | What that means is that I can either
use my right arrow to move pass them, or I
| | 04:48 | can actually type it.
| | 04:49 | And so I'm going to type that closing
bracket there, and so I can continue
| | 04:53 | typing as if I were typing that
closing quote and that closing bracket like I
| | 04:59 | normally would, and it works just fine.
| | 05:01 | Otherwise, I can just move the cursor, and that
will also allow me to use those completions.
| | 05:06 | So, you'll notice now it's got these
little up pointing triangle, and it's
| | 05:10 | actually slightly brownish color;
| | 05:13 | that's telling me that I need a
semicolon here, or that I need something.
| | 05:18 | I can click on this little red stop
sign over here; it will bring up the fix-it
| | 05:22 | for it, and it says Insert a semicolon.
| | 05:25 | So, I can do that, or I can just
press Escape. You see it says Expected
| | 05:28 | semicolon after expression.
| | 05:29 | I can just type the
semicolon, and all that goes away.
| | 05:32 | So, the standard editor has very sophisticated,
very powerful, very complete code completion.
| | 05:38 | While we're talking about the
standard editor, there's just one more thing
| | 05:42 | that I want to show you.
| | 05:43 | I'm going to press Command+S
to Save at this point.
| | 05:46 | Because I have just the standard
editor up here, I don't have the assistant
| | 05:49 | editor up -- and a lot of times on a
larger screen, I might use the assistant
| | 05:53 | editor, so I can see the header
and the file at the same time.
| | 05:57 | We'll cover the assistant
editor next in this chapter.
| | 05:59 | But if I don't have this assistant
editor available, I don't have enough screen
| | 06:03 | space for it, or I just want this view
with just my code showing, I can just
| | 06:07 | press Ctrl+Command+up arrow, and it will
switch me to what's called a counterpart file.
| | 06:11 | In this case, it's the
header file for that method file.
| | 06:15 | And if I press it again, I get right
back to where I was in that method file.
| | 06:20 | So, it's just as if I had the assistant
editor, but it was, like, behind it or something.
| | 06:24 | And Ctrl+Command+down arrow
does exactly the same thing.
| | 06:27 | So, I can do the up arrow, or the down
arrow, and this is because it's possible to
| | 06:31 | have more than one
counterpart file for a given file.
| | 06:35 | And so up arrow will shift up
through the possibilities, and the down arrow
| | 06:39 | will shift down through
the list of possibilities.
| | 06:42 | But in most cases, where there are just
two counterpart files, you know, one of
| | 06:45 | each, then either the up arrow or the
down arrow will just cycle through them.
| | 06:50 | So, the standard editor has many features,
and we'll explore more of them as we go along.
| | 06:54 | It's important to know at this point
that this is the basis for both the
| | 06:58 | assistant editor, and the version
editor, and we'll cover those later in
| | 07:01 | this chapter.
| | Collapse this transcript |
| Working with the Assistant editor| 00:00 | The assistant editor is a very
convenient tool for editing two or more
| | 00:04 | related files side by side.
| | 00:07 | Here, I've opened a working
copy of TestBedOSX here in Xcode,
| | 00:12 | and notice I have the
utility pane open here.
| | 00:15 | I'm going to close that by pressing
Option+Command+0, and I'm also going to hide
| | 00:21 | the navigator pane with Command+0.
| | 00:24 | And here we have the standard editor
open, and if want to open the assistant
| | 00:29 | editor, I can select the assistant
editor here from the Editor selector, or I can
| | 00:35 | come up to the View menu, and select
Assistant Editor, and say Show Assistant
| | 00:39 | Editor, or I can use
the Option+Command+Return.
| | 00:43 | You can see that shortcut here in the
menu. So, I'm just going to close that
| | 00:47 | menu, and I'm going to press Option+Command+
Return, and there we have the assistant editor.
| | 00:52 | I like to use keyboard shortcuts.
Editing code is a keyboard oriented activity,
| | 00:58 | and if I've got my fingers on the
keyboard already, then using those keyboard
| | 01:02 | shortcuts becomes really convenient.
| | 01:04 | That's my preference;
| | 01:05 | you can do this any
of those different ways.
| | 01:08 | So, you'll notice here in the assistant
editor, what it does by default is it
| | 01:12 | opens the first counterpart
in the editor to the right,
| | 01:16 | and whatever file you had opened in the
standard editor is in the editor at the left.
| | 01:20 | So, in the editor at the left, I have
EWAppDelegate.m, and you can see that up
| | 01:25 | here in the jump bar.
| | 01:27 | And over here on the right, it's
AppDelegate.h, and again, you can see that in
| | 01:32 | the jump bar, and of course in
the comments and the code as well.
| | 01:35 | But it's good to know to about the
jump bar, and to know that you can see what
| | 01:39 | files are open in the jump bar.
| | 01:41 | Now, over here in the View menu, under
Assistant Editor, you'll see a number
| | 01:44 | of different options.
| | 01:45 | One of them says Add Assistant Editor.
| | 01:47 | You'll notice that there's no
keyboard shortcut by default.
| | 01:50 | Later in this chapter, we're going to
add a keyboard shortcut for that, which I
| | 01:54 | think there ought to be.
| | 01:56 | And so, if I click Add Assistant
Editor, you'll notice it adds another one
| | 02:00 | stacked down here below, and we can
change that orientation here in this menu.
| | 02:06 | We can say Add Editors on the Bottom
instead, and now we've got our main one
| | 02:09 | on the top, and our assistant editors underneath.
Or there's All Editors Stacked Horizontally.
| | 02:18 | I would call this Vertical, but they
call it Horizontal, because they're left to
| | 02:22 | right next to each other.
| | 02:24 | So, the stacking I guess is horizontal.
| | 02:25 | The editors themselves look pretty
Vertical to me. Or, under Assistant Editor
| | 02:31 | here, you can stack all the editors
vertical, and this might be convenient if you
| | 02:34 | have your monitor turned vertically.
| | 02:36 | I did that for a long time.
| | 02:38 | I had my iMac monitor normal, and I had
a second monitor that I used vertically
| | 02:43 | for all of my code editing.
| | 02:44 | I did that for a long time, and
I used to use this orientation.
| | 02:48 | For now, we're going to leave this in
the default orientation, which is the
| | 02:51 | assistant editors on the right, and
then they stack one on top of each other as
| | 02:56 | you add editors, where we
can go and add another editor.
| | 02:59 | You can add, I imagine,
an unlimited number.
| | 03:01 | I've never tried to add a lot of them,
but I've never run into that limit.
| | 03:06 | You can adjust them by grabbing, and
it's just a little bit difficult to grab,
| | 03:11 | and then you can grab this, and you can
adjust the height of these, just like you
| | 03:16 | can adjust this spacing here side to side.
| | 03:22 | And if you want to delete an editor,
you select that editor, and up here in the
| | 03:26 | View menu, you'll notice that there
is Remove Assistant Editor, and it has a
| | 03:31 | shortcut, Ctrl+Shift+Command+W, and
if I click on that, it will remove the
| | 03:36 | editor that I've selected.
| | 03:38 | I'm going to go ahead and press that
keystroke, Shift+Ctrl+Command+W, and I'm
| | 03:44 | going to remove that last one as well.
| | 03:47 | You can choose what goes
in this assistant editor.
| | 03:50 | You notice that Counterparts are selected,
and there's only one counterpart, or
| | 03:55 | you can just manually
choose another file if you like.
| | 04:01 | And so we'll just choose this NIB file here,
and there's our main window for our TestBedApp.
| | 04:12 | And in fact, what I'm going to do here
is I'm going to go ahead and I'm going to
| | 04:17 | select that NIB file on the left, and
I'm going to show you what happens here.
| | 04:24 | The automatic counterpart
is going to be one of these.
| | 04:28 | So, if I come up here to the View menu
at this point, and I select Reset Editor,
| | 04:34 | you see that it brings up the first
counterpart, which is the .h file, and this
| | 04:41 | is very common for when you're editing
NIBs in an assistant editor, because you
| | 04:45 | can use it to make connections.
| | 04:48 | Sometimes you'll also want the .m file.
| | 04:51 | In this case, I actually have a
connection in my .m file for the scroll view.
| | 04:55 | So, you can see that editing a NIB file,
or a user interface is also a great use
| | 05:02 | of the assistant editor.
| | 05:04 | So, the assistant editor is very
convenient. It's used for editing two or more
| | 05:08 | related files side by side.
| | Collapse this transcript |
| Using the Version editor| 00:00 | The version editor allows you to step
through different versions of a file.
| | 00:04 | Let's take a look at how this works.
| | 00:06 | Here I have open a working
copy of TestBedOSX here in Xcode.
| | 00:11 | I'm going to hide the utility pane and
the navigator pane just for demonstration
| | 00:17 | purposes, because I have such limited
screen real estate here, and I'm going to
| | 00:22 | come down, and I'm going to add some
code, just because we need a different
| | 00:26 | version to look at here.
| | 00:27 | So, I'm just going to copy and paste
this for loop here, and instead of Klaatu,
| | 00:32 | barada, nikto, we'll just
say one, two, and three.
| | 00:37 | And I'm saving that with Command+S on
my keyboard, and I'm going to compile and
| | 00:44 | run by pressing Command+R on my
keyboard, and we can see that runs Klaatu,
| | 00:48 | barada, nikto, one, two, three, and
I'm going to press Command+Q to Quit.
| | 00:54 | And you see up here,
it says Finished running.
| | 00:57 | And so we've added some code here.
| | 00:59 | Now let's open the version editor.
| | 01:01 | So, there's a few different ways
we can open the version editor.
| | 01:05 | We can open it here in the Editor
selector by pressing the version editor
| | 01:09 | button; it's that one right there. Or
you can up to the View menu, and select
| | 01:14 | Version Editor, and Show Version Editor.
| | 01:16 | And the version editor has three
different views: Comparison View, Blame
| | 01:21 | View, and Log View,
| | 01:22 | and we're going to start
with the Comparison View.
| | 01:24 | Now, you'll notice there's
also a shortcut here for it:
| | 01:28 | Option+Shift+Command+Return.
| | 01:31 | That's a lot of keys to accord at once,
and I will typically just come up here
| | 01:36 | to the menu, or use the version
editor button for as often as I use this.
| | 01:41 | So, I'm just going to select Show Comparison
View, and here's our comparison view,
| | 01:45 | and I'm going to scroll down here to
where we made this change, and you'll notice
| | 01:49 | that it's highlighted here on the left,
and this graphic representation shows
| | 01:54 | where it was inserted
relative to the previous version.
| | 01:58 | So, down here at the bottom, you
notice we have a jump bar for the version
| | 02:01 | editor, and it's down at the bottom
here, because we still have the standard
| | 02:06 | editor jump bar at the top.
| | 02:08 | And you'll see that it says here Local
Revision, and over in the right here, it
| | 02:13 | has this complicated, with my
name, and it says Base_Head.
| | 02:17 | So, that's what was last
saved in the Git repository.
| | 02:22 | If we click on this little time icon
here, we get a different view of the
| | 02:26 | center, and you see we can scroll to
different versions. So, I can take this
| | 02:33 | right-hand bar, and I can select a
previous version, and that's the version that
| | 02:38 | was committed by me on January 12th, and
we can see here, when I start to scroll,
| | 02:44 | and the scrollbar shows up, you can see
that there's a red line up there, and a
| | 02:49 | red line down there.
| | 02:50 | The red line down here is this revision,
and up here we can see what I changed
| | 02:54 | last in this file before committing it.
| | 02:58 | So, that's very interesting.
| | 03:00 | You'll notice that we can change
these over here as well in the jump bar, I
| | 03:04 | can come down here and select a much
earlier version of this, and you'll see
| | 03:08 | that it has a lot of changes. Well, that was,
like, the first version before I added any code.
| | 03:14 | And that's kind of helpful; not terribly.
| | 03:17 | On the other hand, what we can do -- I'm
going to go back up here to the base version.
| | 03:22 | We can come over here to what's called
the Blame mode. So, you have these
| | 03:26 | three different Views; we have the
Comparison View, the Blame View, and the Log View.
| | 03:31 | In the Blame View, we can see each chunk of
code, and who committed that to the repository.
| | 03:39 | In this case, it was all committed
by me, and this is our local revision.
| | 03:45 | And so we can see that this new for
loop that I added is not committed yet.
| | 03:50 | So, why don't we go ahead and
commit this to the repository?
| | 03:53 | I am going to bring up the navigator pane.
| | 03:56 | You'll notice here that my file has an
m next to it, and that means that it's
| | 04:00 | been modified, and saved, but it has
not yet been committed to the repository.
| | 04:05 | So, if I Control+Click on this, or right-click
on it, and under Source Code, select
| | 04:10 | Commit Selected Files, and you
can just put any comment down here.
| | 04:15 | It needs a comment.
| | 04:16 | Normally your comment will be very
descriptive, because other people read it.
| | 04:19 | I happen to know this is a working copy;
I'm about to delete it anyway, so I'm
| | 04:23 | just going to say, Just kidding,
| | 04:25 | and come over here, and commit one file.
| | 04:27 | You have to have the comment. It
won't let it commit without a comment, and
| | 04:31 | that's actually Git that does that.
| | 04:32 | Subversion does the same thing. Virtually
all the revision control systems that
| | 04:36 | I know require a comment
for every commit.
| | 04:39 | So, I'm going ahead and commit one file,
and now you'll notice that my comment is
| | 04:46 | right there in the Blame column.
| | 04:47 | So, I can look at each of these pieces
of code -- this was in the initial release
| | 04:52 | version, this was in the
initial commit, here's version 1.1 --
| | 04:55 | so you see each of these has a comment.
And you'll also notice -- let's go back
| | 05:03 | down here to the Just kidding one --
| | 05:05 | there's a little I here.
| | 05:06 | When my cursor hovers over one of these
chunks of code in the Blame column, you
| | 05:12 | get this little I, and if you
click on that little I, you get more
| | 05:16 | information; how many files
were changed, what they were,
| | 05:20 | you can view the changes.
| | 05:22 | If I click on View Changes, then I get
this side by side comparison view that
| | 05:26 | just pops up there
over my current view.
| | 05:30 | So, this Blame View is actually really,
really useful, and perhaps not very
| | 05:35 | descriptively named,
even if it is a cute name.
| | 05:37 | And then the last view is the
Log View, which is also very useful.
| | 05:43 | It's very much like what you get here
from the jump bar, but with a little bit
| | 05:48 | more detail, and you can select any
one of these, and you can bring up the
| | 05:51 | information on it and you can see the
history of the file there in the Log View.
| | 05:57 | The most common view you're going to
use probably is the Comparison View, and I
| | 06:01 | often bring up the
timeline in the comparison view.
| | 06:05 | The difference here is, without the
timeline -- let me bring this back to my
| | 06:10 | previous version,
so we can see the differences --
| | 06:12 | and without the timeline, you get this
little graphic here that shows where things go.
| | 06:17 | With the timeline, you still get that;
you just don't have the little curved
| | 06:20 | part of it there in the middle.
| | 06:21 | I find the timeline
to be really, really useful.
| | 06:24 | So, that's the version editor. It's a
very powerful tool for navigating the
| | 06:28 | changes in a file, and this can be
particularly powerful in a collaborative
| | 06:32 | environment where you have several
different people committing changes to the
| | 06:35 | same files into a repository.
| | 06:37 | And we'll look more version
control in a later chapter.
| | Collapse this transcript |
| Exploring code completion| 00:00 | One of the great advantages of an
integrated development environment like Xcode
| | 00:04 | over a good text editor, like VI, or
BBEdit, is that the editors in an IDE can be
| | 00:09 | tightly coupled with the
compiler and the documentation suite.
| | 00:13 | This allows the text editor to do a
much better job of code completion, and to
| | 00:17 | offer quick access to other tools,
like documentation, and UI tools.
| | 00:21 | Here I've opened a working copy of
TestBedOSX here in Xcode, and because of my
| | 00:26 | limited screen real estate, I'm
going to hide the utility pane with
| | 00:30 | Option+Command+0, and the navigator
pane with Command+0. You don't necessarily
| | 00:35 | need to do that on your screen if
you have a lot of screen real estate.
| | 00:39 | I'm going come down here into my
runTest method, and I'm just going to
| | 00:42 | start typing some code.
| | 00:43 | I'm going to make a call to messageString,
which is a method that I defined down
| | 00:47 | here on line 44, and just to display a
message on the screen, and mostly to show
| | 00:52 | you how code completion works, I'll
start by pressing the left square bracket,
| | 00:57 | which in Objective-C says I'm going
to send a message to an object, and the
| | 01:02 | object here is self, so I'm
going to start typing self.
| | 01:05 | I'm going to just press s, and then
one more letter, e, and you see now it
| | 01:09 | comes up with some code completion choices,
and you notice we have a number of them here.
| | 01:13 | I could select one with my cursor, but
really, I've got my hands on the keyboard,
| | 01:18 | and I don't necessarily want to be
taking my hands off the keyboard all the
| | 01:21 | time to go to the mouse.
| | 01:23 | I tend to follow the Seymour Rubinstein
school of thought when it comes to the
| | 01:28 | mouse, that a mouse is a wonderful
thing if you happen to have three hands.
| | 01:32 | I don't have three hands.
| | 01:33 | I like to keep my two hands right on
the keyboard when I'm typing a lot, and so
| | 01:38 | there's a few different
ways I can do this.
| | 01:40 | I can select something using my up and
down arrows, and you see, as I do that,
| | 01:44 | right there in my code on line 31 where
I've started typing, it starts to fill
| | 01:48 | things out, and in the case of a
function call, it'll even give me all of the
| | 01:53 | types for the arguments.
| | 01:55 | But self is actually what I want, and
so all I really need to do is press the
| | 01:59 | tab key, and watch what happens.
| | 02:02 | It fills in the rest of the identifier,
in this case, self, and it even puts in
| | 02:07 | that right square bracket for me, so
I don't necessarily have to type that
| | 02:11 | when it comes time.
| | 02:12 | So, I'm going to press the spacebar here,
and I'm going to call a method on self,
| | 02:16 | which messageString, so I'm just going
to start typing again very slowly, and we'll
| | 02:20 | watch what happens.
| | 02:21 | The minute I press the m, it starts
giving me suggestions. Now, the first
| | 02:25 | suggestion here for me is messageString,
because I've been typing in this code,
| | 02:30 | and on this computer, and it has some
idea that that's probably what I want. It
| | 02:34 | is adaptive and predictive.
| | 02:37 | And so, at this point, I can just press
the tab key, or I can type a few more
| | 02:41 | letters, like I would tend to do, and
when I get as far as the first s, that's
| | 02:45 | really the only choice that it has
available, and I can just press the tab key,
| | 02:50 | and it'll take me over to the first argument,
in this case, the only argument to my method.
| | 02:54 | As you can see now there on line 44,
it's expecting an argument of type
| | 02:59 | NSString pointer, and so I can just
start typing a string, and I'm going to press
| | 03:04 | the @ sign, and again
| | 03:06 | I start getting some
suggestions. And I press a quote mark,
| | 03:10 | and it knows that I want
a string at this point.
| | 03:13 | And so, I'll start typing the string,
and as I start typing, it knows that I'm
| | 03:20 | typing a string, and it puts in the
closing quote mark next to the closing
| | 03:25 | right square bracket.
| | 03:27 | So, it knows what it is
at this point that I want.
| | 03:30 | Those are my only choices for proper
syntax with what I'm typing at this point.
| | 03:35 | So, I can either type the quote, and the
square bracket, and I'll show you what
| | 03:40 | happens when I do that.
| | 03:41 | My cursor just passes over the character
that I'm typing, and it allows me to
| | 03:46 | type as normally, and
I'll put in a semicolon here.
| | 03:49 | Also, if I want to -- and I'm just going to
this again here -- if I wanted to, instead
| | 03:58 | of typing the quote, and the right
square bracket, I could press Command+right
| | 04:03 | arrow, and it takes me right to the end
of the line, and it fills in all of those
| | 04:07 | things, so I don't have to type them.
| | 04:09 | In some cases, especially with
Objective-C, where you might have a number
| | 04:12 | of nested punctuation marks, it'll
take you to the end of all of them, and I
| | 04:18 | can press my semicolon.
| | 04:19 | I now have good, correct, working code.
| | 04:21 | Code completion can be used
for more complex things as well.
| | 04:24 | If I come down here to type a for loop,
you'll notice I get several different
| | 04:28 | choices, and one of them is the forin
Objective-C Fast Enumeration, so I can
| | 04:34 | select that with my arrow key, and at this
point, I can just press Enter, or I can press tab.
| | 04:40 | I'm going to press Enter, or the Return
key here, and you'll notice it gives me
| | 04:44 | the things that I need to fill in.
| | 04:46 | I need a type with an object, and I
need a collection, and I need statements.
| | 04:51 | So, I can just tab through these things,
I'm pressing the tab key here, and the
| | 04:56 | Shift+Tab to go backwards.
| | 04:58 | So, I can say id o, and that'll give me
a temporary variable of type id, which
| | 05:04 | is the Objective-C polymorphic generic
object type, and then I can type my collection.
| | 05:11 | So, in my collection, I'm just going to
put a few strings, like I did up there in
| | 05:15 | line 28. So, I'll use the at symbol,
and the left square bracket,
| | 05:17 | and this is the new Objective-C literal
collection, and I can just start typing objects.
| | 05:17 | So, I'm going to tab over to where it
says objects, and I'll just type a few
| | 05:29 | strings, and you notice that it has
completed my last quote. I can just use a
| | 05:35 | right arrow there, and put in a
comma, or I can do it this way;
| | 05:40 | I can just type the closing quote, which
tends to be what I do, because my fingers
| | 05:45 | are used to doing that anyway.
| | 05:46 | Now I can just tab over to
statements, and I can type in my statements.
| | 05:55 | So, I start typing StringWithFormat, and
you notice I get a number of choices. I
| | 05:59 | can press Enter here, or if I press Tab,
you'll notice it takes me to the first
| | 06:03 | width, because there's a number of
choices within that that fit width, and if I
| | 06:09 | press Tab again, it'll take me to the
word format, still allowing me to choose
| | 06:15 | some of the different string width
choices. And I press Tab again, and here I can
| | 06:21 | type in the arguments for string with
format, which is a variadic function,
| | 06:25 | and so it has both a string, and
the ellipsis, which means I can have
| | 06:29 | more objects after that.
| | 06:31 | So, I'm just going to type in a format
string, and a new line, and a close quote,
| | 06:40 | and a comma, and o, and I have constant
id o is the one that I want, so I'm just
| | 06:47 | going to press Enter to select that.
Command+right arrow to take me to the
| | 06:52 | end, and a semicolon, and I've now typed
correct code, with very few actual typing characters.
| | 06:59 | This is a lot of characters here on the screen.
| | 07:02 | Objective-C, like I said, tends to be
very verbose. Apple's naming conventions
| | 07:06 | tend to be very verbose, and it's the
code completion that makes that practical.
| | 07:12 | With good code completion, we can
have a verbose language that's very, very
| | 07:16 | readable, and still not have to
spend all day typing one line of code.
| | 07:20 | So, I'm just going to save this with
Command+S, and run it with Command+R, and we
| | 07:25 | can see that it works; compiles with no
issues, and there we have klaatu, barada,
| | 07:32 | nikto. This is a string, another
string, and one, two, three; you notice I'm
| | 07:37 | missing some new lines there, and we
see it works exactly as I typed it.
| | 07:42 | So, I'll press Command+Q to quit my
TestBed, and come back to Xcode; you see it
| | 07:47 | says Finished running up
there in the status box.
| | 07:52 | So, Objective-C is very verbose language.
Apple's code and naming conventions
| | 07:57 | make it even more verbose, and this
would be a problem, but for the robust and
| | 08:01 | well implemented code completion in
Xcode that makes coding in Objective-C a
| | 08:06 | relatively pleasant experience.
| | 08:08 | Xcode's code completion system is also
integrated with the Snippets library.
| | 08:13 | In fact, the forin loop that we typed
up there, which implements Objective-C's
| | 08:18 | Fast Enumeration is actually a
snippet from the Snippets library.
| | 08:23 | We'll cover snippets in the next movie.
| | Collapse this transcript |
| Using snippets| 00:00 | Xcode provides a wonderful facility for
defining snippets of code that you can
| | 00:05 | use with code completion.
| | 00:06 | Here I've opened a working copy of
TestBedOSX here in Xcode, and I'm going to
| | 00:12 | hide the navigator pane with Command+0 to
give us a little bit more room for the editor.
| | 00:18 | I'm going to leave the utility pane open,
because we're going to use it in this lesson.
| | 00:22 | On the right-hand side of the screen,
you find the utility pane, and down at the
| | 00:26 | bottom of it is the library pane, and
I'm going to just expand that a little bit
| | 00:31 | by dragging it up, and you'll notice
that it has four sections: a File Template
| | 00:35 | Library, the Code Snippet Library, the
Object Library, and the Media Library.
| | 00:41 | We're going to select the Code Snippet
Library, and I'm going to scroll down here
| | 00:45 | and find the
Objective-C Fast Enumeration.
| | 00:50 | You can also find it by typing fast
enumeration down here in the filter box. So,
| | 00:56 | there it is, and if I click on that,
you'll see this is the foreach loop that
| | 01:02 | we've been using a lot
throughout this code.
| | 01:06 | So, we can find that down here in our
code; there's a version of it, and if I come
| | 01:09 | in here, and I just start typing for,
you'll notice this forin, and if I click
| | 01:15 | on that, you see it comes up in our
code, forin. And there's placeholders for
| | 01:23 | the different things that you need to
make that work; an object, a collection,
| | 01:27 | a set of statements.
| | 01:29 | I can also get that snippet into my
code by simply dragging it from the Code
| | 01:36 | Snippet Library over to
where I want it, and there it is.
| | 01:40 | You'll notice it doesn't come up as
nicely formatted when I drag it, and I don't
| | 01:45 | tend to do it that way anyway.
| | 01:46 | I tend to do it by typing it,
and using the code completion.
| | 01:52 | That's the really easy way to do it;
| | 01:53 | you keep your fingers on the keyboard.
| | 01:55 | Now, this becomes really powerful,
because not only do you get to use all of
| | 02:03 | these snippets that are provided with
Xcode, but you can also create your own.
| | 02:08 | Here in this selector, you find code
snippets for iOS, for OS X, and one called
| | 02:13 | User, and in this user area --
you'll notice it's empty right now --
| | 02:17 | we can create our own snippet,
so let's go ahead and do that.
| | 02:20 | One thing that I tend to do a lot is this
piece of code right here: self messageString:
| | 02:27 | NSString stringWithFormat. That's a lot of
typing, and it's a little bit prone to error.
| | 02:35 | It's several things that are nested,
and so I'm going to create a code snippet,
| | 02:37 | because I tend to use this a lot,
especially in programs like this,
| | 02:41 | which is a TestBed, and I'm going to be
using this to experiment with code,
| | 02:46 | to try out different things, and they're
going to print lots of messages
| | 02:49 | on the screen using this method.
| | 02:51 | So, I'm just going to copy this and I'm
going to paste it down here in an open
| | 02:54 | space. And this part here is going
to be different every time I use it,
| | 03:01 | so I want to have a placeholder here
with descriptive text that says what
| | 03:06 | goes there, and what goes there is a format
string, and a variable number of arguments.
| | 03:10 | And so we type that like this;
| | 03:11 | we use a left angle bracket, and a pound
sign, and this opens the placeholder,
| | 03:16 | then I type the word format string, and
a comma, and three dots, or an ellipses,
| | 03:24 | which means a variable number of variable
typed arguments, and then I close the
| | 03:29 | snippet placeholder with a pound
sign, and a right angle bracket.
| | 03:33 | And so now I have my snippet, and I want
to add it to the library, so I grab all
| | 03:39 | of this, and I simply drag it over
to the user area of the library.
| | 03:45 | You have to have user selected in the
little snippet selector there, and I drag
| | 03:49 | it in, and it starts up a new
snippet, and it's called My Code Snippet.
| | 03:53 | Well, I'm going to change the name of
this, and I'm going to call it Message
| | 03:58 | with Format, and its Summary will
be messageString call with NSString
| | 04:05 | stringWithFormat. And so that's just
a Summary; it's not something I'm ever
| | 04:13 | going to have to type again, but
when I look at it, I'll have an idea of
| | 04:17 | what this snippet is.
| | 04:18 | The Completion Shortcut, we'll just call
this messageWithFormat, and I'm going to
| | 04:24 | say -snippet, and that way
I'll know that it's a snippet.
| | 04:28 | The Completion Scope is Function, or
Method, because it has got this whole self in
| | 04:34 | it and everything, and so
it fits in that context.
| | 04:37 | Different contexts, Class Implementations,
Class Interface Methods, Class
| | 04:42 | Surface Variables, Code Expressions; if
I was just going to make this snippet of
| | 04:46 | just the NSString stringWithFormat, it
would be a code expression, Processor
| | 04:50 | Directives, Strings or Comments,
and Top Level.
| | 04:53 | And so these are our Completion
Scopes; these are the scopes where this
| | 04:58 | completion will be available, and
Function or Method is going to work for this one.
| | 05:01 | So, I say Done, and now I come in here
and I take this whole line out, and I just
| | 05:07 | type -- actually, I'm going to put this
inside of another for loop like this, so
| | 05:11 | we'll just create a for loop, and I'll
make this say one, two, and three, and put
| | 05:20 | in a close bracket there. And here I'm
going to type messageWithFormat-snippet,
| | 05:30 | and there's my whole snippet, and it's
highlighted my placeholder, so I could put
| | 05:34 | what I want here, and I'm just going to
make it percent, at, and a new line, and give it
| | 05:39 | the O, Command+Right arrow to get to
the end of the line, and semicolon.
| | 05:45 | That's a lot less typing, and it's a lot
easier to remember. tThis is a really
| | 05:49 | good use of a snippet.
| | 05:51 | You will find many uses for snippets in
your code. You'll find the things that
| | 05:55 | you tend to do a lot, the various
coding patterns that you tend to use a lot,
| | 06:00 | and you can create snippets out of them,
and it makes it really easy to type, and
| | 06:05 | to get into your code.
| | 06:06 | So, I'm going to press Command+S to
save, Command+R to run, and there we have
| | 06:11 | klaatu, barada, nikto,
and one, two, three.
| | 06:14 | Command+Q to quit the TestBed. And so
these snippets are a realtime saver.
| | 06:19 | The Library Snippets are
mostly pretty intuitive.
| | 06:22 | They'll show up as you type.
| | 06:24 | You can drag them into your code, and
creating your own snippets can be a great
| | 06:28 | time saver for small chunks of
code that you tend to use a lot.
| | Collapse this transcript |
| Accessing documentation| 00:00 | Xcode provides easy access to
the documentation from the editor.
| | 00:04 | Here I've opened a working copy of
TestBedOSX here in Xcode, and I'm going to go
| | 00:11 | to the settings. So, you can get to the
settings by pressing Command+Comma, or by
| | 00:15 | bringing up the Preferences
selection in the Xcode menu.
| | 00:22 | If we click on the Downloads tab
here, and select Documentation from the
| | 00:27 | selector, you'll see that I've installed
the iOS 6.1 document set, and the OS X
| | 00:33 | 10.8 document set, and
the Xcode 4.6 document set.
| | 00:39 | You want to make sure that you've
installed the document sets that you're
| | 00:43 | going to need here,
| | 00:45 | and you'll also want the
documentation for Xcode itself.
| | 00:49 | So, I'll close the Preferences here.
Once you have the Documentation installed,
| | 00:55 | you can access it
in the Organizer at any time.
| | 00:57 | In the Window Menu, you'll see Organizer
is Shift+Command+2, so if I just press
| | 01:06 | Command+Shift+2 on my keyboard here,
and press Documentation tab, I have the
| | 01:13 | full documentation
available at any time.
| | 01:15 | There is the iOS and OS X documentation,
and there's the Xcode documentation.
| | 01:27 | Now, you can search documentation,
like, for example, if I want to bring up
| | 01:31 | NSString, I can search that here, and
there's the class documentation right there
| | 01:39 | for NSString, and other
relevant documentation.
| | 01:46 | If I click on the NSString Class
Reference, that's with the little C there, you
| | 01:51 | see there's the entire class reference
for NSString, and it's actually pretty
| | 01:57 | big, so you can navigate through it
using this jump bar up here. There's your
| | 02:05 | Overview, Subclassing Notes, Adopted
Protocols, Tasks; this is very common. If
| | 02:12 | you want to know how to Create and
Initialize a string, you just click on that,
| | 02:16 | and it jumps to that part of the
documentation set, and there are all the
| | 02:21 | methods for initializing a string. If you want
to initialize it, for example, with a UTF8String,
| | 02:28 | there is the documentation
for an initWithUTF8String.
| | 02:34 | So, here in the Organizer, you can read
the documentation, you can read articles,
| | 02:38 | guides, release notes, library
documentation; there are even code samples.
| | 02:42 | You can search to find what you
need in the index, and this can be very
| | 02:46 | convenient, and it can also cumbersome.
| | 02:49 | So, I'm going to close the Organizer.
| | 02:53 | What's more convenient is that you
can access documentation from your code.
| | 02:58 | This is actually very simple.
| | 03:02 | Here I have an NSString object, and
so I can Option+Click on this NSString
| | 03:11 | identifier here,
so I'll press the Option.
| | 03:14 | When I press the Option key, you see
that my cursor changes, and what I'm
| | 03:18 | hovering over gets highlighted, and when
I click on that, I get this little pop
| | 03:23 | up that describes the class, and gives
me an easy link to both the header file
| | 03:29 | where it's defined,
and the class reference.
| | 03:32 | If I click on the Class Reference, it
opens the Organizer, with that class, just
| | 03:38 | as if I'd search for it.
| | 03:40 | So, I'm going to
close the Organizer again.
| | 03:42 | Now, I'm pressing Command+W this time.
| | 03:44 | I do tend to like to use the keyboard.
| | 03:47 | It's worth noting that a Command+Click
will just show its definition, so I'll
| | 03:52 | Command+Click, and you see when I hold
down the Command key, I get this different
| | 03:56 | highlighting, and when I click, it
just takes me right to the interface
| | 04:02 | definition in the
NSString.h header file.
| | 04:07 | I can press the back button up here in
the jump bar, or if I Command+double-click
| | 04:13 | on it, so here I'm going to double-click,
it opens up in a separate editor
| | 04:18 | window, and I can press
Command+W to close that window.
| | 04:23 | The built in documentation in Xcode is
very thorough, it's very useful, and as
| | 04:27 | you can see, it's
very convenient to get to.
| | 04:30 | It's literally a click away
from the symbol in your editor.
| | Collapse this transcript |
| Using keyboard shortcuts| 00:01 | Back on the 1980s when the graphical
user interface was fairly a new concept,
| | 00:05 | a prominent software engineer named
Seymour Rubinstein, who was instrumental in
| | 00:10 | developing the first word processor for
personal computers, was quoted as saying,
| | 00:15 | A mouse is a wonderful thing if
you happen to have three hands.
| | 00:19 | For those of us who have less than
three hands, and would like to keep our
| | 00:23 | two hands on the keyboard while working,
keyboard shortcuts are a wonderful thing.
| | 00:28 | Here I have opened a working copy of
TestBedOSX here in Xcode, and we have a
| | 00:34 | simple project; an Objective-C
file is open in the Editor.
| | 00:39 | If we want to open the corresponding
header file in the assistant header, we
| | 00:43 | could use the mouse to press the assistant
editor button in the Editor selector
| | 00:48 | up here at the top of the screen, or you
can use the mouse, and go up here to the
| | 00:54 | editor menu, or rather, the View menu.
| | 00:59 | You can use the mouse to hunt through the
menus, and find the assistant editor that way.
| | 01:04 | Now, if you're like me, and you're
impatient, and you have your fingers on the
| | 01:10 | keyboard already, you'd probably
rather just to use the keyboard for this.
| | 01:14 | So, I can press Option+Command+Return,
which is a keyboard shortcut, and that opens
| | 01:21 | my assistant editor, and it opens it
with the counterpart file, the H file, here
| | 01:26 | in that assistant editor, or I can simply
press Command+Return, and go back to the
| | 01:32 | standard editor, and I can use Command+
Ctrl+up arrow to open the corresponding
| | 01:38 | header file in the same editor window,
| | 01:41 | and I can press Ctrl+Command+down
arrow to go back to my previous file.
| | 01:45 | Ctrl+Command+up arrow cycles through the
corresponding files, and if there's just
| | 01:51 | one, then it toggles back and forth, and
the same with Ctrl+Command+down arrow.
| | 01:56 | So, if I'm working on a laptop with a
small screen, and I don't really have a lot
| | 02:01 | of room to have side by side files,
that's a great keyboard shortcut.
| | 02:04 | Xcode has many, many keyboard
shortcuts; too many to remember.
| | 02:09 | If you have a particular function you
like to use a lot, you can usually find
| | 02:13 | the keyboard shortcut in the menu bar.
| | 02:15 | And so, for example, I can come up
here in the View menu, and I can see
| | 02:19 | the standard editor is simply
Command+Return, the assistant editor is
| | 02:24 | Option+Command+Return,
the version editor is
| | 02:27 | Option+Shift+Command+Return, etcetera.
| | 02:32 | All of these shortcuts
are shown in the menu.
| | 02:36 | It is also possible to add, change,
and even remove keyboard shortcuts.
| | 02:42 | Let's take a look at how that's done.
| | 02:43 | I'm going to open up the Preferences, and the
keyboard shortcut for that is Command+Comma.
| | 02:50 | You can also find that in the Xcode
menu under Preferences, and you see the
| | 02:53 | shortcut as listed there;
Command+Comma.
| | 02:57 | Command+Comma is common
across OS X apps.
| | 03:01 | Virtually all OS X apps that have a
preferences pane use Command+Comma to open
| | 03:06 | the preferences, so that's pretty
easy to remember if you use it a lot.
| | 03:11 | Here in the Preferences, you'll notice
these tabs across the top, and one of
| | 03:15 | them says Key Bindings, and when I click on
Key Bindings, I see all the keyboard shortcuts.
| | 03:24 | They're all listed here, and you can
see there is a very, very long list of them.
| | 03:28 | Some of them don't have keyboard
shortcuts assigned to them, and we can add a
| | 03:34 | keyboard shortcut for something that we
would like to have a keyboard shortcut for.
| | 03:38 | For my part, if I'm using an assistant
editor -- I'm just going to close this for
| | 03:43 | a moment, and I'm going to get rid of my
navigator pane by pressing Command+0, and
| | 03:49 | I'm going to get rid of the utility pane
by pressing Option+Command+0, so I just
| | 03:54 | have the editor here.
| | 03:56 | And I'm going to open my assistant
editor by pressing Option+Command+Return.
| | 04:01 | Now, if I want to add an assistant editor,
if I want to be editing more than one
| | 04:06 | file on the right-hand side here, I
can come up to the View menu, and go to
| | 04:12 | Assistant Editor, and I can
say Add Assistant Editor.
| | 04:15 | You'll notice that there's
no shortcut for that.
| | 04:17 | Well, I would like to have a shortcut
for that, and I would like it to be like
| | 04:22 | the remove one, except with an A
instead of a W, so it would be
| | 04:27 | Ctrl+Shift+Command+A. So, let's go ahead and
create a shortcut for Add Assistant Editor.
| | 04:34 | So, I'm going to come back up here, I'm
pressing Command+comma to bring up my
| | 04:39 | Preferences again, my Key
Bindings tab is selected,
| | 04:43 | and the first thing I'm going to do is
I'm going to create a new command set.
| | 04:47 | I don't want to edit the default command
set; I'd like to have that to fall back to.
| | 04:52 | So, I'm going to come down here, and
press the Plus button, and I'm going to
| | 04:55 | say duplicate default.
| | 04:56 | And this says default to, I'm just
going to call it working, so that'll be my
| | 05:02 | working command set.
| | 05:05 | And over here in the search box, I'm
going to type in Add Assistant -- there it is:
| | 05:10 | Add Assistant Editor, and I'm going to
come over here to key, and press that once,
| | 05:17 | and I'm going to press Ctrl+Shift+Command+A,
and you see that comes up in there
| | 05:24 | and I'm just going to move my,
cursor out of this and click.
| | 05:28 | If I press Enter, it could ruin that, and I
just want that to be right there like that.
| | 05:34 | So now I've added a shortcut key for
Add Assistant Editor. If I come up here to
| | 05:39 | my View menu under Assistant editor,
| | 05:41 | you see Add Assistant
Editor now has that shortcut key;
| | 05:45 | it's even listed in the
menu, so that I can remember it.
| | 05:48 | Well, that's great, so I'm going to
close my Preferences pane, and I'm pressing
| | 05:52 | Command+W to do that.
| | 05:54 | And now I'm going to press Command+Shift+
Ctrl+A, and it adds an assistant view.
| | 05:59 | Delete assistant editor is already
Shift+Ctrl+Command+W, so I can press that,
| | 06:05 | and it gets rid of it.
| | 06:07 | So, as you can see, it's relatively
simple to add, edit, and delete
| | 06:10 | keyboard shortcuts.
| | 06:11 | If I bring up the Preferences again, I'm
pressing Command+Comma, and I'm going to
| | 06:15 | search for Add Assistant Editor,
and there it is.
| | 06:22 | If I want to delete this shortcut, I
just press the minus, and that shortcut is
| | 06:27 | gone, and if I want to add it back,
I just press plus, and I press
| | 06:33 | Shift+Ctrl+Command+A,
and there it is.
| | 06:37 | I tap outside of it, and there it is.
| | 06:40 | So, I'll close that with Command+W. When
you're using the keyboard for a lot of
| | 06:46 | typing, keyboard shortcuts
can be a real efficiency boon.
| | 06:51 | You can keep your fingers on the keyboard,
and especially if you type by touch,
| | 06:55 | you don't even have to take your eyes
off the screen to use a keyboard shortcut.
| | 07:00 | Xcode allows you to add, change,
and remove keyboard shortcuts.
| | 07:03 | For most purposes, I suggest you retain
the defaults as much as possible, and
| | 07:08 | create a new Command set if you need
to add one or two keyboard shortcuts.
| | Collapse this transcript |
| Using tabs| 00:00 | Xcode has a tab interface that is very
powerful, and it's also a bit different
| | 00:05 | than you may expect.
| | 00:07 | Here, I've opened a working
copy of TestBedOSX here in Xcode.
| | 00:12 | In Xcode, with each tab, you
get a completely independent
| | 00:15 | single window interface.
| | 00:16 | I'm going to press Command+T to open a
new tab, and you see we now have two tabs,
| | 00:25 | and if I select one, and I select
the other one, they look exactly the same,
| | 00:31 | and that tab actually includes everything
on the screen here under the tab bar.
| | 00:37 | For example, if I press Command+0
to get rid of the navigator, and
| | 00:43 | Option+Command+0 to get rid of the
utilities inspector, and then I switch
| | 00:50 | over to the other tab,
| | 00:51 | you'll notice that the other tab
still has the navigator and the utilities,
| | 00:57 | and the tab that I was using
does not have them.
| | 01:00 | So, that entire interface, everything
below the tab bar, is part of the tab.
| | 01:06 | Like with most OS X applications,
you can navigate between tabs with
| | 01:11 | Shift+Command+left bracket
and Shift+Command+right bracket.
| | 01:17 | You'll notice in the Window menu here,
it says Select Next Tab, Select Previous
| | 01:22 | Tab; it just says Command+left curly bracket
and Command+right curly bracket.
| | 01:27 | That's actually the same thing, because
the only way to get the left and right
| | 01:31 | curly brackets is with the Shift key, so
| | 01:33 | I tend to think of it as Shift+Command+left bracket
and Shift+Command+right bracket, even though it's
| | 01:40 | technically, I guess, Command+left curly bracket and
Command+right curly bracket, but this is the same in most
| | 01:47 | tabbed applications in OS X.
| | 01:50 | I'm going to close this tab;
| | 01:52 | I'm going to press Command+W to close
the tab that I have currently selected.
| | 01:57 | Now, on the other tab, you notice my tab
bar goes away, because I only have the one tab.
| | 02:01 | Now, if I want to open a different file
in a new tab, I actually need to change
| | 02:08 | a setting in Xcode's Preferences.
| | 02:10 | So, I'm going to open the Preferences
with Command+Comma, and select the General
| | 02:15 | tab over here in the Preferences.
| | 02:18 | You notice here, Navigation, Optional
Navigation, Double Click Navigation.
| | 02:23 | So, if I want to double-click and have it
open a file in a new tab, which I think
| | 02:29 | ought to be the default, but it's not,
so I have to open this little dropdown,
| | 02:36 | and select Use Separate Tab.
| | 02:38 | You notice down here, this changes now,
Double Click opens in separate tab.
| | 02:42 | So, this describes the current settings.
| | 02:46 | If I put this back, you see that says
separate window, and if I make this a
| | 02:50 | tab, it says Opens in separate tab.
| | 02:53 | So, I'm going to press Command+W
to close the Preferences.
| | 02:57 | Now I'm going to double-click on
my .h file, and it opens in a new tab.
| | 03:06 | This tab has the .m file,
and this tab has the .h file.
| | 03:12 | I guess the last time I did this, I closed the
utilities, and it looks like it remembered that.
| | 03:17 | So, I can open that up with Option+Command+0,
or close it with
| | 03:21 | Option+Command+0. Tabs in Xcode may
take a bit of getting used to if you're
| | 03:26 | already familiar with a different
editor or IDE, but they are very useful, and
| | 03:31 | functional, and they are also
used with Xcode's behaviors.
| | 03:35 | We'll cover behaviors
in the next movie.
| | Collapse this transcript |
| Using behaviors| 00:00 | The editor in Xcode has a lot of
different features and options that can be
| | 00:04 | enabled or disabled
depending on how much you want them.
| | 00:07 | These include different display features,
sounds, notifications, and other behaviors.
| | 00:12 | You can change a lot of this on the
fly using the mouse, or the keyboard
| | 00:16 | shortcuts, or you can
automate a lot of this using behaviors.
| | 00:20 | Here, I've opened a working copy of
TestBedOSX here in Xcode, and I'm going to
| | 00:25 | open up the Preferences by pressing
Command+Comma, and I'll select Behaviors here,
| | 00:31 | and then down under Running,
I select Pauses.
| | 00:34 | This is a behavior that gets triggered
whenever the execution of your program pauses.
| | 00:39 | So, let's say you've written a program
in Xcode, and you got to run it, you
| | 00:44 | press Command+R, or you press the
big Run button on the toolbar, and your
| | 00:48 | application is running.
| | 00:50 | For some reason, it has to stop running;
it hits an error, or hits a breakpoint,
| | 00:54 | and the execution pauses.
| | 00:56 | That triggers this behavior.
| | 00:58 | So, the way that it works now, it shows the
debug navigator, and it shows the debugger.
| | 01:03 | So, if I come in here and trigger this
behavior -- I'm going to put in a breakpoint.
| | 01:08 | I'm going to put it right
here on line 30.
| | 01:10 | So, I just click on the
Breakpoint bar, and it sets a breakpoint.
| | 01:15 | Now I'm going to press Command+R on my
keyboard, and that will build and run the code,
| | 01:21 | and it stops at the breakpoint.
| | 01:23 | When it stops at the breakpoint, notice
the breakpoint navigator comes up, and
| | 01:28 | the debugger comes up.
| | 01:30 | Pressing Command+Comma again, this is
exactly what the Pauses behavior says to do.
| | 01:35 | So, I can modify this.
| | 01:37 | I can have it do this same thing, but do it
in its own tab, which I actually like doing.
| | 01:43 | I like the way this works better.
| | 01:46 | So, I click on Show tab named,
and I put in a name.
| | 01:50 | It's important to press the
Return key after you type the name.
| | 01:53 | Otherwise, believe it or not,
it's a bug.
| | 01:56 | It will not open that tab.
| | 01:57 | Now, I'll close the Behaviors, and
I'm going to stop the running code by
| | 02:03 | pressing Command+Period,
| | 02:07 | bring up my file navigator again, or
my project navigator, and now when I
| | 02:11 | run this -- I'm going to press Command+R --
it hits the breakpoint, it opens up
| | 02:16 | the debug navigator,
| | 02:18 | it opens up the debugger, but it
does all of these in a new tab.
| | 02:21 | So, this tab is called Debug.
| | 02:23 | My old tab is still there, and it
still has the project navigator, and it
| | 02:27 | doesn't have the debugger.
| | 02:28 | So, this is actually really convenient.
| | 02:30 | I can go back and I can work on my code,
and leave the debugger running or not,
| | 02:35 | and it does these two
tasks in two different tabs.
| | 02:38 | This is why I like to do it this way.
| | 02:40 | I'm going to press Command+Period,
and I'm going to remove this breakpoint
| | 02:47 | by Ctrl+Clicking or right-clicking on the
breakpoint, and selecting Delete Breakpoint.
| | 02:53 | I'm going to go ahead
and close my Debug tab.
| | 02:56 | So, that's how you can modify a behavior.
| | 02:58 | You can also create a new behavior.
| | 03:00 | So, let's bring up the Preferences
again with Command+Comma, and I'm going to
| | 03:05 | press on this little button down here
at the bottom of the Behaviors, this
| | 03:08 | little plus button, and that will add a
new behavior, and I'm going to name this Edit.
| | 03:13 | So, this is the behavior for
when I'm just editing code.
| | 03:15 | I'm going to edit code
in its own tab named Edit.
| | 03:19 | I'm going to hide the
navigator altogether.
| | 03:24 | I'm also going to make sure that the debugger
is hidden, and the utilities are hidden.
| | 03:31 | I'm going to Show my editor
as an Assistant Editor.
| | 03:35 | I have to name this tab, or it won't
open one, so I'm going to name it Edit, and
| | 03:40 | press the Return key,
so it keeps that setting.
| | 03:43 | So, this is how I want
my editing to happen.
| | 03:45 | If I close this, and I come up here in
the Xcode menu under Behaviors, and select
| | 03:51 | Edit, I now get a new tab called
Edit, and it has no navigator, it has no
| | 03:58 | utilities, and it's just
space for me to edit.
| | 04:01 | Now, another nice feature about this, if
I come back in here to my Preferences --
| | 04:07 | I want to press Command+Comma -- and
select my Custom Edit behavior, I can go
| | 04:13 | over here and I can
set a shortcut key for it.
| | 04:16 | I'm going to set Shift+Option+Command+E,
and I'm using that shortcut because it's
| | 04:24 | not used by anything else.
| | 04:25 | The Shift+Option+Command
shortcuts tend to be available.
| | 04:29 | I have to click outside of that shortcut
area. I can't press the Return key,
| | 04:33 | because it will try to assign it to
the behavior, and yet I need to click
| | 04:38 | outside of it; I need to make it set,
otherwise it doesn't remember the shortcut key.
| | 04:44 | Now, we have a shortcut
key set up for this.
| | 04:46 | I'm just going to press Command+W
to close the Preferences, and
| | 04:49 | I'm going to press Shift+Option+Command
+E, and it will switch me to my already
| | 04:57 | open Edit tab, and if I don't have
that already open, and I press that key
| | 05:01 | combination, Shift+Option+Command+E, it
will create that tab, and set it up the
| | 05:07 | way that I want it set up.
| | 05:09 | So, this is really awesome.
| | 05:10 | I've created a new behavior, I've set a
shortcut key for it, and what's really
| | 05:15 | cool about this is, if I set another
behavior -- I'm just going to go ahead and
| | 05:20 | create another behavior real quick.
| | 05:21 | We're going to call this UI, and
that's for editing user interfaces.
| | 05:27 | I'm going to put it in its own
tab named UI, press the Return key,
| | 05:32 | and I'm going to, again, hide the
navigator, and hide the debugger.
| | 05:37 | This time I want to
show the utilities bar,
| | 05:41 | and I'm going to also show my
editor as the Assistant Editor.
| | 05:48 | This is going to be Shift+Option+Command
+U, and again, I have to click outside
| | 05:53 | of that, so it's actually set.
| | 05:55 | Now I can come over here and I can
press Shift+Opton+Command+U, and it creates
| | 06:00 | a new tab, it's called UI.
I might need to select my window,
| | 06:05 | and it opens at exactly
the way I wanted it.
| | 06:07 | I have my utility pane, I have my
assistant editor, and I have my UI.
| | 06:13 | From here, now, with my keyboard, I
can jump to whichever tab I want,
| | 06:18 | Shift+Option+Command+E will get me to
my editor, Shift+Option+Command+U will
| | 06:24 | get me to my UI editor.
| | 06:27 | So, you can see that the
Behaviors is incredibly powerful.
| | 06:31 | Given how complex Xcode is -- the
screen can become cluttered, it can become
| | 06:35 | destracting -- the Behaviors feature
is an excellent tool for making Xcode's
| | 06:40 | complexity work for you, and for
keeping the destractions out of the way
| | 06:44 | while you're working.
| | Collapse this transcript |
| Quickly finding a symbol| 00:00 | Sometimes when you're working on a
project, you just need to find where a
| | 00:04 | particular symbol is defined.
| | 00:06 | Xcode provides a simple and powerful
feature called Open Quickly, that allows
| | 00:10 | you to quickly find that symbol,
and open the file where it's defined.
| | 00:13 | Here, I've opened the working copy of
BWRSS in Xcode, and I'm going to search for
| | 00:21 | a symbol called getFeedIDs.
| | 00:24 | I can find the Open Quickly in the File
menu, and there it is, or I can simply
| | 00:29 | press the shortcut key Shift+Command+O.
I'm going to press Shift+Command+O, and
| | 00:36 | I just typed the symbol in
there, and I typed getfeedids.
| | 00:41 | I don't even need to
capitalize it properly.
| | 00:44 | There it is; it finds it.
| | 00:45 | It's defined in RSSDB.h on line 20.
| | 00:50 | I can just press the Return key here
on my keyboard, or I could click the Open
| | 00:55 | button, but I'm just going to press the
Return key on my keyboard, and there it's
| | 00:59 | opened the file,
and it's found the symbol.
| | 01:02 | This opens the file in the current editor,
and it just works very simply, and very well.
| | 01:09 | The Open Quickly feature
is deceptively powerful.
| | 01:12 | It's especially useful when you're
working on a large project with a lot of
| | 01:16 | files, as in this one.
| | 01:18 | In fact, this file isn't
even in this list over here.
| | 01:22 | It's over under this BWDB.
| | 01:26 | So, this simple and powerful feature
helps you find the symbol, open the
| | 01:30 | corresponding file,
and jump to the symbol.
| | Collapse this transcript |
|
|
3. Version ControlUnderstanding version control| 00:00 | Version control, sometimes called revision
control, or source code management, is
| | 00:05 | a system that manages changes to your
code with varying degrees of granularity.
| | 00:10 | In simple terms, it allows you to
recover different versions of your code,
| | 00:14 | should you wish to discard some changes,
or maintain different branches of the
| | 00:17 | same code, like a development version,
a data version, and a release version.
| | 00:23 | The version control system manages a
directed graph, like a tree of deltas or
| | 00:28 | changes to your code base.
| | 00:30 | This allows you to maintain branches of
your code base, and merge them together later.
| | 00:35 | In a simple application, you may
just want to keep track of one chain of
| | 00:39 | development as a powerful
and flexible backup system.
| | 00:43 | In a more complex application, your
project may be a coordinated effort with
| | 00:47 | many developers, and various
testing, and release versions.
| | 00:51 | There are many different
systems available for version control.
| | 00:54 | Xcode provides support
for two: Git, and Subversion.
| | 01:00 | Git was originally developed by Linus Torvalds,
the original author of the Linux Kernel.
| | 01:05 | The inspiration for Git was the large
distributed network of developers working
| | 01:09 | on the Linux Kernel.
| | 01:11 | They had tried other systems with
varying levels of success, and eventually,
| | 01:15 | Linus decided to write his own instead.
| | 01:18 | His intention in creating Git was to
build a source code management system that
| | 01:22 | was fast, efficient, distributed, simple,
and free for use with the very large,
| | 01:28 | very distributed Linux Kernel
development team.
| | 01:31 | Git is extremely flexible.
| | 01:33 | It's just as good at supporting
one developer with one repository on
| | 01:37 | one computer, as it is with a
large group of developers using
| | 01:40 | distributor depositories.
| | 01:41 | Xcode also provides support for a
version control System called, Subversion.
| | 01:48 | Subversion is a more traditional
Version Control system modeled after an
| | 01:52 | older system called CVS.
| | 01:54 | Subversion requires a server to
manage the repository, and a client to
| | 01:59 | check files in and out.
| | 02:01 | The server can be run on the same system as
the client, or on a server at a remote location.
| | 02:07 | Subversion may be a good choice for
developers who prefer or are more familiar
| | 02:11 | with traditional CVS-based
version control systems.
| | 02:15 | In my mind, Git is a superior
choice for most new projects.
| | 02:19 | It's simple, robust, and fast, both
for local and distributed repositories.
| | 02:25 | The examples in this course use Git.
| | 02:28 | The techniques for using
Subversion are very similar.
| | 02:31 | Both Git and Subversion have many more
features than we'll cover in this course.
| | 02:37 | For more details about version control,
including more complete tutorials on
| | 02:41 | both Git and Subversion, see the
course Fundamentals of Software Version
| | 02:45 | Control, here on the lynda.com
Online Training Library.
| | Collapse this transcript |
| Creating a project with Git support| 00:00 | Creating a new project with
Git support is easy.
| | 00:03 | You simply check a checkbox that says,
Create local Git repository for this
| | 00:07 | project in the project
save dialogue box.
| | 00:11 | It really is that simple.
Let's take a look.
| | 00:13 | I'm going to start Xcode, and I can
either press this button here in the welcome
| | 00:19 | box that says Create a new Xcode
project, or I can come up here to the File
| | 00:24 | menu, and under New, select Project.
| | 00:26 | This is the new project dialogue box.
| | 00:29 | I want to select application under
OS X, and select Cocoa Application inside
| | 00:36 | there, and press Next.
| | 00:38 | I'm just going to name this Temp, and
under Organization Name, you'll have your
| | 00:42 | own organization, your Company Identifier.
| | 00:45 | Class Prefix can be whatever works
for you; for this purpose, it really
| | 00:48 | doesn't matter much.
| | 00:50 | You want to make sure that Create
Document-Based Application is not checked, and
| | 00:54 | Use Core Data is not checked and Include
Unit Tests is not checked, but you do
| | 01:00 | want to check
Automatic Reference Counting,
| | 01:03 | and select Next. I'm just
going to store this on my Desktop.
| | 01:06 | You'll save it wherever
it works for you.
| | 01:08 | You just want to be sure you can
find it, so you can delete it when we're
| | 01:12 | done with this movie.
| | 01:13 | Under Source Control, you want to check
the checkbox that says Create local git
| | 01:17 | repository for this project.
| | 01:18 | That's what creates the Git repository.
| | 01:20 | I'm going to press the Create
button, and it's that simple.
| | 01:24 | We now have a new project
with Git Support.
| | 01:27 | I'm just going to select the AppDelegate.m
file, and whatever prefix you selected
| | 01:32 | in the creation of this project
will be there. Mine says BW.
| | 01:38 | Just under Insert code here, I'm just
going to put a line of code, and the line
| | 01:43 | of code is going to be NSLog, and I'll
tab over, and @"%s",__ and I'm going to
| | 01:53 | select the one that says Function
there, and put in a semicolon.
| | 01:58 | You'll notice over here that little m is
grayed out; that means that the file is modified.
| | 02:04 | That m means it's a .m file;
an Objective-C source file.
| | 02:08 | It's grayed out when the file is modified,
but it has not been saved yet, and
| | 02:13 | now you see that m appears over there.
| | 02:15 | After a period of time, it checks, and
it sees that it's modified, which means
| | 02:19 | that it's different than
what's stored in the Git repository.
| | 02:22 | This indicator here is
the source control status.
| | 02:26 | When there's an m, it means that the
file is different than the file in the
| | 02:30 | repository, that it has not been
committed yet, and that it is modified.
| | 02:35 | If we come over here to the file inspector
on the right-hand side, and scroll all
| | 02:40 | the way down, you see
the source control section.
| | 02:43 | There's a version, which is generated by
the system, and then there's status, which
| | 02:48 | says m, and modified, and then the
location of the file, in this case, you can
| | 02:53 | see it's on my Desktop.
| | 02:55 | If I press this Discard button, it
will discard the changes. Note, this
| | 03:00 | operation cannot be undone, and it will
it bring it back to the last committed;
| | 03:04 | what's called the
head of the repository.
| | 03:07 | So, if I press that Discard Changes,
you'll notice that everything that I typed
| | 03:11 | there is gone; that m is gone, the m
over here, the icon is not shaded anymore,
| | 03:17 | and under Source Control it
says, Status: No changes.
| | 03:19 | So, that's just an easy way to get
back to the last committed state.
| | 03:24 | I'm going ahead to put that line back in
here, and this time, I'm going to press
| | 03:32 | Command+S to Save the file.
| | 03:34 | When I do that, you see that now that m
icon is not shaded anymore, but we still
| | 03:39 | have this Modified status.
| | 03:40 | If we look over in Source Control under
the file inspector, you'll notice that
| | 03:45 | the Status says Modified.
| | 03:48 | That means that our file is now saved,
because this m is not shaded, and it's
| | 03:53 | been changed since it's been
committed to the repository.
| | 03:57 | Now I'm going to bring up the version
editor, and that's this one on the right
| | 04:03 | over here in the Editor selector,
and I'm going ahead and get rid of the
| | 04:07 | Uutilities pane over there, so
we have a little bit more room.
| | 04:11 | Here in the version editor, you'll
notice that it shows the change that I've
| | 04:16 | added this line since the version on
the right, and the version on the left is
| | 04:21 | the local revision, and the version on
the right is base head, which means that
| | 04:25 | it's the head of the repository;
| | 04:27 | it's the last committed version.
| | 04:29 | If I click on this icon here,
that will bring up the timeline.
| | 04:33 | You see on the left, it says local,
and on the right, it says base, and
| | 04:37 | there aren't any others.
| | 04:38 | Now, if I commit this change, and I'm
just going to right-click on this file, or
| | 04:43 | Ctrl+Click on it, and come down here to
Source Control, and say Commit Selected
| | 04:48 | Files, it requires you to
put in a comment here.
| | 04:51 | It won't allow you
to commit if you don't.
| | 04:53 | If I put press that, it says A commit
message is required, so I'm just going to
| | 04:57 | say, This is a commit message,
and Commit 1 file.
| | 05:06 | Now you see our m has disappeared,
and our base and local are the same.
| | 05:11 | If I put this back, you'll see
that they're exactly the same.
| | 05:15 | I can, if I want, in the right-hand
side -- either side; I'm going to do it on
| | 05:20 | the right-hand side -- I can go back to the
previous version, and I can see those changes.
| | 05:25 | Those changes still exist.
| | 05:26 | I could revert back to
that if I wanted to.
| | 05:31 | So, It's really easy to create a project
with a local Git repository in Xcode.
| | 05:35 | We'll look at more details
in the rest of this chapter.
| | Collapse this transcript |
| Adding Git to an existing project| 00:00 | In the last movie, I showed you how to
create a new project with Git support.
| | 00:04 | If you already have a project that was created
without a Git repository, you may still add one.
| | 00:09 | Here's how.
| | 00:11 | First, we're going to open up
Xcode, and create a new project.
| | 00:13 | I'll come down here
to my dock, and start Xcode.
| | 00:16 | I'm going to select create a new
project from the welcome screen here.
| | 00:21 | Of course, I can also use the
File menu, under New Project.
| | 00:24 | I'm going to select the application
under OS X, and Cocoa Application, press Next.
| | 00:32 | I'm going to name this Temp-nogit,
and I'm going to make sure that Create
| | 00:39 | Document-Based Application is not
checked, and core data is not checked, and
| | 00:42 | Include Unit Texts is not
checked, and press Next.
| | 00:46 | I'm going to save this on my Desktop.
| | 00:49 | In this case, I'm going to make sure
the Create local git repository is not
| | 00:53 | checked, so if it is checked, you want to
uncheck it for this purpose, and click Create.
| | 00:59 | Now, we have our new project. You'll
notice that when I make a change in one of
| | 01:04 | these files -- I'm going to select
AppDelegate, and just type in a line of code
| | 01:08 | here, NSLog(@"%s",__FUNCTION__);
| | 01:17 | I'm going to press Command+S to save,
and you'll notice that this m icon will
| | 01:21 | change from gray to solid white, and
that means that the file is now changed
| | 01:27 | in the file system.
| | 01:28 | If we come over here to the file
inspector, and scroll down, you'll notice
| | 01:32 | there's no section now for Source
Control and there's no little m over here in
| | 01:37 | the project navigator.
| | 01:39 | We do not have a Git repository.
| | 01:41 | I'm going to press Command+Q now to
quit Xcode, because Xcode can get confused
| | 01:47 | if you change the Source Control
Status of a project while it's running.
| | 01:51 | Now, we're going to add a Git
repository to this existing project.
| | 01:57 | I don't have the terminal application
in my dock, so I'm going to press
| | 02:00 | Command+Space to bring up a Spotlight
search field, and just start typing
| | 02:04 | terminal, and when I see Terminal under
top hit, I press Return, and that opens up
| | 02:10 | the terminal application.
| | 02:12 | This is set up with all the defaults, so
if you have changed how your terminal
| | 02:16 | works, or you've changed your shell, I'm
going to assume you know how to work it,
| | 02:20 | but in the default, I can just type cd
for change directory, and space, and start
| | 02:25 | typing Desktop, and just press the Tab
key for the tab completion, and change
| | 02:31 | directory to my Desktop where this
Temp-nogit is, and that's our project.
| | 02:36 | I type ls to list directory, you
see we have ExerciseFiles and Temp-nogit.
| | 02:40 | Again, I'm going to change directory
into my Temp-nogit, and I'll just start
| | 02:45 | typing, and I press the Tab key, and I'm
going to type ls -al, and that will show me
| | 02:51 | all the files in this directory, even
the hidden files, and you notice that this
| | 02:56 | is all that's in that directory.
| | 02:57 | There's a dot, which is the metafile
that points to the current directory, and
| | 03:02 | there are two dots that point to the
previous directory; the parent directory.
| | 03:05 | Then there's a directory called Temp-nogit,
and another directory called
| | 03:10 | Temp-nogit.xcodeproj, and
that's' actually the project bundle.
| | 03:13 | That's how it shows up
in the UNIX file system.
| | 03:17 | In this directory, this is where we want
to create the Git repository; in the top
| | 03:22 | directory of your project. So, I've
changed the directory into my Desktop, which
| | 03:27 | is the folder that contains the project,
and then I changed directory into the
| | 03:32 | project itself, which is Temp-nogit.
| | 03:36 | That contains another directory with
the same name, but it's this top directory
| | 03:42 | of the project, which is where
we want to put the Git repository.
| | 03:47 | We're going to type a few Git commands
in order to create the repository, so
| | 03:51 | I'm going to type git space init.
| | 03:55 | That will actually
create the repository.
| | 03:58 | I'll press Enter there; Initialized empty
Git repository in, and it gives the path.
| | 04:04 | Now if I type ls -al again, you'll
notice that there's a new hidden file. Files
| | 04:09 | that begin with a dot are hidden
files, and it is called .git, and it is a
| | 04:13 | directory. That's this first letter
over here; d means that it is a directory.
| | 04:18 | If I type ls -al .git, we can see the
contents of that directory, and you can see
| | 04:24 | that that's all set up for git.
| | 04:27 | Now that we have created the Git
repository, we need to add all of our files
| | 04:31 | to it, so we type git add dot; remember
dot is a metadirectory that points to
| | 04:38 | the current directory.
| | 04:39 | I'm going to type, again, git commit,
and this will commit all of our existing
| | 04:45 | files to the directory, -m, and I'm just
going to say, "Initial commit" because
| | 04:51 | remember, you always have to have a
comment for any commit action in git, or in
| | 04:57 | most source code management systems.
| | 05:01 | I can scroll back up here a little bit.
| | 05:04 | We get all of these that says that
Initial Commit, Committer, and it gives some
| | 05:09 | instructions and stuff.
| | 05:10 | We're actually using git
at the command line.
| | 05:13 | That's all we need to do.
| | 05:14 | We have now added the Git repository,
we've initialized it, we've added our
| | 05:18 | files to it, and we've committed our
files to it, and so I can type Exit, and
| | 05:24 | that logs me out, and I can
press Command+Q to quit Terminal.
| | 05:27 | Now, when I come back into
Xcode, you see there's my recents.
| | 05:34 | I can just double-click on that,
and it'll open up this project.
| | 05:38 | Now, we actually have a Git repository
on this project, so I'm just going to
| | 05:42 | duplicate this line.
| | 05:43 | Pressing Command+C, and Command+V
twice, and now I have two lines.
| | 05:49 | I'm going to press Command+S to Save, and
you notice that an m comes up over here,
| | 05:54 | and if I look over here at my file
inspector and scroll down, I now have a
| | 05:59 | Source Control section, which
shows that this file is modified.
| | 06:03 | I can come up here into my version
editor, and I'm just going to remove the
| | 06:09 | utilities pane over there, and you see
the change is logged, and if I bring up the
| | 06:14 | timeline, you see
we have base and local.
| | 06:16 | We now have a Git repository.
| | 06:20 | I can commit this, right-clicking or
Ctrl+Clicking on the file under Source
| | 06:25 | Control, Commit Selected Files,
a comment, and Commit 1 file.
| | 06:31 | Now our base and local are the same,
and if I select over here, I've got the
| | 06:38 | previous revision, so there's our
differences, and you see that these show up in
| | 06:43 | the timeline. Because I made that change
while this was open, I had to select it again.
| | 06:48 | Now, our timeline, we can look at the
base, we can look at the previous version,
| | 06:53 | and we can look at the local version.
| | 06:55 | Now we have successfully added a
Git repository to an existing project.
| | 07:00 | It does require a little bit of command
line work, but it's really not too hard
| | 07:04 | to add a Git repository
to an existing project.
| | 07:07 | We'll learn more about Xcode's
version control support in the rest of
| | 07:10 | this chapter.
| | Collapse this transcript |
| Committing changes| 00:00 | As you work on a project with version
control, you will want to commit your
| | 00:04 | changes to a repository now and then.
| | 00:06 | When you do this, you create a recovery
point in the version control system, so
| | 00:11 | that you can refer back
to it in the future.
| | 00:13 | Here I've opened a working
copy of TestBedOSX here in Xcode.
| | 00:17 | I'm going to hide the utility pane
with Option+Command+0, just to give us a
| | 00:22 | little more room on the screen.
| | 00:24 | You don't need to do that if you have
enough room on your screen. And now I'm
| | 00:28 | going to go ahead and I'm going to make
just a little change to this project, so
| | 00:32 | that we have something that we can
work with in the version control system.
| | 00:36 | Here in the runTest method,
I'm just going to add a line: self
| | 00:39 | messageString:@"this is a new line"
and I'm going to put in a new line at the
| | 00:46 | end, and a semicolon after that, and
you'll notice that the M icon for the file is
| | 00:54 | gray now, because I haven't pressed Save yet.
| | 00:56 | So I'm going to press Command+S
to save, and now it's saved.
| | 01:00 | And this other little m comes, up and
that means that it's modified in the
| | 01:05 | version control system.
| | 01:07 | So, if we bring up our version editor,
you'll notice that we have this change here.
| | 01:13 | On the left-hand side, we
have the local revision;
| | 01:15 | on the right-hand side, we
have the base of the repository.
| | 01:19 | And you'll notice in the scrollbar at
the right, there's a little red line
| | 01:22 | there, and that red line means that there's
a change right around that spot in the file.
| | 01:28 | So, I've made my change.
| | 01:30 | The revision control system
sees that change.
| | 01:32 | Now I want to commit that change.
| | 01:34 | To commit a change, I select the file.
| | 01:37 | I can select more than one file if
there are changes in more than one file.
| | 01:41 | In this case, I just have a change in
one file, so I'm going to select just
| | 01:45 | that one file, and I'm going to Ctrl+click
on it, or right-click on it there
| | 01:50 | in the project navigator.
| | 01:52 | In the context menu, I'm going to
scroll down to Source Control, and select
| | 01:57 | Commit Selected Files.
| | 02:00 | This gives me a new change view, and I
can look at all the changes in my various
| | 02:05 | files; I can select files on the left,
I can look at the changes on the right,
| | 02:09 | and when I'm sure that this is
something I actually want to commit to the
| | 02:13 | repository, then I need to type a
comment down here in the comment box.
| | 02:17 | If I don't do that, and I try to press
the Commit button, I get this message that
| | 02:21 | a comment is required
to perform this operation.
| | 02:26 | And so I'm just going to type a little
message here, Added a message, and
| | 02:31 | press the Commit button.
| | 02:35 | So, now we can see here in the version
editor that the local revision and the
| | 02:39 | base are now the same.
| | 02:40 | There are no red lines here on the
scrollbar, and this message, this is a new
| | 02:47 | line, is on both sides.
| | 02:50 | If I bring up the timeline view, I can
move this right side back a version; we
| | 02:56 | can see the change there.
| | 02:59 | Now, if I want to revert this change,
the easy way to do this is we'll go
| | 03:03 | back to the non-timeline version, where
we have this little one; that shows that
| | 03:07 | there is one change there.
| | 03:10 | And if I click on that, I get a little pop-up
that says Discard Change, and I can press that.
| | 03:15 | The selected change will
be reverted. I may undo it.
| | 03:20 | I'll press OK, and that
change has now been reverted.
| | 03:24 | You see my file in the project navigator
now has the gray m, which means there is
| | 03:29 | a change to that file.
| | 03:30 | And if I press save, Command+S, I get
an m saying that it's been modified, and
| | 03:37 | that modification has not
been committed to the repository.
| | 03:41 | So, if I go back now to the timeline
view, and select base on the right, I see
| | 03:46 | that these files are now different.
| | 03:48 | The base has that line, and the local
revision does not, so by reverting that
| | 03:53 | change, I have actually made a change to
my local file, and that can be committed
| | 03:58 | to repository again, I
can make different changes;
| | 04:01 | I can do different things with it.
| | 04:03 | So, committing changes to the repository
is an important part of managing your
| | 04:07 | source code with a
version control system.
| | 04:10 | The integrated tools in Xcode make
this very easy, so you can focus on your
| | 04:14 | coding, while still keeping
the repository up to date.
| | Collapse this transcript |
| Exploring changes| 00:00 | With the version editor, it's really easy
to browse through the changes in a file.
| | 00:04 | I've opened a working copy of BWRSS
here in Xcode, and the utility pane is
| | 00:09 | already hidden on my copy.
| | 00:11 | If it is or it isn't yours, you
can set it the way that you like.
| | 00:15 | This is the project for
the 3.0 version of my BWRSS.
| | 00:19 | This was a complete rewrite from scratch,
so I don't have the entire history of
| | 00:23 | the project in here.
| | 00:25 | I do tend to rewrite
stuff from scratch.
| | 00:27 | I find that I get a better
quality of code that way.
| | 00:30 | If I bring up the version editor,
you can see that, of course, these files
| | 00:35 | are all committed, so on the right,
using the jump bar, I can jump to an
| | 00:40 | earlier version if I like.
| | 00:42 | This view doesn't show the comments, so
it's hard to tell which version it is.
| | 00:46 | If I bring up the timeline, then
it's easier to see the comments.
| | 00:50 | Here's one that says, The iPad version
is now working with some issues to fix.
| | 00:55 | And if I bring up that
version, then we see the changes.
| | 00:59 | As I start to scroll, you can see the
red lines there on the scroll bar, and that
| | 01:03 | shows areas where the file has changed.
| | 01:07 | So, here's an area where I deleted some code,
and here's an area where I added some code.
| | 01:15 | It looks like I commented something out, or
I added a line, and then commented it out.
| | 01:20 | So, this is an easy way to see
changes to a particular file.
| | 01:23 | If I look at the TableViewController.h,
and again bring up the timeline, and go
| | 01:29 | back to that same version, I can see
that this file has some changes down there.
| | 01:36 | You can easily see the changes to the
rest of the project by going to the Log
| | 01:40 | view, and if I go to that version that
says iPad version now working, I can
| | 01:46 | click on the little information that
says that nine files changed for that
| | 01:49 | commit, and I can either pick one of
those from here by double-clicking on it,
| | 01:54 | and it will bring up this view for
seeing the changes in that file, and then I
| | 02:00 | can just select different files from
the bar at the left to see what the
| | 02:04 | changes are in those.
| | 02:08 | If I go to the Blame mode, and I'm going
to go back to this TableViewController
| | 02:12 | file here, I can see which code was
changed in which revisions, and again, I can
| | 02:18 | click on that little i, and I can get
the details from that entire commit.
| | 02:25 | So, Xcode provides these tools to make
it possible to easily browse the history
| | 02:29 | of a file or a project.
| | Collapse this transcript |
| Using snapshots| 00:00 | Xcode provides a handy feature for
making a backup of your entire project.
| | 00:05 | The Snapshot feature
is very easy to use.
| | 00:08 | Here, I've opened a working copy of
TestBedOSX here in Xcode, and I'm going to
| | 00:14 | start by creating a snapshot. Then I'm
going to make a bunch of changes, and I'll
| | 00:18 | restore from the snapshot, so
you can see how that works.
| | 00:21 | So, I come up here to the File menu,
| | 00:23 | and down here it says Create Snapshot, and
you see Ctrl+Command+S will do the same thing.
| | 00:31 | So, I'm just going to name this snapshot,
and I'll put in a little description, and
| | 00:40 | I'll click Create Snapshot.
| | 00:42 | Now, for larger projects it might take
longer, but this snapshot is now done, and
| | 00:47 | I can come in here
and I can change stuff.
| | 00:51 | So, like for instance, I can make a copy
of this, and I can come in here and make
| | 00:57 | these say, one, two, and three, and I
can just put in another message here if I
| | 01:03 | want to, and I'll save this, and I'll
build and run by pressing Command+R. And
| | 01:11 | there we see I've changed it.
| | 01:13 | It now has these extra messages in it.
| | 01:16 | Now, if I want to restore from my snapshots --
say I made a bunch of changes, and I
| | 01:20 | went down to some blind alley, and
I didn't really want to be there --
| | 01:24 | I can come up to the File menu,
and I can say Restore Snapshot.
| | 01:27 | It will give me a list of all of
the snapshots that I've created.
| | 01:31 | I'll select this one that says
Demo snapshot, and I'll click Restore.
| | 01:35 | It's going to show me what the changes
will be, which is really nice, and I
| | 01:40 | can just say Restore.
| | 01:42 | It's asking me if I want to have
Xcode take automatic snapshots before
| | 01:47 | restoration, so that I can get back to
this place if I decide I didn't really
| | 01:51 | want to have that snapshot, and I
can enable that, and then it'll make an
| | 01:56 | automatic snapshot there.
| | 01:58 | I say OK, and build and run again, so I
press Command+R, and you can see that we're
| | 02:05 | back to our original
Klaatu barada nikto.
| | 02:09 | So, I'll press Command+Q to quit our
TestBed, and I've created the snapshot
| | 02:14 | successfully, and restored
from the snapshot successfully.
| | 02:18 | It's worth noting that you can also
automatically create snapshots with any of
| | 02:22 | the actions available
in the behavior setting.
| | 02:25 | So, if I come up here to Preferences,
and click the Behaviors tab, we'll just
| | 02:33 | select one of these; I'll select the
Running Pause one, and I'll scroll down here
| | 02:38 | to the bottom, and you see
there's a Create Snapshot.
| | 02:41 | So, if I check that, then snapshots
will be automatically created any time
| | 02:46 | this behavior happens.
| | 02:47 | So, I'm just going to quit the Preferences
here real quick with Command+W, and if
| | 02:54 | we go back to the Restore Snapshot, you
see we got this automatic snapshot from
| | 03:00 | when we restored
from the snapshot before.
| | 03:03 | So, it says before restoring a snapshot,
that's the action, and Automatically
| | 03:07 | generated by Snapshot Restoration.
| | 03:10 | So, I could restore from that, and we would be
back to where we were before I did the restore.
| | 03:16 | So, I'm going to go ahead and do that,
and there are our changes, and I'll say
| | 03:20 | Restore, and our restoration
was successful.
| | 03:23 | And if I build and run with Command+R
again, you'll see there are those extra
| | 03:27 | messages we had put in before.
So, Command+Q to quit the TestBed,
| | 03:31 | and one more thing I want to show you
in this Restore Snapshot is you can have
| | 03:36 | it just show the user created ones. So,
this is the one we did intentionally,
| | 03:40 | and under All Snapshots, we now have two
automatically generated ones, because we
| | 03:45 | set it to automatically create a
snapshot every time we do a restore.
| | 03:49 | So, I'll press Cancel.
| | 03:51 | Snapshots are convenient, and they're
an easy way to back up an entire project
| | 03:56 | before making a major change.
| | Collapse this transcript |
|
|
4. Interface BuilderExploring Interface Builder| 00:00 | Xcode provides a rich environment for
creating and editing graphical interfaces
| | 00:05 | called Interface Builder.
| | 00:07 | Originally part of the next step
project builder development environment,
| | 00:10 | Interface Builder has shipped with
Xcode since the beginning of OS X.
| | 00:15 | In earlier versions of Xcode, Interface
Builder was a separate application.
| | 00:20 | Beginning with Xcode 4, Interface
Builder is fully integrated with Xcode as
| | 00:24 | a first class editor.
| | 00:26 | Interface Builder provides powerful, yet
simple tools for building and modifying
| | 00:31 | graphical interfaces for both OS X
desktop and iOS mobile applications.
| | 00:36 | Using Interface Builder, you can often
create a fully functional user interface.
| | 00:41 | The inspector provides several handy
interfaces for viewing and adjusting
| | 00:47 | various properties of the interface objects,
including connecting them with your code.
| | 00:53 | The file inspector provides information
about the nib, or storyboard file that
| | 00:57 | contains the interface you're working on.
| | 01:01 | The quick help pane provides brief
documentation for the selected object,
| | 01:05 | including links to the
full online documentation.
| | 01:09 | The identity inspector provides information
and options specific to the selected class.
| | 01:15 | The attributes inspector provides easy
access to various attributes specific
| | 01:19 | to the selected class.
| | 01:21 | The size inspector allows you to specify
the size and alignment properties for
| | 01:26 | the selected class. And the connections
inspector allows you to create and edit
| | 01:31 | connections between the selected
interface object and your code.
| | 01:35 | The object library gives you easy access
to the stock interface objects, so you
| | 01:40 | can incorporate them in your design.
| | 01:42 | Interface Builder also provides an
optional document outline view that provides
| | 01:47 | a hierarchical view of all the
objects in a given document.
| | 01:51 | This can be useful for a variety
of purposes, including the ability to
| | 01:55 | conveniently select a specific object
without having to expose it on the screen.
| | 02:00 | Interface Builder is a deceptively
simple, yet powerful tool for creating and
| | 02:05 | editing user interfaces.
| | 02:07 | In the rest of this chapter, I'll
show you what you need to know to use
| | 02:10 | it effectively.
| | Collapse this transcript |
| Creating a simple interface XIB| 00:00 | Interface Builder provides two
formats for storing interfaces.
| | 00:04 | The traditional NIB file is used to
create one screen or one interface at a time.
| | 00:09 | The flow between screens
is controlled by your code.
| | 00:12 | The file extension is XIB, because the
modern version of the file is stored as
| | 00:17 | xml, but it's typically pronounced NIB
as if it were spelled N-I-B, because it
| | 00:22 | used to be spelled that
way before the xml files.
| | 00:25 | The newer storyboard file may
contain multiple screens or interfaces.
| | 00:30 | Storyboards are currently
available only for iOS applications.
| | 00:34 | For this movie, we're going
to create a simple XIB file.
| | 00:37 | We'll cover storyboards later.
| | 00:39 | So, let's create a new
iPhone application.
| | 00:43 | I could just press this
Create a new Xcode project button,
| | 00:46 | but just in case you don't have this
welcome window, you can also do it from the
| | 00:50 | New > Project in the File menu.
| | 00:53 | I am going to create an iOS application
this time, and it will be a single view
| | 01:00 | application, and we'll go ahead and name
it Temp-xib, X-I-B, like that, and I am
| | 01:08 | going to select the Device; iPhone. Not
iPad, or Universal, but iPhone, and uncheck
| | 01:15 | Use Storyboards, check automatic reference
counting, and uncheck unit tests. Select Next.
| | 01:22 | And you want to check create local git
repository just in case, but we really
| | 01:26 | won't be using that in this
movie, and I'll select Create.
| | 01:30 | I'm just going to leave
this on my desktop.
| | 01:32 | You'll want to save it someplace
where you can find it later to delete it.
| | 01:39 | So, there's our simple
iPhone application.
| | 01:42 | I'm going to select the XIB file here.
| | 01:45 | I'm just going to close the navigator
pane, so we have a little bit more room,
| | 01:50 | and I'm going to start by
adding some controls to the app.
| | 01:53 | So, down here in the libraries pane,
I've selected the object library, and I'm
| | 01:58 | just going to drag a label right up
here, and you'll notice the little guides
| | 02:03 | come up, and I'll follow the center guide
right up to the top guide, like that, and
| | 02:08 | then I want to make it
as wide as the space.
| | 02:11 | So, I'm going to grab this little square;
not the top one, not the bottom one,
| | 02:14 | but the middle one on the left there, and
I can see that I've got it by the shape
| | 02:19 | of my cursor, and I'm just going to
drag that over to the left guide.
| | 02:23 | I'm going to do the same with the right
one, and drag that over to the right guide.
| | 02:30 | Then over here in the ittributes inspector,
I'm going to select Center Alignment.
| | 02:35 | We're going to change it from Label to
the word Status, and I always press the
| | 02:41 | Return key when I change one of these,
because that makes sure that this system
| | 02:46 | has actually got it.
| | 02:47 | And I watch it change over here.
| | 02:49 | I've had times where I typed something
in, and I've gone to do other things, and
| | 02:53 | hadn't pressed Enter,
and it didn't get changed.
| | 02:55 | I am going to move this
out of the way just a little bit.
| | 02:58 | If we scroll down here, you see there
are a lot of different properties that you
| | 03:02 | can change, and this is
true of all the objects.
| | 03:04 | There are lots and lots of different
properties that you can change for each object.
| | 03:09 | So, that's our label,
| | 03:11 | and now I'm going to grab a
slider, also from the Objects library.
| | 03:16 | And same thing; middle guide and right
up below Status, you'll see another guide
| | 03:21 | pops up to give a nice
spacing between these two objects.
| | 03:24 | So, we'll just drop it there.
| | 03:26 | I'm going to grab this left-hand, and
drag it out to the left-hand guide, and
| | 03:30 | grab this right-hand anchor, and drag
it out to the right-hand guide, and now
| | 03:34 | we've got a nice slider.
| | 03:36 | And just one more object; I'm going
to grab a round rectangle button, and
| | 03:41 | center guide, and then a new guide
right below the slider. There we have three
| | 03:47 | objects for our interface.
| | 03:49 | So, I'm just going to press Command+S to save
this, and Command+R to run it in the simulator.
| | 04:00 | There's our little app
running in the simulator.
| | 04:02 | Yours will look different, and that's
fine, as along as it has got something that
| | 04:06 | looks like this in the middle.
| | 04:08 | So, there's our button, there's our
slider, and there's our Status label.
| | 04:13 | Now, of course, they don't do anything
yet, and I want to show you how to hook
| | 04:17 | this up to code, because that's an
important part of using Interface Builder.
| | 04:22 | So, I'm going to go ahead --
| | 04:23 | rather than quit the Simulator, it
sometimes takes it a while to fire up, I'm
| | 04:27 | just going to switch back to
Xcode. I'm going to press Command+Period,
| | 04:31 | which stops it from running.
| | 04:33 | You'll notice up here on our status display,
finished running on the iPhone simulator.
| | 04:38 | And now we're going to
bring up the navigator again,
| | 04:40 | and just for now, I'm going to close the
utilities, because we're going to switch
| | 04:45 | to editing some code.
| | 04:46 | Come over here to our view controller,
and I'm going to start typing in a
| | 04:50 | little bit of code. It's not much.
| | 04:52 | You can type along with me.
| | 04:53 | Up here at the interface, I'm going to
put in some curly braces, and I'm going to
| | 04:58 | add our IBOutlet for the status display,
UILabel, and an asterisk, because all
| | 05:08 | objects are pointers in
Objective-C, and status;
| | 05:13 | we'll name it Status.
| | 05:15 | So, now, you'll notice when I complete
that, this little circle pops up over here.
| | 05:19 | That's because this is an IBOutlet.
| | 05:21 | IBOutlets and IBActions, they get these
little circles, and I'm going to show you
| | 05:26 | what to do with that as
soon as we're done typing code.
| | 05:29 | We're going to use that actually
for connecting it up to the interface.
| | 05:32 | It makes this really, really easy.
| | 05:34 | Just one other note; it used to be
traditional to put these IBOutlets in the
| | 05:37 | header file, but starting in Xcode 4.5,
Objective-C now has features that
| | 05:43 | make it easy to keep this interface private, and
there is really no reason for it to be public.
| | 05:48 | Other objects don't really need access to
that, at least not in this little bit of code.
| | 05:52 | Now we can come down here are the bottom,
where we are going to type a couple of IBActions.
| | 05:57 | So, - (IBAction) and this first one
will be for the button, and I'm going to
| | 06:08 | call it buttonPressed, and come out here to the
end of that line, and put in some curly braces.
| | 06:13 | Again, you notice that little
circle popped up in the middle when I
| | 06:16 | completed that IBAction.
| | 06:17 | We just need an integer variable for
keeping account, so static int count = 0.
| | 06:20 | And that little yellow exclamation
point; that just means that I have an
| | 06:28 | unreferenced variable.
| | 06:29 | I've got a variable,
| | 06:30 | and if I click on this, you'll
see it says Unused variable 'count'.
| | 06:34 | I've created a variable, I haven't it yet;
that's going to change here in just a moment.
| | 06:39 | And now we're going to access the text
property on the UILabel that's named status.
| | 06:46 | So, I'll say status.text =
and use
| | 06:52 | [NSString stringWithFormat:]
@"Button pressed: %d
| | 07:03 | for the count, and comma count. I
want to put a ++ in front of that count,
| | 07:10 | so that it increments each
time the button is pressed.
| | 07:13 | So now we've got our IBAction for the
buttonPressed, and we're going to create
| | 07:17 | one for the slider now. - (IBAction)
and this will be sliderAction.
| | 07:27 | Now, in this case, we're going to
use this generic center object.
| | 07:31 | This is Objective-C polymorphism at
work, and so I'm going to assign it to a
| | 07:39 | temporary UISlider Object.
| | 07:41 | So, UISlider * slider = sender;
| | 07:49 | and again, we get the little warning
that we have an unused variable.
| | 07:52 | We'll fix that right now. status.text
= NSString stringWithFormat:@"Slider: %f"
| | 07:58 | and that's for a floating point number,
and we are going to get that floating
| | 08:09 | point number from our slider.value.
Really very simple code here, and we're done.
| | 08:16 | So, you notice that we have a
sliderAction, and a buttonPressed action,
| | 08:20 | and then way up here,
we've got our IBOutlet.
| | 08:22 | We want to hook those up
now to our XIB file.
| | 08:25 | So, I'm going to select the XIB file, and
I'm going to go ahead and get rid of the
| | 08:30 | navigator view, and I'm going
to select the assistant editor.
| | 08:36 | Now, the assistant editor, in the
case of a XIB file, it defaults to the
| | 08:41 | corresponding.h file, and if I come up
here where this says Automatic, I can
| | 08:48 | switch to the other counterpart.
| | 08:51 | In this case, we have two possible
counterparts, and the other one is the .m file,
| | 08:55 | and that's where we find out stuff.
| | 08:57 | In fact, I can press Ctrl+Command+up arrow,
and it will cycle through those
| | 09:05 | corresponding files.
| | 09:07 | So, I want the .m file, so that's the .m
file right there, and up here, we see our
| | 09:15 | Status, and we have that little circle.
| | 09:17 | So, I'm just going to grab that
circle with my mouse, and drag it over to
| | 09:21 | status, and let go on.
| | 09:23 | It's as simple as that;
it's connected up.
| | 09:25 | We can do the same thing
now with the button.
| | 09:27 | Here's our buttonPressed action, drag
it to the button; our sliderAction, and
| | 09:32 | drag it to the slider, and we're done.
| | 09:35 | I can save this, press Command+S to
save it, and Command+R to run it, and there
| | 09:43 | we have it.
| | 09:46 | I pressed the button, it says
Button pressed: 1, Button pressed:
| | 09:49 | 2, Button pressed: 3,
| | 09:50 | and I move the slider, and it's got
the value for the slider as I move it.
| | 09:56 | So, it's as simple as that.
| | 09:58 | We have now successfully created an
interface, written some code, hooked up the
| | 10:04 | code to the interface;
| | 10:06 | you notice, when I hover over these, it
highlights the object that it's connected to.
| | 10:11 | You'll also notice, if you Ctrl+click on
this, or rather, if you Ctrl+click on the
| | 10:16 | object itself, you get a
little status display.
| | 10:22 | In this case, it shows that it's
connected to the referencing outlet called
| | 10:27 | Status in the file's owner.
| | 10:30 | If I Ctrl+click or right-click on the
slider, you notice that the value changed
| | 10:35 | event triggers the
sliderAction over here in our code.
| | 10:40 | There's our sliderAction code. And you
can actually hook up all these different
| | 10:43 | events to different things
if you'd like to.
| | 10:47 | On the button, the same thing; Ctrl+
click or right-click, and it's Touch Up
| | 10:52 | Inside. So, you have got Touch Down, or
drag down, or you have got all these
| | 11:00 | different possible actions
that can trigger different events.
| | 11:03 | In this case, we're connected to
buttonPressed, and there it is right there.
| | 11:08 | So, it's as simple as that.
| | 11:09 | I'm going to press Command+Period
| | 11:11 | to stop our app from
running on the simulator.
| | 11:14 | When I switch the simulator, you see it
stopped, and there's our little icon there.
| | 11:19 | I can actually just press on
that, and it still works just fine.
| | 11:24 | So, I'm going to quit the simulator
now with Command+Q, and I'm going to quit
| | 11:33 | Xcode, and go ahead and
delete my temporary project.
| | 11:38 | So, you see it's really easy to create
your user interface in Interface Builder,
| | 11:42 | and then hook up the
interface objects to your code.
| | 11:44 | We'll explore more of Interface
Builder's features in the next few movies.
| | Collapse this transcript |
| Using the Identity inspector| 00:00 | The identity inspector is used to
view and manage metadata for an object.
| | 00:04 | Here, I have a working copy
of BWRSS opened here in Xcode.
| | 00:10 | I'm going to select the storyboard file,
the one that says iPhone, and I'm going
| | 00:15 | to go ahead and I'm going to close
the navigator, and I'm going to open the
| | 00:21 | utilities pane,
and this is a storyboard.
| | 00:26 | A storyboard is basically a collection
of XIB files all in one package, and it
| | 00:31 | has transitions, it has segues, and so
you see we have a navigation controller,
| | 00:35 | we have a feeds table view controller,
we have another table view controller
| | 00:40 | for items, we have this separate view
controller for adding feeds to the RRS
| | 00:47 | feed, and we have another Web view
controller for displacing the information
| | 00:52 | from a particular feed.
| | 00:54 | So, I'm just going to go ahead and
select this table view controller here, and
| | 01:00 | over here in the utility pane, I'm
going to select the identity inspector.
| | 01:04 | You might have another
one of these selected.
| | 01:06 | Select the identity inspector,
and we see the information on this
| | 01:11 | particular table view.
| | 01:13 | If I actually select the Items Table
View Controller down here in this bar at
| | 01:17 | the bottom, you see that up here in
the identity inspector it says Class is
| | 01:23 | BWRSSItemsTableViewController,
and there is a selector here.
| | 01:28 | You can select different
classes for this object.
| | 01:32 | This is how you select a
custom class for your object.
| | 01:36 | In this case, we have a table view
controller, and I wanted to subclass that, so
| | 01:41 | that I can customize it, and make it work the
way that I want it to work for this application.
| | 01:46 | This is the most common use
for the identity inspector.
| | 01:50 | You're creating an application, you
want to subclass a particular object, you
| | 01:53 | drop the object into your XIB, or your
storyboard, and you come over here to the
| | 01:58 | identity inspector, and you select your
custom class for this object, and that
| | 02:05 | allows you to write your
code in that subclass file.
| | 02:10 | There's other information available here.
| | 02:12 | There is a Storyboard ID
and a Restoration ID.
| | 02:16 | The Storyboard ID is used when you want to
create a new view controller based on this one;
| | 02:21 | the Restoration ID is used when you want
to allow the operating system to handle
| | 02:25 | state preservation
and restoration for you.
| | 02:27 | There's also accessibility information
available; not in this class, but if we
| | 02:32 | scroll down here, it's really
easy to navigate around here.
| | 02:36 | I'm just scrolling around
on my touch pad here.
| | 02:39 | So, I'm going to select this button, and
you'll notice that I could subclass it
| | 02:45 | if I waned to. Right now, it's
just UIbutton, and there's nothing else
| | 02:49 | available in there.
| | 02:51 | If I scroll down here, though, you'll
notice that there is an Accessibility
| | 02:54 | panel, and I can select information and
change information for accessibility, for
| | 02:59 | voiceover traits, to describe aspects
of the element, and other object-dependent
| | 03:05 | options for accessibility.
| | 03:07 | So, the identity inspector is used to
inspect and manage metadata for an object,
| | 03:14 | and most commonly, it's used just to
designate a custom class for an object.
| | Collapse this transcript |
| Using the Attributes inspector| 00:00 | The attributes inspector is where you
can view and change specific attributes of
| | 00:05 | a user interface element.
| | 00:07 | Here, I have a working
copy of BWRSS open in Xcode.
| | 00:12 | I'm going to select this iPhone storyboard
file, and I'm going to close the
| | 00:20 | navigator pane, and I'm going
to open the utilities pane.
| | 00:25 | I'm also going to close
the outline view over here.
| | 00:29 | If you have it open, you can
just press that to close it.
| | 00:32 | This button over here will open it, and now
we can see the storyboard a little better.
| | 00:37 | I'm just going to click on this table
cell, and then over here in the navigator
| | 00:43 | pane, I'm going to select
the attributes inspector.
| | 00:47 | The attributes inspector is where
much of the magic happens in Interface
| | 00:52 | Builder, where much of
the magic happens.
| | 00:55 | The items in the attributes inspector
depend upon the object being inspected.
| | 01:00 | All these properties can also be
inspected and changed in your code.
| | 01:05 | So, here we have this table cell,
and we can select its style.
| | 01:10 | In this case,
I have the Subtitle style.
| | 01:13 | I can select the Left Detail style, I
can select a Right Detail style, with
| | 01:20 | the title and the detail to the right,
I can select the Basic style, which
| | 01:25 | just has the title, or I can select
a Custom style, and that will get
| | 01:31 | customized in my code.
| | 01:34 | The Subtitle style is the style that
we need for this particular application.
| | 01:38 | So, for the table cell, an identifier is
very important, because we use that to
| | 01:42 | reference the cell in the code, and you
can see there's just all these different
| | 01:47 | attributes, you know, how the selection
is, it's got an accessory, like a
| | 01:52 | disclosure arrow or disclosure
detail; a lot of different options.
| | 02:02 | Let's take a look at
a different element.
| | 02:03 | I'm going to scroll down over here, and
I'm just scrolling using my touch pad here;
| | 02:09 | scroll down over here to this button,
and you see we get an entirely different
| | 02:14 | selection of things.
| | 02:15 | It's a rounded rectangle button, but we
can make it any of these different types
| | 02:22 | of buttons. Bring back the rounded
rectangle, and you'll notice that it has
| | 02:28 | changed size, and it no
longer says cancel in it.
| | 02:35 | You can put that right in here. So, you
can see you can customize all kinds of
| | 02:39 | different things here.
| | 02:40 | You can give it a default image, you
can give it a default background image, and
| | 02:44 | if you drop down one of these, it will
display the different images that are
| | 02:48 | available in your package.
| | 02:49 | These are images that I have in
this particular application for
| | 02:52 | different purposes.
| | 02:53 | You can select color, and
alignment; all kinds of things.
| | 02:59 | Just one more; let's just take
a look at this text field here.
| | 03:04 | Here we can have the default text,
or we can have placeholder text.
| | 03:08 | If I take out this default Text, you
notice that it's now blank, and if I put in
| | 03:14 | a placeholder text, you'll notice that
we now have this little grayed text in
| | 03:22 | there, and when you run the application,
the minute you put your cursor inside
| | 03:26 | of that that field, that placeholder text
will disappear, and allow you to start typing.
| | 03:30 | Again there are many,
many different options.
| | 03:34 | There is a capitalization option to see
how things will be capitalized inside of
| | 03:40 | this field. Whether or not
you want your correction on;
| | 03:43 | for the case of a URL, I don't want it
suggesting that some funny-spelled word
| | 03:48 | is really a different word, so
I don't have the Correction on.
| | 03:51 | What type of keyboard will come up when
you type into that text field; whether
| | 03:57 | it appears as an alert; what the
return key button will say, and on and on.
| | 04:06 | You have your alignment, how it's
viewed; all these different options.
| | 04:12 | So, the attributes inspector is where
you control a lot of the attributes of
| | 04:16 | your interface elements.
| | 04:17 | Again, keep in mind that all of
these properties may also be controlled
| | 04:21 | from your code.
| | Collapse this transcript |
| Using the Size inspector| 00:00 | The size inspector is used to control the
dimensions and layout of an object on the screen.
| | 00:06 | Here, I've opened a working
copy of BWRSS here in Xcode.
| | 00:11 | I am going to open this
iPhone storyboard.
| | 00:16 | I'm going to go ahead and close the
navigator pane, and open the utilities
| | 00:22 | pane, and scrolling around here with my track
pad, I'm going to select this button right here.
| | 00:33 | I have the size inspector open,
with the size inspector tab there in
| | 00:38 | the utilities pane.
| | 00:39 | I'm just going to drag this to
give us a little bit more room.
| | 00:44 | Now, the options available in the size
inspector depend on the state of the auto
| | 00:49 | layout checkbox in the file inspector.
| | 00:52 | If we look at this, we see that we have
the springs and struts type of autosizing.
| | 01:00 | If I come over here to the file inspector,
and I need to scroll down a little
| | 01:05 | bit, and I select Use Autolayout --
| | 01:08 | now, this checkbox applies to the entire
document, not just to this particular object,
| | 01:14 | so that's important to remember.
| | 01:16 | When you check this Use Autolayout, it
doesn't apply just to the object that's
| | 01:22 | selected; it applies to the entire
storyboard document. And now I come back over
| | 01:27 | here to the size inspector.
| | 01:28 | You see we have
entirely different options.
| | 01:31 | We no longer have
the springs and struts.
| | 01:33 | We now have constraints, and resistance,
and hugging, and all of that kind of stuff.
| | 01:40 | So, this is the autolayout.
| | 01:42 | Autolayout is a subject in itself, and
suffice it to say that it still works
| | 01:48 | pretty much the same way.
| | 01:49 | You can drag an item around, you can
resize it, and it will come up with options
| | 01:54 | that makes sense, for the most
part, and for most applications.
| | 01:58 | Now, if I come back over here, and I
uncheck autolayout in the file inspector,
| | 02:05 | and then come back to the size inspector,
we have the traditional springs and
| | 02:10 | struts type of sizing.
| | 02:12 | Autolayout is a new feature
beginning with Xcode 4.2, actually.
| | 02:18 | Before autolayout, the springs and struts
alignment was the only choice available.
| | 02:22 | This project was actually created
before autolayout, so it was created with
| | 02:26 | springs and struts, and as you can
see, it works the same either way;
| | 02:30 | it's just that the
adjustments are different.
| | 02:34 | The Autosizing box right here shows springs
on the inside, and struts on the outside.
| | 02:38 | For the most part, all you have to do is
drag your objects around the screen, and
| | 02:43 | that will work fine.
| | 02:44 | The springs and struts just indicate
which sides of the object are anchored, and
| | 02:47 | how those anchors work when
things move, or the window resizes.
| | 02:51 | In many cases,
you'll never touch any of this.
| | 02:55 | Independent of the autolayout checkbox
in the file inspector, you can adjust
| | 03:01 | the position and dimensions of an
object using numeric controls at the top of
| | 03:05 | the size inspector.
| | 03:07 | As you change the numbers, the object
moves relative to the origin selected in
| | 03:11 | the box on the left.
| | 03:12 | So, I've got my origin on the left, and
if I increase the width, you see that the
| | 03:17 | Cancel button gets wider to the right.
| | 03:24 | If I move the origin, say, to the bottom
center, now when I increase the width,
| | 03:30 | you'll notice it increases,
anchored on the bottom and the center.
| | 03:35 | So, either with or without autolayout,
the size inspector is actually
| | 03:39 | pretty user-friendly.
| | 03:40 | Play around with it for awhile, and
you'll find that it makes a lot of sense.
| | Collapse this transcript |
| Making interface connections to the code| 00:00 | In order for interface objects to
work with your code, you need to make
| | 00:03 | connections between those
objects and your code.
| | 00:06 | We'll start by creating a simple iPhone
project to make some connections with,
| | 00:10 | and I'm going to create
a new Xcode project.
| | 00:13 | I could press this button, or I can use
the File menu; New > Project, and
| | 00:18 | this is going to be an iOS application, and
a single view application. Pressing Next,
| | 00:23 | I'm going to name this Temp-connection,
| | 00:27 | and you notice I've
selected the iPhone device.
| | 00:30 | You don't want Universal,
or iPad, just iPhone.
| | 00:34 | And I've selected Use Storyboards.
| | 00:36 | I've done this with XIBs
in some of the other movies.
| | 00:39 | It really doesn't matter here;
| | 00:40 | it's just going to be a single scene.
| | 00:42 | If i've selected Use Storyboards,
so we can see how that works.
| | 00:46 | And Use Automatic Reference Counting
is checked, and unit test is unchecked.
| | 00:53 | I've checked
Create the local git repository.
| | 00:56 | Probably not going to use it in this
project, but it doesn't cost much in terms
| | 01:00 | of space or processing time, so I
just generally leave it checked.
| | 01:03 | I'm going to save this
project on the Desktop.
| | 01:06 | You can save it wherever you like, as
long as you remember where it is, so that
| | 01:11 | you can delete it later.
| | 01:12 | Press Create, and we have our project.
| | 01:15 | So, I'm going to select the
Storyboard from the left here.
| | 01:18 | You notice I have the document outline
hidden, and I'm just going to drop a few
| | 01:24 | objects here into our storyboard.
| | 01:27 | Down here in the library, I've got the
object library selected, and I'm going to
| | 01:32 | select Controls from the dropdown menu.
| | 01:34 | We'll start with the Label.
| | 01:36 | You notice the centering guide, and the
top guide, and then I'm just going to
| | 01:42 | drag the left-hand side -- not the upper
or the lower, just the middle left-hand
| | 01:46 | side -- drag it all the over the left
to the guide, and the same with the
| | 01:50 | right-hand side
just over to the guide.
| | 01:53 | Then here in the attributes inspector,
I'm going to select center, and I'm going
| | 01:59 | to change the text in the
label to see status.
| | 02:02 | I'll come down here to library,
and I'm going to grab a slider.
| | 02:07 | I'm going to do the same thing.
| | 02:08 | I can come up to the middle right
below the status label, or I could actually
| | 02:13 | just drag this over to the left-hand
guide, and then just grab the right guide,
| | 02:18 | and pull it to the right side,
and I still get the same result.
| | 02:22 | It's maybe one less step, and
it's still centered just fine.
| | 02:26 | And sliding back up in the controls,
I'm going to grab a round rectangle button,
| | 02:32 | and drop it in right under
the slider, and also centered.
| | 02:38 | So now, we have our control objects, and
let's create some code to connect to that.
| | 02:43 | So, I'm pressing Command+S to save, and
coming down here to the view controller,
| | 02:48 | I'm just going to hide the utilities
pane, so we have a little bit more room
| | 02:53 | on the screen here.
| | 02:54 | Now, up here on line 11, where it says,
interface, I'm going to put in a curly
| | 02:58 | brace, and when I press Return, Xcode
will put in the second curly brace for me,
| | 03:02 | and so I can declare an IBOutlet
for our status label; UILabel, and the
| | 03:10 | asterisk makes it a pointer, because
all objects in Objective-C are a
| | 03:15 | pointers, and name it status.
| | 03:19 | You notice when I put in that semicolon,
I get this little round circle over here.
| | 03:23 | That's for making the connections.
| | 03:24 | We're going to use that
in just a minute.
| | 03:27 | Scrolling down to the bottom of the
file, I can type in our IBActions.
| | 03:31 | I'm going to start with the button
action, so I call this buttonPressed, and put
| | 03:39 | in the curly braces. I need to
clear a static int for keeping count.
| | 03:46 | When this action occurs, we're going
to set the text property in our status
| | 03:51 | label, so our status label
is called status;
| | 03:53 | You see it's declared up here, and I
just say, dot text for the text property of
| | 04:00 | the status, object, equals,
create an NSString --
| | 04:04 | This will be a temporary string --
stringWithFormat, Button pressed %d, allows
| | 04:16 | us to use our integer, and display its
value, and I'll just increment it with,
| | 04:24 | ++count, and that will return a new
incremented value each time the button is pressed.
| | 04:31 | So, that's our button action.
| | 04:32 | Let's create an IBAction
for the slider.
| | 04:35 | I'll call this one sliderAction.
| | 04:41 | These IBActions, they pass a sender; the
sender is a polymorphic object with the
| | 04:47 | ID type, so I'm going to assign it to
a temporary variable of type UISlider.
| | 04:54 | Now I have access to the properties
of the slider, and I can set the status
| | 04:59 | text with its value.
| | 05:01 | stringWithFormat, Slider: %f.
| | 05:08 | The value of the Slider is a
floating point number, and I just take
| | 05:14 | Slider.value, and now
we have our slider action.
| | 05:19 | Again, we have this
little connection circle.
| | 05:21 | I'm going to press Command+S to save, and
we're going to go back over the storyboard.
| | 05:26 | I'm going to hide the navigator, and I'm
going to show the utilities, and I'm also
| | 05:36 | going to bring up the outline view
with this little button over here.
| | 05:40 | Grab this little scrollbar
at the bottom.
| | 05:44 | Now that we have some controls, and
some code to connect them to, there are
| | 05:48 | several ways to make these connections.
| | 05:50 | We can use the connections inspector,
we can use the document outline, and we
| | 05:54 | can make connections directly
from the interface to the codes.
| | 05:57 | We're going to look at each of these.
| | 05:59 | You can even pick and choose, and
combine between these various methods, because
| | 06:02 | the effect is exactly the same.
| | 06:04 | Let's start with the document outline.
| | 06:06 | If I select the view controller, you'll
notice over here in the organizer, I
| | 06:11 | can select the connections inspector,
and these are the connections available
| | 06:15 | for the view controller.
| | 06:17 | In fact, if I right-click or Ctrl+Click
on the view controller in the document
| | 06:21 | outline, I get a little pop-up that has
all of those same connectors as we have
| | 06:26 | in the connections inspector.
| | 06:28 | So, we have this actions; button
pressed, and slider action, and you notice it
| | 06:33 | says received actions.
| | 06:35 | Received actions are things that
go from the control to the code, and
| | 06:40 | outlets, like this status outlet
up here, are things that go from the
| | 06:45 | outlet to the object.
| | 06:48 | So, if I drag a connection from the
status outlet, I can drag it to the status
| | 06:54 | object, and likewise, if I want to drag
something from the button, I could hold
| | 07:00 | down the Ctrl Key on my keyboard and
drag, or I can right-click+drag and drag
| | 07:06 | that to the view controller, and
you see I get a little pop-up
| | 07:10 | that allows me to select
the button pressed.
| | 07:13 | So, the same thing happens if I select
the view controller, and right-click on it,
| | 07:17 | and I grab the button pressed here,
| | 07:19 | I can drag it to the button, like that,
and it will give me an option of which
| | 07:24 | event in the button I want to select.
| | 07:27 | Touch Up Outside is actually the default.
| | 07:29 | If I just select the button here, and
right-click on it, and drag it over to
| | 07:36 | view controller, and I'll go
ahead and I'll select button pressed.
| | 07:41 | Now, when I select the button
here, you see it's selected,
| | 07:45 | you notice that the connection is to the
Touch Up Inside event, so that's the default.
| | 07:52 | Likewise, if I take the slider, and I
Ctrl+Drag that slider to view controller,
| | 07:59 | and let go, and I'll select slider action,
you'll see that the value changed is that default.
| | 08:05 | So, you can connect any of these
various events, and you can have them go to
| | 08:09 | different action methods, and those
are also available if you Ctrl+Click or
| | 08:14 | right-click on the object itself.
| | 08:16 | So, that's all hooked up, and I'm
actually going to do three undos here.
| | 08:23 | Let me just select my view controller,
and we can see these three connections, and
| | 08:27 | I'm going to press Command+Z on my
keyboard to undo them one at time, so you see
| | 08:32 | all three of those are now undone,
| | 08:34 | so that we can instead show you how
to do this with the assistant editor,
| | 08:39 | connecting directly to the code.
| | 08:41 | So, I'm going to go ahead and I'm going
to hide the utilities pane, and I'm going
| | 08:47 | to hide the outline.
| | 08:48 | I'm going to bring up the assistant
editor, and so there's our code on the right.
| | 08:54 | You notice that it defaulted to
the header file, and this is an
| | 08:58 | automatic selection.
| | 08:59 | If I select here where it says Automatic,
you'll see that either of these .m or .h
| | 09:04 | files are available.
| | 09:05 | I'm going to select the .m file for the code,
because that's where we put our interface.
| | 09:10 | It's common also to put the interface
in the header file, and if you chose to do
| | 09:14 | that, you just need to realize that
that interface is now public, and is
| | 09:17 | accessible to other parts of the code.
| | 09:19 | In this case, there is no need to do
that, and I tend to the default to private
| | 09:23 | interfaces, where there's no
purpose in exposing those interfaces.
| | 09:27 | Making these interfaces
completely private
| | 09:29 | is actually a relatively new
feature in Objective-C, and it began to be
| | 09:34 | available around Xcode 4.2.
| | 09:37 | For practical purposes,
I consider it Xcode 4.5.
| | 09:40 | In any event, here we are.
| | 09:42 | We have these little circles,
which is how we make these connections.
| | 09:45 | You notice when I hover my mouse over it,
it turns into a plus sign, so I can connect
| | 09:50 | this status IBOutlet to the object
itself, and you notice that that circle is now
| | 09:57 | filled in, and that piece of code right
there, that status object in the code, is
| | 10:03 | now actually controlling this object.
| | 10:07 | Likewise, these IBActions, I can have
them respond to events from these objects
| | 10:12 | by selecting the action, and
dragging it over to the object.
| | 10:18 | Of course, the other way doesn't work,
because these IBActions are receivers;
| | 10:22 | they're not senders.
| | 10:24 | So, if I try to drag from the button with
holding down the Ctrl key, you see that
| | 10:28 | it doesn't highlight, and it
doesn't allow me to make that connection.
| | 10:31 | The button goes to the button,
and the slider goes to the slider.
| | 10:36 | I'm pressing Command+S to save, and
we've seen all the different ways we can
| | 10:41 | make these connections.
| | 10:42 | I'm going to press Command+R to run, and
you'll see that our application now works.
| | 10:48 | So, this will bring up
the iPhone simulator.
| | 10:51 | You notice I have iPhone 6.1 simulator
selected in my scheme here at the top.
| | 10:56 | There is our application
running in the simulator.
| | 11:00 | If I press the button, you see the
status changes the button pressed: 1,
| | 11:03 | so that means that the button is
receiving the action, it's sending that
| | 11:08 | action to the IBAction method, the
IBAction is setting the text and status to
| | 11:13 | button pressed, and a number.
| | 11:16 | Every time I press it, that number
increments, and that's because I have that
| | 11:20 | ++count right there.
| | 11:21 | Likewise, with the slider, I grab the
slider, and change its value, and you see
| | 11:26 | that the slider value changes.
| | 11:28 | That's because the action of moving the
slider is sending messages to the slider
| | 11:33 | action, IBAction method, and that is
reading the value of the slider, and setting
| | 11:40 | the text on the status object.
| | 11:43 | That status object is also connected
to the status object in our Interface
| | 11:48 | builder, and so those
numbers up there change.
| | 11:52 | So, you can see there are a number of
ways to make the necessary connections
| | 11:57 | between the interface and your code.
| | 11:59 | Each of these methods have their
advantages, so it's good to know your options.
| | Collapse this transcript |
|
|
5. StoryboardsUnderstanding storyboards| 00:00 | If you're developing an iPhone or
iPad app, and you're using the latest
| | 00:03 | version of Xcode, you have the option
to use storyboards instead of XIB files
| | 00:08 | for your interface.
| | 00:09 | A storyboard is a collection of
view controllers that are connected to
| | 00:13 | eachother with segues.
| | 00:15 | A storyboard is created by creating a
project with the Use Storyboards option checked.
| | 00:20 | This creates the storyboard, and it
creates your project with code to support
| | 00:24 | the storyboard system.
| | 00:26 | Each view controller within the
storyboard is referred to as a scene.
| | 00:30 | Each scene has a dock at the bottom,
which is used for selecting the controller.
| | 00:35 | This dock is not part of the user interface,
and will not be visible on the device screen.
| | 00:39 | On an iPhone, each scene generally
corresponds with a full screen of content.
| | 00:46 | On an iPad, the screen may have more
than one scene at once, as in the case of
| | 00:50 | pop-over controllers,
and split view controllers.
| | 00:54 | Scenes are connected with segue.
| | 00:56 | A segue is an object that describes
the transition form one scene to another.
| | 01:00 | The segue manages the transition
between scenes, even providing tools for
| | 01:05 | passing data to the next view controller,
allowing you to set up the next view
| | 01:09 | controller, and maintain
context and state.
| | 01:12 | A segue identifier is used to indicate a
particular segue, and its next view controller.
| | 01:18 | This is important for passing objects
to the next view controller in order
| | 01:22 | to maintain content.
| | 01:24 | Within the context of a scene, a storyboard
works exactly the same as a nib file.
| | 01:29 | Each of the utility inspectors
works the same.
| | 01:32 | Connections are made in the same way,
and the document outline, aside from having
| | 01:36 | a lot more controllers,
works exactly as expected.
| | 01:40 | For an iOS app, storyboards are a great
way to lay out an application interface.
| | 01:44 | In many instances where you would
have previously found your self coding
| | 01:47 | interface transitions, and sometimes
entire view controllers by hand, you may
| | 01:51 | find that you can now lay out most, if
not all of your application scenes in
| | 01:57 | a storyboard.
| | Collapse this transcript |
| Creating an iPhone interface using storyboards| 00:00 | In this movie, I'll walk you through
the process of creating a storyboard
| | 00:03 | app for the iPhone. Let's get started.
| | 00:05 | I'm going to create a new project.
| | 00:07 | Of course, if you don't have the welcome
to Xcode box coming up, you can come
| | 00:11 | here to File > New, and Project.
| | 00:12 | This time, I'm just going to press this
create new project button here, and I'm
| | 00:17 | going to select an iOS application.
| | 00:19 | I'm selecting a single view application,
even though we're going to have multiple views.
| | 00:23 | I want to show you the process of
creating that. I'm going to press Next,
| | 00:27 | and I'm going to name the project Temp-
storyboard, and my class prefix says BW.
| | 00:35 | You can use whatever you like there.
| | 00:37 | And under Device, I've selected
iPhone; not iPad, or universal.
| | 00:41 | I've got Use Storyboards and Use
Automatic Reference Counting selected, and I'm
| | 00:46 | going to press the Next button.
| | 00:47 | I'm storing this on my Desktop.
| | 00:49 | You can store it wherever you like,
just remember where it is, so that you
| | 00:52 | can delete it later.
| | 00:53 | And I'm pressing the Create
button, and there's our project.
| | 00:57 | So, I'm going to come down here and select
the storyboard, and there's our storyboard.
| | 01:02 | I'm going to press this equals sign
here, and this equals sign will shrink
| | 01:07 | it down to a size
where we can actually use it.
| | 01:10 | But I need to zoom back in order to
manipulate objects, and so you'll see
| | 01:14 | me doing that a lot.
| | 01:15 | So, you'll notice we have the
view controller, but we have no
| | 01:18 | navigation controller.
| | 01:19 | We have no navigation controller, because
we selected a single view application.
| | 01:24 | In order to have multiple scenes, multiple
view controllers, you need to have a
| | 01:29 | navigation controller in order
to manage the multiple screens.
| | 01:33 | So, I'm going to select our view controller,
and I could actually select it down
| | 01:37 | here with the little dock.
| | 01:39 | And you'll notice, if I come up here to
the identity inspector, this is already
| | 01:44 | set up for a class BW view controller,
and BW controller is here in the code;
| | 01:49 | that's because we selected a prefix,
| | 01:51 | and Xcode does it's best when creating
a new project to set up things so that
| | 01:56 | they're useable right away, and
so it's done this for us already.
| | 01:59 | I'm going to come up here to the Editor
menu, which is not where I would expect
| | 02:03 | to find this, and I'm going to select
Embed In > Navigation Controller, and that
| | 02:07 | will create a navigation controller,
and embed our view controller within it.
| | 02:12 | So, I'm going to slide this navigation
controller over to the side here, so that
| | 02:16 | we can see both of these items.
| | 02:18 | And there we have our navigation
controller, and our view controller, and they
| | 02:23 | are connected here with this connection.
| | 02:25 | And if I select the navigation controller,
you can see, there is its class,
| | 02:30 | UINavigationController,
| | 02:31 | and come over here to the attributes
inspector, you'll notice that all of these
| | 02:35 | are selected as inferred, and
I'm concerned with the top bar.
| | 02:39 | You notice we have a top bar, and that
top bar is already set to a navigation bar.
| | 02:44 | But I'm going to set navigation bar
explicitly, because that's what I want.
| | 02:49 | I don't want the default.
| | 02:50 | I want a navigation bar.
| | 02:52 | Even though the navigation bar is the
default, if for some reason in some future
| | 02:56 | version of Xcode that gets changed to a
different default, I know for this app,
| | 03:00 | I want a navigation bar.
| | 03:02 | So, I tend to select things like that
explicitly, even though they're already
| | 03:06 | implicitly selected.
| | 03:07 | Now, I'm going to come over here to our
view controller, and because it's zoomed
| | 03:11 | out, it's not actually letting
you select the navigation bar.
| | 03:16 | So, I'm going to just double-click on
it, and it will zoom in a little bit, and
| | 03:20 | it's actually zooming in
to its natural size.
| | 03:22 | And so now I've selected the
navigation bar, and you see the class is
| | 03:29 | UINavigationItem, and that
navigation bar is selected.
| | 03:33 | I'm going to go ahead and I'm going to
collapse the navigator, and I'm going to
| | 03:38 | bring up the document outline, so we
can see the things that are selected here.
| | 03:44 | And so, scroll back over.
| | 03:47 | I've selected navigation item,
which is this navigation bar.
| | 03:51 | I'm going to come over here to the attributes
tab, and I'm just going to give it a title.
| | 03:57 | I'm going to call this Data.
| | 03:59 | This is our data view.
I'll select that scroll bar.
| | 04:01 | I'm going to scroll down here, so we can
see the screen, and I'm going to drop a
| | 04:07 | button on that navigation bar.
| | 04:10 | So, I'm going to come over here to Windows &
Bars, and I'm going to grab a navigation item.
| | 04:17 | Actually, what I want is a bar button item,
and I'm going to drop that up here on the right.
| | 04:24 | And you see, it defaults
to saying item; title it Go.
| | 04:27 | This will be our go button to go to the
next view. And you see Style, Bordered,
| | 04:33 | and you can leave it as Plain, or you
can leave it as Bordered, and I'm just
| | 04:39 | going to just color it a little bit.
Select the crayons, and one of these here,
| | 04:47 | I think, says Clover.
| | 04:48 | I like the one that says Clover.
| | 04:50 | There it is, and close that,
| | 04:53 | and so there is our clover colored
button that says Go. I like that.
| | 04:57 | So, now that we have our go button, we're
going to create another view controller.
| | 05:02 | So, I come back up here to controllers
and objects, and I'm going to grab a view
| | 05:08 | controller, and I'm just going
to drop it in right next here.
| | 05:13 | There's our view controller, and
kind of line it up there a little bit.
| | 05:18 | It's probably not that
necessary, but it's what I do.
| | 05:23 | Now, we have another view controller.
| | 05:25 | I can connect them with a segue, and the
way I do that is I grab this button, and
| | 05:30 | I'm holding down the Ctrl key as I drag
it with my mouse, and I drag it over to
| | 05:36 | the new view controller, and let go.
| | 05:39 | It says, Action Segue: push, modal, or
custom, and I'm going to select push.
| | 05:43 | Now you'll notice our view controller
got its navigation bar, because it's now
| | 05:50 | also embedded in this
navigation controller.
| | 05:52 | So, once we connect this segue, we have a segue
item here, and that's actually a segue object,
| | 05:59 | and our new view controller is now
part of this whole navigation controller's
| | 06:04 | domain, and so it gets the same top bar.
| | 06:08 | You notice I left top bar as
inferred for both of these controllers.
| | 06:12 | I only actually set it on
the navigation sontroller.
| | 06:17 | So, these actually inherit;
| | 06:18 | by selecting infer, that inherits
the properties of the top bar from the
| | 06:23 | navigation controller.
| | 06:24 | So, in order to actually select the
navigation bar, I need to zoom in and select
| | 06:29 | it here, and I can give this one a title.
| | 06:32 | I'll call this one Status.
| | 06:35 | And I'm also going to make
sure it has a back button.
| | 06:37 | Now, this is interesting;
| | 06:38 | when I type something in the back
button, it gives it a back button, but it
| | 06:42 | doesn't actually display it on the screen;
it will only display it when we run the app.
| | 06:47 | So, I'm going to put in the word back
here, and that gives it a back button, but
| | 06:51 | we don't actually see that in the
status bar here, in the top bar.
| | 06:56 | But if I save this, and run it, at this
point, I can do that, and this will run it
| | 07:02 | in the iPhone 6.1 simulator there.
| | 07:06 | So, there is our application in the
simulator, and when I press this green Go
| | 07:09 | button, you see we get that Status
screen, and it's got the back button there
| | 07:15 | that says Data in it.
| | 07:17 | It doesn't actually say back,
like I said, back button there, back;
| | 07:22 | it's got an indicator of what screen you're
going back to, which is the Data screen.
| | 07:25 | Here, we've got a working storyboard,
and we haven't written one line of code,
| | 07:31 | which I kind of think is pretty cool.
| | 07:33 | So, I'm going to select Xcode again, and I'm just
going to just press Command+Period to finish running.
| | 07:40 | You notice it says Finished running now.
| | 07:41 | If I go back to the simulator, you
see that the application has stopped.
| | 07:50 | So, now we can go in and
drop in some UI elements.
| | 07:53 | So, I'm going to go back to our first
screen here, and we want to be able to
| | 07:57 | enter some data in order to pass
that data on to the next screen.
| | 08:01 | And so, I'm going to
come down here to Controls,
| | 08:04 | and I'm going to drop in a Label.
| | 08:07 | Go up here to the top.
| | 08:08 | I'm just going to drag this over to the
left top, and left guides, and then drag
| | 08:12 | the width of it all the
way over to the right guide.
| | 08:16 | And then I'm going to come down, and I'm
going to grab a slider, and I'm gong to
| | 08:21 | do the same thing right under the label,
and off to the left, it looks like I
| | 08:25 | missed it there a little bit; right
there, and grab that right guide, and drag it
| | 08:32 | over to the right guide.
| | 08:33 | Now, we have a slider, and we're
also going to drop in a text field.
| | 08:40 | Here's a text field, and put that in
right under the slider, and also off to the
| | 08:45 | left, and grab that right adjustment
widget, and drag it over to the right guide.
| | 08:52 | Now, the label is going to say Slider
Value instead of Label; Slider Value.
| | 09:02 | And over here,
I'm in the attributes inspector.
| | 09:06 | So, I've got Slider Value there.
| | 09:08 | I don't need to change
anything for the slider.
| | 09:12 | In the text field, I'm
actually giving it a placeholder.
| | 09:15 | I'm going to say Type something here;
| | 09:19 | and there we have a placeholder that
gives an instruction to the user to type
| | 09:23 | something in this box.
| | 09:24 | Now, we're going to come
over to our status controller.
| | 09:27 | I'll select that, and here we're just
going to drop in a couple of labels.
| | 09:32 | So, this will report the
data that we passed over.
| | 09:35 | So, I'm coming over to the left, and I'm
going extend this to the right up to the
| | 09:41 | guide, and this one will
be called Slider Value.
| | 09:48 | And we're going to create another one.
| | 09:50 | I'm just going to Option+drag this,
and you'll see it will duplicate it.
| | 09:54 | That's a pretty normal Mac paradigm there,
and this one will be called Text Field Text.
| | 10:03 | We're not actually going
to see either of these.
| | 10:06 | Once the code is all plugged in, by the time
we move to the screen, they'll be all filled in.
| | 10:10 | That's just kind of
placeholder, so we can look at it now.
| | 10:13 | That's our complete screens there.
| | 10:15 | That's our storyboard.
| | 10:16 | I'm going to press Command+S to save,
and Command+R to run this in the simulator,
| | 10:21 | and there you see we have our slider
value, we've got a working slider, and we
| | 10:26 | can type something here, and I can
press the Go button, and there's our
| | 10:32 | placeholder for our data.
| | 10:34 | And I can come back to data, I can
change it, I can do something else, and I can
| | 10:39 | press Go, and that will show up here.
| | 10:42 | So, we have a working
navigation controller.
| | 10:45 | We have two scenes that we
can go back and forth to.
| | 10:48 | We haven't written a line of code.
| | 10:50 | So, don't delete this
little project yet.
| | 10:52 | In the next movie,
we're going to build on this.
| | 10:54 | We're going to write just a few lines
of codes, so that our little app can pass
| | 10:58 | data between scenes.
| | Collapse this transcript |
| Coding an iPhone storyboard interface| 00:00 | In this lesson, we're going to build
upon the storyboard we created in the last
| | 00:04 | lesson by writing some code
to pass data between scenes.
| | 00:07 | If you haven't done the exercise in the
last movie, you may want to do that now;
| | 00:11 | you'll be using it in this lesson.
| | 00:14 | So, here I have opened my Temp-storyboard
project from the previous movie.
| | 00:18 | I'm going to go ahead here and zoom out
a little bit because of the size of my
| | 00:23 | screen, so that you can
see the entire storyboard.
| | 00:26 | We have our navigation controller
and we have two view controller'\s.
| | 00:31 | The view controller on the left is
called Data, and the View Controller on the
| | 00:35 | right is called Status,
| | 00:37 | and that's just the labels up here.
| | 00:38 | I haven't actually set
the classes on these yet.
| | 00:41 | If you look at the view controller on
the left, the one called Data, you'll
| | 00:45 | noticed that it's set up
as class BWViewController;
| | 00:48 | and if we look at the one on the right,
it's still a UIViewController, because
| | 00:52 | that hasn't even been set.
| | 00:54 | The BWViewController one was set by
Xcode when it created this project.
| | 01:00 | I'm going to go ahead and close the
utilities, and open up the navigator, and
| | 01:07 | switch over here to BWViewController.m,
and before we start coding, I'm going to
| | 01:13 | go ahead and create a new file.
| | 01:16 | I'll select New > File from the File menu,
and this is going to be an objective-C class.
| | 01:23 | So, I have Cocoa Touch selected on
the left under iOS, and Objective-C class,
| | 01:29 | and I'll press Next.
| | 01:30 | This is going to be the class for our
status view controller, so I'm just going
| | 01:34 | to call it BWStatus, and it's going
to be a subclass of UIViewController.
| | 01:41 | I have not checked Targeted for iPad, and I
have not checked With XIB for user interface.
| | 01:48 | Even though this is spelled X-I-B,
most people pronounce it nib, because, well,
| | 01:52 | how would you pronounce XIB anyway?
| | 01:54 | So, I'm going to press the Next button,
and the default for where to store it is
| | 02:00 | actually the right place.
| | 02:01 | It's in the folder with all
of these other M and H files.
| | 02:05 | You want to make sure that your group,
Temp-storyboard, that's this group here,
| | 02:09 | is selected, and the target
Temp-storyboard is also selected.
| | 02:14 | So, I'll press Create,
and there's our new files;
| | 02:18 | BWStatusViewController, and
BWStatusViewController.h.
[00:02:4.27]
So, real quick, I'm going to
| | 02:25 | come back over here to the storyboard,
and I'm going to select the status view
| | 02:30 | controller on the right.
| | 02:32 | I'm just going to bring up the identify
inspector, and I'm going to select
| | 02:37 | BWStatusViewController
here for the class.
| | 02:42 | I'll press the Enter key; make
sure that that gets selected.
| | 02:48 | So, now this says status view controller
over here on the right, and view
| | 02:52 | controller on the left.
| | 02:54 | So, view controller has BWViewController,
and status view controller has
| | 02:58 | BWStatusViewController, and you can see
how they parse this out for what they
| | 03:03 | put down here in the dock, and it helps you
to be able to identify the correct screens.
| | 03:09 | So, I'm just going to hide my utilities
panel there again, and I'm going to press
| | 03:15 | Command+S to save that file, so it's
no longer gray there in my navigator.
| | 03:19 | I'm going to come over here to our status
view controller, the .h file, and we're
| | 03:24 | going to start by creating synthesized
properties for the values passed from
| | 03:29 | the root view controller.
| | 03:30 | So, under interface and before end, I'll put
in @property, and nonatomic, and strong in
| | 03:40 | parentheses there, and we'll start with an
NSString, and this will be for our textFieldText;
| | 03:49 | and another one just as the same as
the previous one, and this one will be
| | 03:58 | NSNumber, and this will
be our sliderValue.
| | 04:06 | Now, with recent versions of Xcode, you
get the latest version of Objective-C,
| | 04:12 | and in the current version of Objective-C,
this is all you need to do anymore
| | 04:17 | to have a synthesized property.
| | 04:20 | You no longer need to go into the
.m file, and put these synthesized
| | 04:26 | statements up at the top.
| | 04:28 | That happens for you automatically.
All you need to do is put these property
| | 04:33 | directives in the .h file, and I'm putting
them in the .h file rather than the .m
| | 04:38 | files, because these actually are
going to be accessed from another class.
| | 04:42 | So, these need to be public properties,
which is also why I'm synthesizing.
| | 04:46 | Now we can come over here to our
BWViewController.m, and we'll declare IBOutlets
| | 04:53 | for our UI elements. Come up here into
the interface section, and give it some
| | 04:58 | curly braces, so we can put them inside
there, and these are private variables,
| | 05:03 | because they do not need to be
accessed from outside of this class.
| | 05:07 | So, I'm going to select an IBOutlet, and
the first one is a UITextField, and I'm
| | 05:12 | going to give it an underscore, and
call it textField, and I'm pressing
| | 05:18 | Command+S to save.
I just do that a lot by habit.
| | 05:21 | The reason I gave it an underscore
in the front of the variable name is
| | 05:26 | because that's a really valuable convention,
and it's a really common convention.
| | 05:31 | It distinguishes this variable from a
synthesized property, so that I know when
| | 05:36 | I'm accessing this variable.
| | 05:37 | I'm not accessing it through any
setters, or getters, and I know that it's a
| | 05:41 | local private variable.
| | 05:43 | So, I'm going to select another
IBOutlet, and this one is UILabel, and
| | 05:49 | again _statusLabel;
| | 05:54 | and finally, an NSNumber, and this is
just placeholder variable, so it's not
| | 06:00 | an IBOutlet, and this will be a slider value,
and you'll see how we'll use this in a minute.
| | 06:08 | So, I got little connection circles up
for my IBOutlets, and that's all correct.
| | 06:12 | Now I need an action for my slider, so
that I can store that value up here as it
| | 06:20 | changes, and also display it in
my status label as it changes.
| | 06:24 | So, this will be IBAction,
and this is our slider action.
| | 06:30 | We've done this before now, but this
one is going to be a little bit different.
| | 06:35 | We'll have a UISlider, temporary variable
called slider, and we'll assign the
| | 06:41 | value of sender to that.
| | 06:43 | So, that basically de-refences this
polymorphic sender, so that we can access the
| | 06:48 | slider's value, and I'm going to assign
the slider value to this NSNumber up
| | 06:54 | here, and the slider value is actually a
C float, and so we're going to do it this
| | 07:01 | way: NSNumber and numberWithFloat.
| | 07:03 | This will be slider.value.
| | 07:11 | So, that converts the float value from
the slider object into an NSNumber that
| | 07:16 | we can use to pass to the synthesized
variable in our status view controller.
| | 07:21 | Now, the other thing we want to do in
this action is display it in the labels.
| | 07:26 | So, I'll say statusLabel.text = NSString
stringWithFormat, Slider value, and a
| | 07:39 | percent sign, and an at sign,
which will allow it to use this NSNumber
| | 07:46 | object, so that will be
slider value, like that.
| | 07:52 | So, that's our action,
| | 07:54 | and now we get to the interesting part.
| | 07:56 | We're going to come up here, and
we're going to put in a prepareForSegue;
| | 08:01 | prepareForSegue,
which looks like that.
| | 08:09 | This allows us to set properties
in the destination view controller.
| | 08:14 | This prepareForSegue gets called after
the segue has already been set up, and
| | 08:19 | it's called with a sender, and again,
this is a polymorphic sender, and this
| | 08:24 | sender is the segue object, and it contains
a destination view controller, which
| | 08:29 | allows us to set properties on
our destination view controller.
| | 08:33 | So, before we can do this, we need to
come up here and import the header file
| | 08:40 | for our status view controller.
| | 08:46 | Now we can come back down here to
prepareForSegue, and we can create a variable
| | 08:50 | in here, BWStatusViewController.
| | 08:51 | I call it statusController = segue.
destinationViewController, like that, and this
| | 09:05 | just means that that's an unused variable.
| | 09:07 | I've created this variable without using
it yet, and we'll get to that in a moment.
| | 09:12 | Now, there's one other thing I want
to show you before we go and code the
| | 09:16 | rest of this segue.
| | 09:17 | If come back up here into the storyboard,
and I select this segue right here;
| | 09:22 | this is the segue -- I'm going to bring
up the utility inspector, and I'll scroll
| | 09:27 | over here, so we can see --
| | 09:28 | so, I've selected the segue, and I come
over here to the attributes inspector, and
| | 09:33 | there's a place for an identifier, and
I'm going to call this segueToStatus.
| | 09:40 | In this particular instance, it's
not necessary, because there's only one
| | 09:44 | segue, but we'll see an example
later in this chapter where you can have
| | 09:49 | multiple segues from a particular view controller,
and you'll need to be able to identify them.
| | 09:54 | So, we're going to go
ahead and do that here anyway.
| | 09:57 | So, I'm just going to copy this onto
my clipboard, and I'm going to come back
| | 10:04 | into the BWViewController.m, and I'll
go ahead and get rid of this here, so we
| | 10:11 | have some room to type, and I'm going to
say if segue.identifier isEqual ToString
| | 10:24 | and paste in my segue
status there, segueToStatus.
| | 10:26 | Now, I can come down here
and I can cut and paste this.
| | 10:33 | And so, now everything that we put in
here will only happen if there's a segue,
| | 10:39 | and it's the right segue.
| | 10:40 | So, this would allow us to have multiple
segues in this view controller that go
| | 10:45 | to different places, and to still
be able to control them from the one
| | 10:48 | prepareForSegue method.
| | 10:50 | So, that's the way that that's done, and
that's what that segue identifier is for.
| | 10:54 | So, now that I have statusController
variable here, which is of the type
| | 10:58 | BWStatusViewController,
| | 10:59 | so, this is actually pointing
to the actual object that is my
| | 11:04 | destinationViewController, I can go
ahead and set some properties there,
| | 11:08 | I can say
statusController.textFieldText
| | 11:12 | remember that public property that we
setup over there? equals _textField.text
| | 11:20 | and I can say statusController
.sliderValue = _sliderValue.
| | 11:28 | And now, we're completely done
with our BWStatusViewController.
| | 11:31 | I keep pressing Command+S to
save, so that's already saved.
| | 11:35 | I need to come back up here to my
storyboard, and save that as well.
| | 11:39 | And here's our segue, and
there's our segue identifier.
| | 11:45 | So, that's all right,
and I've pressed save.
| | 11:46 | Now we're going to come back up to the
statusViewController.m, and we'll display our text.
| | 11:54 | This is really simple.
| | 11:55 | We come in here into viewDidLoad, and before
we do that, of course, we need our IBOutlets.
| | 12:00 | So, we have IBOutlet for our slider
label, UILabel, and IBOutlet UILabel for our
| | 12:16 | textFieldTextLabel, and then we come
down here, and to viewDidLoad, and we have
| | 12:25 | sliderLabel.text = NSString
stringWithFormat Slider value: %@
| | 12:37 | and self.sliderValue.
| | 12:41 | And of course, we need to say self.
sliderValue, because that's a property.
| | 12:47 | Let's switch this over just a little
bit, and then textFieldLabel, the same
| | 12:51 | thing; .text = self.textFieldText.
| | 12:57 | So, that's completely done now.
| | 12:59 | We've done all of our coding.
| | 13:01 | Now, all we need to do is go to the storyboard,
and hook up our UI elements to the code.
| | 13:06 | So, I'm going to come back
over here to the storyboard.
| | 13:09 | I'm going to start with the data view
controller, and I need to zoom in you do this;
| | 13:13 | now I'm going to right-click
on the view controller there.
| | 13:20 | That's just not enough room to do that
at all, so we're not going to do it that
| | 13:24 | way. We're going to come over here to
the connections inspector, and scroll over
| | 13:29 | here just a little bit.
| | 13:31 | I'll go ahead and I'll hide the document
outline; give us just a skosh more room.
| | 13:36 | So, here, we have the statusLabel.
| | 13:38 | So, that will get connected
to the sliderValue.
| | 13:41 | We have the textField, so that will
get connected to the textField there;
| | 13:47 | and we have the sliderAction, which
will get connected to the horizontal
| | 13:54 | slider and Value Changed.
| | 13:56 | Now, alternately I could -- I'm going to
press Command+Z to undo that last one.
| | 14:03 | Alternately, I could just grab the
slider itself, because this is really the
| | 14:07 | direction that it goes, and bring it
down here to the view controller, and
| | 14:14 | select sliderAction.
| | 14:15 | And you see that's just really
hard to do here on this small screen.
| | 14:19 | But if I select the view controller
again, you'll see that the sliderValue
| | 14:24 | changed is the one that got
hooked up there, so that's fine.
| | 14:27 | So, now our data controller is set up,
| | 14:30 | so we're going to scroll over here
to our status view controller, and this
| | 14:34 | one is really easy.
| | 14:35 | We have the sliderLabel,
which will go to our slider value.
| | 14:40 | We have the textFieldLabel, which
will go to our text field text,
| | 14:45 | and now it should be done.
| | 14:46 | I can save this with Command+S, and
command+R to run it, and that builds and runs,
| | 14:53 | and so here's our slider.
| | 14:55 | As I change this, it says Slider
value, and I can type something in here;
| | 14:59 | something in here, and press Go, and
there we have our values: Slider value,
| | 15:06 | and something in here.
| | 15:07 | I can go back and I can change this.
| | 15:12 | Type something really witty
here, and there we have it.
| | 15:18 | So, that's really all there is to it.
| | 15:20 | The prepareForSegue method makes it
really easy to access the destination view
| | 15:24 | controller. That's really the
only thing that's different here.
| | 15:27 | We obviously had to code two view
controllers, but it's not a lot of code.
| | 15:32 | We wrote very few lines of code;
maybe 20, 25 lines of code.
| | 15:36 | We set up everything else in Interface
Builder, and the storyboards just really
| | 15:41 | made this very easy to make a nice two
screen application that shares data
| | 15:48 | between the screens, and it works
smoothly, it does it well, and it was just
| | 15:54 | really not a whole lot of work.
| | 15:56 | So, we have a fully functional
multiscreen app, with just a few lines
| | 16:00 | of code.
| | Collapse this transcript |
| Exploring an iPad storyboard interface| 00:00 | Storyboards for the iPad are very
similar to those for the iPhone interface.
| | 00:04 | The only significant difference is the
possible use of a split view controller.
| | 00:09 | Here, I've opened a working
copy of BWRSS here in Xcode,
| | 00:14 | and first I'm going to
show you the iPhone storyboard.
| | 00:16 | This is actually a universal application,
so it has both an iPhone storyboard,
| | 00:21 | and an iPad storyboard.
| | 00:23 | So, we're going to take a look at
the iPhone storyboard here first.
| | 00:26 | Again, you'll notice there's not a lot
of room here to show the entire thing,
| | 00:30 | so I zoom out a little bit, and you can
see there's a navigation controller, and
| | 00:35 | then there's a feeds table view controller,
which serves as the root view controller here,
| | 00:41 | and the feeds table view
controller has two segues.
| | 00:45 | It has another table view for the items
table view controller, which is actually
| | 00:51 | selected off of this cell in the table
view controller, so whenever you tap on a
| | 00:55 | cell, it brings up an
items view controller.
| | 00:59 | And then there's this plus button
up here in the navigation bar, and that is
| | 01:04 | connected to this add view controller.
| | 01:07 | And if we look at this segue -- I'll go
ahead and bring up the utilities panel here.
| | 01:15 | If we look at this segue here, we notice
that its type is modal, as opposed to the
| | 01:24 | push controller that we've used before,
and it has an Identifier SegueToAddView.
| | 01:30 | And if we look at this other segue,
which you'll notice it highlights the cell
| | 01:35 | there in the table, and that one
is a push style segue, and it has
| | 01:40 | SegueToItemTableView.
| | 01:42 | So, that one actually segues to this
table view here, the items table view
| | 01:47 | controller, and then that one has a
segue to this UI Web view, which is over here.
| | 01:53 | Likewise, if we look at the iPad storyboard --
I'm going to zoom out here, so we can
| | 01:59 | see the entire thing --
| | 02:00 | it looks a little bit more complicated.
| | 02:02 | There are several more scenes in it, but
it's really not that much more complicated.
| | 02:09 | Over here on the left -- let's see if I
can zoom out just a little bit -- this is a
| | 02:14 | split view controller,
and this is iPad specific.
| | 02:18 | And so, it actually has two view
controllers, a master view controller, and
| | 02:21 | a detail view controller, and each of those
connects to its own navigation controller.
| | 02:26 | So, the master view controller, the
one on the left is connecting to this
| | 02:30 | navigation controller here,
which connects to these table views.
| | 02:35 | If we bring up the application in an
iPad -- go ahead and select the iPad 6.1
| | 02:41 | simulator, and press Command+R to run it,
and I'm going to have to scale it out a
| | 02:52 | whole lot for this small screen here.
| | 02:55 | We can see that if click on Feeds, I get
that master view, and this is the feeds
| | 03:03 | table view controller, and if I click
on one of these, then I get this items
| | 03:08 | table view controller.
| | 03:10 | And if I click on one of these, the pop-up
moves out of the way, and I get this
| | 03:20 | Web view, and this is in the detail view
controller, which is this one over here,
| | 03:24 | and the Web view is down here.
| | 03:27 | So, we have navigation controller
for both the detail view, and another
| | 03:33 | navigation controller for the master
view, and this is all controlled by this
| | 03:40 | split view controller.
| | 03:41 | So, the navigation controller for the master
view, that's the one that has the tables in it,
| | 03:47 | so, that's this one here.
| | 03:50 | And the navigation controller, the
larger shaped one over here, this one
| | 03:56 | controls this Web View, and so
that's the one on the right over here.
| | 04:01 | If I rotate the simulator, on pressing
Command and my left arrow, then we have
| | 04:06 | the iPad in landscape mode, and this is
the beauty of the split view controller;
| | 04:12 | now the master view is visible all
the time and the detail view is also
| | 04:20 | visible all the time.
| | 04:21 | So, when I bring up different pages, and
even if I change blogs over here on the
| | 04:27 | left, and bring up an entirely page,
that all comes up on the right-hand side in
| | 04:34 | the detail view controller.
| | 04:36 | The iPad has a more complex interface,
and so the storyboards are necessarily
| | 04:41 | going to be a little more complex.
| | 04:44 | We'll also notice here in my master
view, the feeds table view controller
| | 04:51 | actually has two different segues.
| | 04:53 | It has a segue that goes to the items
table view, which is this segue here, and
| | 04:59 | you see it says SegueToItemTableView
over here in the attributes inspector,
| | 05:04 | and it also has this other segue,
which comes down here to this add view
| | 05:10 | controller, and this is a modal segue, and so it
actually pops this up over the entire screen.
| | 05:17 | So, if I come back over to our feeds
table view controller, and I press this
| | 05:21 | little plus button up here, you see I
get a different segue, and that brings
| | 05:26 | up this modal view Ccntroller with the add
RSS feeds scene in it, and that's this one here.
| | 05:32 | There is a little quirk in Xcode.
| | 05:34 | It won't show me this background; if I look
down here, and I see the add view controller --
| | 05:41 | I probably need to zoom
into it to see it better
| | 05:46 | to be able to select it -- this Scroll
View here has a background which says,
| | 05:53 | Scroll View Text, and it's this scroll
view textured background color, and that,
| | 05:59 | for some reason, doesn't
actually show up in Xcode.
| | 06:02 | And so, it has it on this white
background, and it's hard to see the white text
| | 06:06 | there, but you see in the actual iPad,
you can see that textured background, and
| | 06:12 | the white text shows up just fine.
| | 06:14 | So, that makes it a little bit
difficult to deal with in Interface Builder.
| | 06:18 | So, I'll press Cancel there,
and that modal dialog goes away.
| | 06:22 | So, one thing that we have here that's
a little different that we haven't seen
| | 06:25 | before is we have a scene that has two
segues attached to it, and that's this
| | 06:32 | scene up here; the one that says Feeds
Table View Controller, and you see it has
| | 06:37 | those two segues, the one to the item
table view, and this other segue that goes
| | 06:42 | to this modal dialog boxes,
this Add View.
| | 06:45 | So, let's take a look real
quick at the code that handles that.
| | 06:49 | I'm going to hide the utilities.
| | 06:51 | Notice that these have
identifiers; SegueToAddView.
| | 06:54 | I always put the identifiers even I'm
not using them, and SegueToItemTableView.
| | 06:59 | In this case, we are using them.
| | 07:01 | So, I'm going to close the utilities,
and I'm going to open up the navigator, and
| | 07:05 | we're going to come over here to the
FeedsTableViewController.m, and I'm going to
| | 07:10 | find our prepareForSegue, and you'll
notice here that I have two segues;
| | 07:15 | if segue.identifier isEqualTo
SegueToItemTableView then, we do the segue to the
| | 07:21 | items table view,
and I populate that object.
| | 07:24 | I have a ItemsTableViewController,
which I assign to the segue
| | 07:29 | DestinationViewController.
Go ahead and close the navigator,
| | 07:35 | so you can see that whole line of code.
| | 07:37 | And down here, when the segue identifier
is equal to SegueToAddView, then instead
| | 07:43 | I have a BWRSSAddViewController,
which I'm assigning from the
| | 07:49 | destinationViewController.
| | 07:51 | And there, I assign a
property within that object.
| | 07:54 | So, you can see that storyboards for
the iPad work basically the same as those
| | 07:59 | for the iPhone, although they
are necessarily more complex.
| | 08:04 | This example shows some of the
possibilities of those complexities.
| | Collapse this transcript |
|
|
6. CompilingUsing the Xcode compilers| 00:00 | The default compiler that ships with
Xcode is the LLVM compiler infrastructure.
| | 00:06 | LLVM originally stood for Low Level
Virtual Machine, but this initialism has
| | 00:11 | been abandoned as the project has evolved
beyond its original focus on virtual machines.
| | 00:15 | It is now much more of a
compiler system than anything else,
| | 00:19 | so officially, LLVM is just a name,
and doesn't have any other significance.
| | 00:25 | LLVM is a modern compiler infrastructure
that includes a number of excellent
| | 00:29 | tools; most notably, Clang, a modern
compiler front-end with native support for C,
| | 00:35 | C++, Objective-C,
and a hybrid Objective C++.
| | 00:40 | Clang represents a significant
advancement in compiler technology,
| | 00:44 | providing outstanding performance,
excellent diagnostics, tight IDE integration,
| | 00:50 | refactoring, static analysis, and a
simple code base unencumbered by the decades
| | 00:56 | of history behind GCC.
| | 00:59 | Because of its modularity, LLVM
has a number of subprojects, including
| | 01:03 | implementations of Java and .net
virtual machines, an OpenCL implementation,
| | 01:08 | other virtual machines and compilers
for Python, Ruby, Haskell, PHP, D, and a
| | 01:15 | wide variety of other languages.
| | 01:18 | Apple is a major contributor
to the open source LLVM project.
| | 01:21 | Xcode also ships with an older
version of GCC; the GNU Compiler Collection.
| | 01:28 | GCC is a very old and mature compiler
system that is widely deployed, and
| | 01:33 | supported on virtually
every modern computing platform.
| | 01:36 | Xcode used GCC as its primary compiler
up to the last versions in the 3.0 branch.
| | 01:43 | Beginning with Xcode 3.1, Apple began
shipping a hybrid compiler with a GCC
| | 01:48 | front-end, using LLVM
for code generation.
| | 01:52 | Once Clang became sufficiently mature,
Apple began recommending the full LLVM
| | 01:57 | Clang compiler system. Starting
with Xcode 4.0, GCC has deprecated.
| | 02:02 | As of Xcode 4.6, an older version
of GCC is still provided with an LLVM
| | 02:09 | back-end code generator.
| | 02:11 | It's included for projects that may
still depend upon it, but it will likely be
| | 02:15 | removed from future
Xcode distributions.
| | 02:18 | In this chapter, we'll explore a few
details about using Xcode with LLVM in some
| | 02:23 | common circumstances.
| | 02:25 | Keep in mind that a compiler is a
huge subject, and we'll only be scratching
| | 02:29 | the surface here.
| | Collapse this transcript |
| Compiling C++ with the LLVM compiler| 00:00 | There may be times when you'll
want to use C or C++ for your project
| | 00:05 | instead of Objective-C.
| | 00:06 | If you do, there are a few
things you'll need to be aware of.
| | 00:09 | I'm going to create a new project,
and this will be an OS X application, a
| | 00:17 | command line tool, and I'll press
Next here, and it's going to be in C++.
| | 00:23 | Under Type, I'm just selecting C++.
| | 00:25 | I'm going to uncheck
automatic reference counting.
| | 00:30 | It probably doesn't matter,
but just in case.
| | 00:33 | And for the project name, I'm
going to call this Temp-C++.
| | 00:36 | I'm going to save this on my Desktop.
| | 00:41 | You can save it wherever you like, as
long as you remember where it is, so that
| | 00:45 | you can delete it later.
| | 00:47 | Here, we have a simple
Hello, World application.
| | 00:50 | If I select the main.cpp, you'll see
it's just a little bit of code that
| | 00:54 | says Hello, World!
| | 00:55 | I'm going to run it.
| | 00:57 | I'm pressing Command+R on my keyboard.
| | 00:59 | Hence, that builds, and runs, and there we
see it says Hello, World! at the bottom.
| | 01:05 | We'll go ahead and hide the utilities
pane there, so we have a little bit more room.
| | 01:10 | Now, if we want to know what compiler
we're running -- and there's a reason for
| | 01:13 | that; that we'll want to know that.
We'll get to that in a minute.
| | 01:17 | I'm going to go ahead here and
I'm going to just change this.
| | 01:20 | I'm going to take out this whole line
that says standard C out, and I'm going to
| | 01:24 | put in print f Compiler version string:
| | 01:29 | backslah n is for a new line, and then a %s\n
and then the %s, of course, is a C string
| | 01:40 | replacement, so I'm going to type in
here _VERSION_ and that's a macro that
| | 01:44 | returns a C string, and put a
semicolon at the end there.
| | 01:49 | So, I'm going to save with Command+S, and
I'm going to press Command+R to build and run.
| | 01:55 | And here, we have --
| | 01:57 | I'm just going to expand this a little bit,
so we have a little bit more room there --
| | 02:01 | Compiler version string:
| | 02:02 | 4.2.1 Compatible Apple
LLVM 4.2 (clang-425.0.24).
| | 02:11 | So, what this all means, the 4.2.1
compatible, this part here means that it's
| | 02:16 | compatible with GCC 4.2.1, and there's
some history behind why that is, but it's
| | 02:23 | an important detail
under some circumstances.
| | 02:26 | Apple LLVM 4.2 is the actual compiler.
| | 02:30 | This is the LLVM compiler, and it's been
compiled by Apple for inclusion in Xcode.
| | 02:37 | So, that's great.
| | 02:38 | So, I'm just going to come up here to
the top, and I'm going to include the
| | 02:43 | vector header, and we're going to
declare a vector, and we're going to test for
| | 02:49 | some C++ 11 features to make sure that
this compiler is capable of compiling C++ 11.
| | 02:54 | I'm just going to say using namespace std,
like that, and I'm going to come down here.
| | 03:03 | I'm going to declare a vector, a string,
call it v, and I'm going to assign it.
| | 03:13 | This is a C++ 11 feature
called an initializer list,
| | 03:18 | and here's another C++ feature, an
autotype, and a range based for loop, so I'm
| | 03:24 | going to say for -- and there is no completion
for this -- auto s : v, like that, and
| | 03:34 | we'll just say, cout << s << endl.
| | 03:40 | So, this is all C++ 11 features, and we're
just testing to see if C++ 11 is supported.
| | 03:46 | We have the vector with the
initializer list; that's a C++ 11 feature.
| | 03:52 | We have the range based for loop, and an
automatic type for this variable, auto
| | 03:57 | S; that would be the same
as if we had said string.
| | 04:01 | So, I'm going to go ahead and build and
run by pressing the little button up here,
| | 04:06 | and down at the bottom here, you can
see that it comes up and it says one, two,
| | 04:11 | three, which is what its supposed to
do, so we can see that this compiler is
| | 04:15 | supporting C++ 11 features.
| | 04:17 | Now, if we come back up here to our
Build Settings, and I select Build Settings
| | 04:22 | up here, and we have Basic, and I'm
going to select All instead, and I'm going to
| | 04:28 | select Level, so that you
can see what all of this is.
| | 04:32 | Go ahead and collapse the
bottom display area there.
| | 04:36 | So, here we have a lot of settings, and
most of these you're never going to want
| | 04:40 | to touch; they would create
a lot of trouble if you did,
| | 04:43 | but I want you to see the
way that this is organized.
| | 04:46 | These settings apply with
increasing specificity from right to left.
| | 04:50 | So, a setting might apply to the
default, in which case, if we were to
| | 04:57 | change that, it would change everything.
| | 04:58 | It would become a new
default for OS X apps on Xcode.
| | 05:03 | The next one here is the project, and
so if I set something there, it would be
| | 05:10 | enforced for the entire project.
| | 05:12 | Then there's the target, and so if I set
something there, it would just be
| | 05:16 | enforced for that target.
| | 05:17 | And then there's the resolved setting;
| | 05:18 | what is the setting for this target,
because we have this target selected.
| | 05:23 | So, if I come down here to Build Options,
we can see that the compiler is the
| | 05:29 | default compiler,
which is the Apple LLVM.
| | 05:33 | And if I come over here to the project,
and I change that to the LLVM GCC
| | 05:40 | instead, now you see it resolves to
LLVM GCC 4.2, and If I come back over
| | 05:47 | here to our source code, and I go ahead and I
do a build and run, you'll see we get errors.
| | 05:55 | The errors are right on all this C++ 11
stuff, because this compiler does not
| | 06:02 | support C++ 11. It's too old.
| | 06:04 | This is actually a five
or six year old compiler.
| | 06:08 | If I take this out, I'll just select
all of this here, and I'll comment it
| | 06:13 | out. I'm pressing Command and the
forward slash key on my keyboard, and
| | 06:18 | that comments it out.
| | 06:19 | Command+S to save,
Command+R to build and run.
| | 06:23 | Now it runs just fine, and we get our
Compiler version string, which is 4.2.1
| | 06:30 | Based on Apple build
number and LLVM build number.
| | 06:34 | So, this is GCC 4.2.1, with the
LLVM code generator on the back end.
| | 06:42 | That's what this is, and that's why it's
got this LLVM built on the back of it.
| | 06:48 | Now, just a note; the reason that GCC
is up to a version 7 or something like
| | 06:52 | that at this point -- the reason that
Apple is still shipping 4.2.1 is that's the
| | 06:57 | last version that was under the
old GNU General Public License 2.0.
| | 07:03 | The 3.0 version of the General Public
License, Apple didn't feel like they could
| | 07:07 | use it anymore, and so that's the last
version of GCC that they're shipping.
| | 07:12 | Now, if I come back up here, and switch
back over here, back to the LLVM 4.2, and
| | 07:24 | then we come back over here to our
project, and we build and run, you see our
| | 07:28 | compiler version string
now says 4.2.1 Compatible.
| | 07:32 | So, it's not just saying 4.2.1; it's saying,
4.2.1 Compatible Apple LLVM with Clang.
| | 07:40 | And now, if I come and uncomment all
of this, I'll select this again, and I'll
| | 07:44 | press Command+slash, and Command+S to
save, and Command+R to build and run,
| | 07:50 | now we have our C++ 11 features again.
| | 07:55 | So, Xcode allows you to compile a C++
code right out of the box, using the modern
| | 08:00 | LLVM Clang compiler system, or the
Legacy GCC compiler with an LLVM back-end.
| | 08:07 | In the next movie, we'll look at
compiling from the command line.
| | Collapse this transcript |
| Using the compiler from the command line| 00:00 | With Xcodes command line tools installed, it's
also possible to compile from the command line.
| | 00:06 | Of course, if you need to do this, you should be
very familiar with using a UNIX command line.
| | 00:11 | If you don't have the skills already, I
suggest the excellent UNIX for Mac OS X
| | 00:16 | Users on the Lynda.com
Online Training Library.
| | 00:19 | So, I am going to start up Apple's
Terminal app. I am going to come up here to
| | 00:24 | the Spotlight search, and just type in
terminal. I'd only have type in a few
| | 00:28 | letters of it, I can press Enter,
and that brings up the Terminal.
| | 00:34 | I'm going to change directories to my
Desktop, and the Exercise Files, and inside
| | 00:40 | the Exercise Files, a folder called C.
If I type ls-l, I see a listing of the
| | 00:47 | files in that directory.
| | 00:49 | I'm going to make a working copy
of hello.cpp, just by typing cp
| | 00:55 | hello.cpp hello-working.cpp.
| | 01:00 | Now I type ls, and you see we have hello-
working, as well as all of the other files.
| | 01:07 | Now, I need to edit this file.
| | 01:09 | I could use a command line editor, like
VI, if you're familiar with that, or you
| | 01:13 | could use a good source code
editor, like BBEdit, or TextWrangler.
| | 01:17 | Do not use TextEdit; the editor that
comes with Mac OS X. That is not a plain
| | 01:22 | text editor, and it can really mess up a source
code file. Or, you could edit it with Xcode.
| | 01:27 | Now, I could just open Xcode down here
on my dock, and I can come up here to the
| | 01:34 | File menu, and say Open, and go to my
Exercise Files, navigate to the folder, and
| | 01:41 | open hello-working.cpp,
and there we have it.
| | 01:45 | We have our little text editor right there.
Or -- and I'm just going to quit Xcode
| | 01:51 | with Command Q here -- I'm already at
the command line, and I probably have my
| | 01:55 | fingers on the keyboard already and I
just want to edit a file, so I can just
| | 02:00 | type xed, and the name of the file.
| | 02:03 | Xed is simply how you start up
Xcode's editor from the command line.
| | 02:10 | So, xed hello-working.cpp, and you see
it opens Xcode, and it opens that file in
| | 02:18 | the editor all from the command line.
| | 02:21 | So, here we have a little hello.cpp file.
This is actually one of the files from
| | 02:26 | my C and C++ Essential
Training here on Lynda.com.
| | 02:30 | I'm just going to take this whole cout
line here, and I'm going to replace it
| | 02:35 | with another one;
I'm going to type cout.
| | 02:37 | And you notice, because we're using
Xcode, that we have Xcode's completions here.
| | 02:42 | Let's say, Compiler; see?
Xcode has put in the second quote.
| | 02:48 | Compiler version is:
| | 02:51 | and I can just write there over the
quote, and I type endl and __VERSION and
| | 03:02 | another two underscores, and endl
again -- you notice it completes endl for
| | 03:08 | me -- and a semicolon.
| | 03:10 | Now I'm going to press Command+S to
save this, and you'll notice this icon here
| | 03:15 | will change, and it got dark.
| | 03:18 | I'm going to come back to the command
line, and now I want to build and run that.
| | 03:22 | Of course, I'm not using Xcode's IDE;
| | 03:25 | I'm just using it for
a dumb editor there,
| | 03:27 | so how do I compile
from the command line?
| | 03:30 | Well, it's as simple as
this: c++ hello-working.cpp.
| | 03:38 | That compiles, and builds, and now we
have, if we type ls-l again, you'll see
| | 03:44 | we have a new file called a.out, and
if you understand UNIX permissions, you
| | 03:48 | see that's executable.
| | 03:49 | That's what all those x's are.
| | 03:52 | And so, if I want to run a.out, I
type ./a.out, and press Enter, and it says
| | 03:59 | compiler version is 4.2.1
Compatible Apple LLVM, etcetera.
| | 04:05 | So, naming the Apple file a.out like
this is an old UNIX tradition, and we're not
| | 04:11 | in the days of old UNIX anymore.
| | 04:13 | So, if we want to have it say hello, for
example, we can do it this way; we can
| | 04:17 | say c++ -o, and that's a lower case o,
hello, and then the name of this source
| | 04:24 | file, hello-working.cpp.
| | 04:27 | Now if I type ls-l, you'll see that we
have hello, and you'll notice it's exactly
| | 04:34 | the same size as a.out.
| | 04:36 | And so I can type ./hello, and press
Enter, and I'm running our program.
| | 04:43 | So, there is a couple of things worth
noting; if I type g++ instead of c++, I'll
| | 04:48 | get the GNU compiler.
| | 04:49 | So, I'm going to type -o hello, and our
source file name, and I'm going to run it
| | 04:58 | hello, and you'll see that now
it says 4.2.1 (Based on Apple).
| | 05:03 | So, this is the GNU version of
the compiler, so that's g++.
| | 05:09 | If instead of c++, I type cpp, and -o
hello hello-working.cpp, you notice that I
| | 05:19 | get that. It's just an error.
It says c:
| | 05:22 | No such file or directory.
| | 05:24 | The truth of this is, is that cpp
is not the compiler name at all;
| | 05:28 | cpp is the C preprocessor.
| | 05:30 | If I just type cpp hello-working.cpp,
it's basically preprocessing it for me.
| | 05:38 | It's telling me, oh, here is the header
files; I'm going to have to read those.
| | 05:42 | There is an error message, and
it's just spitting back the source.
| | 05:45 | So, this is, in fact, not even
compiling it; this is just the preprocessor.
| | 05:50 | The compiler is C++. It is not cpp;
cpp is an error. It's another program.
| | 05:56 | It's the C preprocessor.
| | 05:56 | C++ -o hello hello-working.cpp; that's the
compiler, and that is Apple's LLVM compiler.
| | 06:09 | So, that's just a little bit of the
capabilities of compiling from the command line.
| | 06:13 | For most projects, you'll probably
want to use the full Xcode IDE, but
| | 06:17 | sometimes it's necessary to use these
tools, especially for projects from an
| | 06:21 | outside source.
| | Collapse this transcript |
|
|
7. DebuggingUnderstanding the LLDB compiler| 00:00 | Debugging is the practice of
finding bugs in your code and fixing them.
| | 00:04 | There are many tools and techniques
available to accomplish this, and even more
| | 00:08 | opinions about how it should be done.
| | 00:11 | The term debugging is widely attributed
to U.S. Navy Admiral Grace Hopper, often
| | 00:16 | called Amazing Grace.
| | 00:18 | Admiral Hopper developed the first
compiler, and was the first to conceive of
| | 00:22 | machine-independent computer languages,
which led the development of COBOL.
| | 00:27 | As the story goes, at some point in
the 1940s, then Lieutenant Grace, a
| | 00:31 | mathematics professor at Vassar, and a
Naval Reserve Officer, was serving as a
| | 00:36 | programmer on the Harvard Mark I, one
of the first programmable computers,
| | 00:40 | when one of her associates
discovered a moth stuck in a relay.
| | 00:44 | She remarked that he was
debugging the computer.
| | 00:46 | Xcode provides two debuggers;
| | 00:49 | the legacy GDB, part of GNU GCC project,
and the newer LLDB, part of the LLVM project.
| | 00:56 | We will be using LLDB for this chapter.
| | 01:00 | The debugger provides support for
breakpoints, program execution control,
| | 01:05 | examining threads and stacks, examining
auto, local, and static variables,
| | 01:09 | examining registers, and much more.
| | 01:11 | In the next movie, we'll take a look at
the debugger in action, and I'll give you
| | 01:15 | an idea of how it's used,
and what it can do for you.
| | Collapse this transcript |
| Using the debugger from within the editor| 00:00 | The debugger is a powerful tool, and
like most powerful tools, it requires some
| | 00:04 | knowledge and patience
to get the most out of it.
| | 00:07 | Here I have open a working copy of
BWRSS here in Xcode, and the first thing
| | 00:13 | I'm going to do is I'm just going to
create a runtime error, so that we can
| | 00:17 | trigger the debugger.
| | 00:18 | So, I'm going to come up here to the
FeedsTableViewController, and I'm going to
| | 00:23 | select -- where is it here --
CellForRowAtIndexPath.
| | 00:25 | This is the method that actually
displays stuff in the table view.
| | 00:34 | I'm just going to come in here, and I'm
going to create an error, and I'm going to
| | 00:38 | create a very common runtime error.
| | 00:39 | If you ever want to get a program
to crash intentionally, this is the
| | 00:43 | easiest way to do it.
| | 00:44 | We're going to create a pointer, a
character pointer, in this case; we'll call
| | 00:49 | it cp, and we're going to assign it a value of
nil, which is the null pointer in Objective-C.
| | 00:56 | And then I'm going to dereference that
pointer, and I'm going to assign a value.
| | 01:03 | Any value will do. I'm just going to
assign zero to what is pointed out by that
| | 01:07 | character pointer, which, in
this case, is pointing at null.
| | 01:11 | It's what we call a null pointer, and
you cannot assign to a dereferenced null
| | 01:14 | pointer; that is always a
runtime error. I can save this.
| | 01:20 | It's perfectly legal C, or an Objective-C,
in this case, so I'm going to press
| | 01:25 | Command+B to build, and
you see it builds just fine.
| | 01:27 | I'm going to press Command+R, which I'll
run it in the simulator, and you'll see
| | 01:32 | it will trigger the debugger, because
its code is invalid, and will crash.
| | 01:37 | So, I'm going to press Command+R here,
and there we have it; it has crashed, and
| | 01:44 | triggered the debugger.
| | 01:47 | So, the error message we get is -- I've
seen this a lot, because I've made this
| | 01:51 | mistake unintentionally -- is execution
bad access (EXC_BAD_ACCESS) address=0x0.
| | 01:58 | I know it's not the world's
most friendliest error message.
| | 02:02 | I happen to know what that means is I
tried to assign something to a null pointer.
| | 02:05 | But what this does for us is it
gives us opportunity to explore the
| | 02:09 | debugger here a little bit.
| | 02:10 | You'll notice that the navigator switched
to the debug view, so this is the debug
| | 02:15 | navigator here. And you'll notice that
the debug area popped up at the bottom,
| | 02:20 | with the debugger on the left,
and the console on the right.
| | 02:23 | And so, you see some stuff down here in
the debugger, and you see some stuff over
| | 02:28 | here in the debug navigator.
| | 02:29 | In the debug navigator, what we have
is the stack, or at least the relevant
| | 02:33 | portions of the call stack, and so
BWRSS FeedsTableViewController was called
| | 02:39 | by UITableViewInternal
createPreparedCellForGlobalRow:WithIndexPath,
| | 02:47 | and that was called by a bunch of other stuff
that was called by UIApplicationMain
| | 02:51 | that was called by main.
| | 02:52 | If I click on main here, you see there
is our main, and it's in main.m, and it
| | 02:59 | called this, which called 37
other things, and ended up here.
| | 03:04 | If you want to see a more complete stack --
that one is abbreviated, because most
| | 03:08 | of the stuff we
don't have access to anyway.
| | 03:10 | But if you want to see the whole thing,
you can click over here in the debug
| | 03:14 | areas jump bar, and I'm
just going to scroll that.
| | 03:19 | There is not room on this screen for it,
but there is all of those things, and you
| | 03:24 | can see some of them are cocoa, some of
them are different libraries, and if you
| | 03:28 | can click on any of these, what you'll
get is assembly language, because we don't
| | 03:33 | even have the source code for that.
| | 03:35 | So, I'm just going to go
ahead and click back on our BWRSS
| | 03:39 | FeedsTableViewController, because that's
what we have the source code to, and
| | 03:44 | that's what we're working on, and
frankly, that's where our bug is.
| | 03:47 | So, I'm just going to delete this bad code
here, and I'm going to press Command+S to save.
| | 03:53 | I'm going to stop the execution;
see, it's still running here,
| | 03:56 | even though if we look at the
simulator, nothing is happening.
| | 04:02 | But it is technically still running
in the debugger, so I'm going to press
| | 04:05 | Command and the Period
| | 04:06 | on my keyboard, which finishes
the execution. It simply stops it.
| | 04:09 | Now we can find some other
ways to explore the debugger.
| | 04:13 | One of the great things that a
debugger provides is a breakpoint.
| | 04:17 | And so I can set a breakpoint here,
and I'm going to set it on line 18;
| | 04:22 | the execution will actually stop on
line 120, because line 18 is a static
| | 04:26 | declaration, and that's provided by
the compiler. That doesn't actually do
| | 04:29 | anything at runtime.
| | 04:31 | Our first runtime code is
this assignment here on line 120.
| | 04:36 | So, with this breakpoint in place. when I
press Command+R to run, and I'm going to
| | 04:41 | do that right now, it will build, and
it will run, and it will bring up the
| | 04:48 | debugger with the breakpoint.
| | 04:49 | Now we don't have an error, but we have
intentionally stopped execution, because
| | 04:53 | we want to see what's going on in our code.
| | 04:56 | Now we've stopped at the beginning
at the top of a function, which is not
| | 04:59 | normally where you'll stop, but it
works for our purposes right here.
| | 05:03 | You see we have the same debug
navigator on the left with our stack, and of
| | 05:07 | course, there are stacks on all these
other threads too, but they don't really
| | 05:11 | matter to us, because that's not our code.
| | 05:13 | You can also get to add those
other threads here in the jump bar.
| | 05:19 | We're really interested in the debugger
itself, and the debugger mostly happens
| | 05:24 | down here in the debug area
at the bottom of the screen.
| | 05:27 | So, you see on the left we
have our variables; we have self,
| | 05:31 | we have tableView,
indexPath, cell, CellIdentifier.
| | 05:36 | You notice that cell is still zero,
because we haven't set it to anything yet,
| | 05:40 | which is what's about to happen here.
| | 05:43 | If we want to see that happen, we can
step, and so here on the jump bar, we
| | 05:48 | have some controls.
| | 05:49 | The first one will just continue program
execution, so it will go around until
| | 05:53 | it gets this breakpoint again,
and stop again.
| | 05:55 | The next one is step over, and
then there is step into, and step out.
| | 06:00 | So, I'm going to press Step Over, because
what that will do is that will actually
| | 06:04 | go to the next line of
code here in this function.
| | 06:06 | That's the one that you're
going to use the most often.
| | 06:09 | And watch the cell variable her; when
I press Step Over, now the cell variable
| | 06:14 | has a value, and that is the address,
because all objects in Objective-C are
| | 06:20 | pointers, so that's the
address of the cell object.
| | 06:25 | If I press Step Over again, we'll get
down here to this loadFeedIDsIfEmpty.
| | 06:32 | That is a great opportunity
for me to show you step into.
| | 06:35 | If I press Step Over again, it'll
come down here to this next assignment on
| | 06:39 | line 128, but if I press Step Into, it
will actually step into this method
| | 06:45 | that's being called, loadFeedIDsIfEmpty.
So, I'm going to press that, and now
| | 06:49 | there we are in that function.
| | 06:51 | And if I press Step Out of, it will
step past this return into the caller, and
| | 06:59 | it goes right back there where we were.
| | 07:02 | This method has already run, but we're
still here, because we haven't executed
| | 07:06 | the next instruction.
| | 07:07 | So, we're going press Step Over again,
and now we're over here to where the
| | 07:12 | feedRow is about to be assigned. You
notice feedRow has been declared here, and
| | 07:17 | so we have it in our variables, but it
doesn't have any key value pairs yet.
| | 07:21 | If I press Step Over again, now it has
six key value pairs, and we can click on
| | 07:27 | this disclosure triangle,
and there they are.
| | 07:31 | We can see each of them: key
value, key value, key value.
| | 07:42 | I want to show you some more here, and
in order to do that, I want to get down
| | 07:46 | here to this return cell. So, I'm
going to show you another feature.
| | 07:50 | If I hover my mouse over any of these
lines here in the debug bar, I get this
| | 07:55 | little green arrow, and if I press on it,
execution will continue up until that
| | 08:01 | point, and so there, I just stepped over
several lines of code to that particular point.
| | 08:07 | It's a convenient way to
get to a particular point.
| | 08:11 | One of the things that's happened here is
that these cell text labels have now been assigned.
| | 08:18 | And if I look at cell, you notice I
get a whole lot of stuff, and there is the
| | 08:25 | reuse identifier, but I'm not actually
seeing the text label, or the detail text label.
| | 08:34 | And there's interesting reasons as
to why that is, but the fact is that I
| | 08:39 | don't get to see them.
| | 08:41 | So, there are a couple of things that we can
do, and I just want to show you a command here;
| | 08:46 | you're not going to use the command line
of LLDB very often, but it is available
| | 08:50 | to you right here if you want it.
| | 08:52 | The most common command that you
will use is po, which means print object.
| | 08:58 | So, I can say po cell.textLabel.
| | 09:02 | I even get code completion here
.text.
| | 09:05 | And when I press Return, we get
that text label, and I can also po
| | 09:11 | cell.detailTextLabel.text, and
I get the detail text as well.
| | 09:20 | So, that's a really useful way to be
able to see those values that you wouldn't
| | 09:24 | otherwise be able to see.
| | 09:26 | Another common way to do it; I'm
going to press Command+Period
| | 09:30 | to stop execution, and I'm going to go
in here, and I'm going to use NSLog, and I
| | 09:39 | can just say textLabel: %@,
| | 09:45 | which will print an
object, cell.textLabel.text.
| | 09:52 | And I can to the same thing
again for the detailText,Label.
| | 10:04 | I forgot my closing quote here, so
it's not giving doing my code completion,
| | 10:12 | because it's actually an
error; cell.detaiTextLabel.text.
| | 10:19 | Looks like I didn't complete
my text over there.
| | 10:22 | Now when I run this, I m going to actually
delete this breakpoint, and I'm going
| | 10:28 | to create a new one
down here at return cell.
| | 10:31 | Now, when I build and run, our breakpoint
will be down here after these NSLog
| | 10:38 | calls, and these NSLog calls will actually
display these values in our consoles.
| | 10:44 | I'm going to press Command+R, and there
it is. We have those strings; they are in
| | 10:50 | our console, so we don't have to do
our po commands in the debugger.
| | 10:54 | So, these are two different ways that you
can accomplish more or less the same thing;
| | 10:58 | po is a little bit more convenient
if you don't already have the NSLogs in
| | 11:02 | there, and it's something
you just want once,
| | 11:04 | and the NSLogs are more useful if you're
going to want to see that over and over.
| | 11:09 | If I go ahead here, and I press the
Continue program execution button, it will
| | 11:16 | go around again, and I'll get the next
record there printed out in our log.
| | 11:21 | So, I'm going to leave that there for a
moment, and I'm going to come over here to
| | 11:25 | the breakpoint navigator. I
just want to show you this.
| | 11:27 | This will list all of your breakpoints,
and allow you to do things with them.
| | 11:30 | I'm going to right-click or Ctrl+click
on this breakpoint here, and I'm going to
| | 11:35 | say Edit Breakpoint, and it allows
you to set some different things.
| | 11:39 | You can ignore it so many
times before stopping.
| | 11:41 | You can add an action which allows you
to do a debugger command, or run Apple
| | 11:47 | script, or a log message,
or play a sound.
| | 11:54 | You can do all kinds of things, but the
most interesting is you can actually set
| | 11:57 | a condition that this breakpoint
will only actually break execution on a
| | 12:02 | particular condition.
| | 12:03 | And I'm going to set a condition here;
I'm going to say cell.textLabel -- and I
| | 12:09 | don't get code completion here,
so I have to type it -- .text is
| | 12:13 | EqualToString@"lynda.blog" and I have
to remember to close my square bracket. So,
| | 12:25 | that's actual Objective-C,
and I'm going to say Done here.
| | 12:29 | I'm going to press Command+Period
| | 12:31 | to stop execution, and there is our
breakpoint, and it has our condition, and now
| | 12:37 | when I build and run with Command+R,
you'll see that this runs twice, and it
| | 12:44 | actually gets to that place where our
cell text label is equal to that string.
| | 12:50 | So, here is textLabel, it's equal to
lynda.blog, and If I come in here and I say
| | 12:55 | po cell.textLabel.text, it says
that it's equal to lynda.blog.
| | 13:06 | That's pretty much the
vital parts of the debugger.
| | 13:09 | There is a lot more to it; it's a very,
very intricate and complex tool, but
| | 13:13 | this is how to get you started, and this
ought to be most of what you need to
| | 13:18 | know, most of the time.
| | 13:19 | So, the debugger is a powerful tool, and
with some patience and experience, you'll
| | 13:22 | learn to use it well.
| | Collapse this transcript |
|
|
8. WorkspacesCreating a workspace| 00:01 | Xcode's Workspace feature
is new with Xcode 4.
| | 00:03 | It allows you to keep several projects and
file systems available in your workspace.
| | 00:08 | Let's take a look at how this works.
| | 00:10 | You'll notice here in my Exercise Files
and on my Desktop, I've created working
| | 00:14 | copies of three different things;
| | 00:15 | TestBedOSX, TestBedIOS,
and the isPrime folder.
| | 00:21 | That folder has a couple of files in it
that's a little prime number checker in C.
| | 00:26 | I'm going to start by starting up Xcode,
and you'll notice that there is no new
| | 00:31 | workspace button here in the welcome screen,
so I'm going to come up to the File menu,
| | 00:36 | and under New, I'm going to select
Workspace to create a new workspace.
| | 00:39 | So, this creates an empty workspace.
I'm going to name this Temp-workspace, and
| | 00:45 | I'm just going to put it on my Desktop,
and you can store this wherever you'd like,
| | 00:49 | as long as you remember where it
is, so that you can delete it later.
| | 00:53 | I'll press, Save and this creates a
new workspace, and you'll notice it's
| | 00:57 | completely empty; it has nothing in it.
| | 01:00 | So, I can add things to it; I can press
this little plus button down here in
| | 01:04 | the search bar at the bottom of the
navigator, and I get choices for New File,
| | 01:09 | New Project, or Add Files.
| | 01:11 | So, New File, and New Project; those
would create new things. A New Project, a new
| | 01:16 | empty project, a new empty file.
| | 01:18 | Add Files is for adding
existing projects and file systems.
| | 01:22 | So, I'm going to click Add Files;
actually, I can click that from right here on
| | 01:27 | the navigator if I press the Ctrl
key, and click at the same time, or I can
| | 01:32 | right-click, and there is the Add Files
there, and we have other new things we can
| | 01:36 | put in there as well.
| | 01:37 | So, I'm going to click Add
Files to Temp-workspace.
| | 01:40 | Now I'm going to navigate over here to
my Exercise Files, and I'm just going to
| | 01:44 | grab this TestBedIOS working.
| | 01:47 | Now, if just add the folder, it will
just add a folder over here, and it won't
| | 01:52 | actually add the project.
| | 01:53 | If I want to add the project, I have to
select the project file, and click Add.
| | 01:58 | But before I click Add, I want to check
these, and make sure that these are correct.
| | 02:03 | Under Destination I do not want to
check Copy Items, because I actually want
| | 02:08 | this to be a link, not a copy.
| | 02:10 | I want it be working
on the original files.
| | 02:13 | And that way, if I'm working on those
files in another context, in another
| | 02:16 | workspace, or working on the project
directly, that also gets updated here.
| | 02:21 | So, I don't want to copy this here;
then I end up having multiple copies of my
| | 02:25 | project, and it becomes
difficult to keep track of.
| | 02:28 | And under Folders, I'm just going to
leave Create groups for any folders selected,
| | 02:32 | and I'm going select Add, and now we
have the TestBedIOS project here in the
| | 02:38 | project navigator, and there is
all our source files for that.
| | 02:42 | And you notice up here in Scheme, I have
TestBedIOS, and I can select the iPhone
| | 02:46 | 6.1 Simulator, and I can actually
even build and run; I'm going to press
| | 02:52 | Command+R here on my keyboard, and it
builds it, and it runs it in the simulator,
| | 03:00 | and there we have our familiar
TestBed for iOS: Klaatu, barada, nikto.
| | 03:04 | So, I'm just going to come back
here to Xcode, and I'm going to
| | 03:07 | press Command+Period
| | 03:09 | to finish running that, and now I'm
going to add another project. So, there is a
| | 03:14 | couple of ways I can do this.
| | 03:15 | If I just come up here and press the
plus button, it says Add Files to
| | 03:19 | "Temp-workspace",
but that's not really reliable.
| | 03:21 | If I'd already selected a file here
inside of my TestBedIOS, then when I press
| | 03:27 | this, it would say Add Files to TestBedIOS,
which is not what I want to do, so you
| | 03:31 | have to be careful, and look at this.
| | 03:33 | And Xcode doesn't allow me a way -- I'm
clicking here, and it doesn't allow me a
| | 03:38 | way to unselect that project.
| | 03:41 | So, the reliable way to do this is either
to Ctrl+Click in this empty space here,
| | 03:47 | and there I get Add Files to "Temp-
workspace" or just simply go to the finder,
| | 03:52 | and drag this other project in.
| | 03:54 | So, I'm going to select this TestBedIOS-
working, and again, if I were to just drag
| | 03:59 | the files, I would get a file
system; I wouldn't get the project.
| | 04:02 | So, I'm going to open that up, and I'm
going to grab the project file, and I'm
| | 04:07 | going to drag that into my space here.
| | 04:09 | And you notice the little
blue line with the circle.
| | 04:11 | If it's not far enough to left, you see
TestBedIOS is selected there at the top,
| | 04:15 | and it would actually nest this
project inside of my other project.
| | 04:19 | I need to drag it a little bit farther
over to the left, so that that other
| | 04:23 | project is not highlighted, and then
when I let it go, it will actually be a
| | 04:28 | separate project in my workspace.
| | 04:32 | So, it's a little bit tricky, no
matter how you slice it, to get that second
| | 04:36 | project in, but it can be done
if you know what you're doing.
| | 04:39 | Now we have the
TestBedOSX project, and there it is.
| | 04:43 | You'll notice my Scheme here still
says TestBedIOS, so if I were to press
| | 04:48 | build and run at this point, I'm pressing
Command+R, it would run my iOS project again.
| | 04:53 | So, I'll come back here to Xcode,
I'll press Command+Period
| | 04:56 | to finish running that, and then I'm
going to come up here, and I'm going to
| | 05:01 | select the TestBedOSX Scheme, and now
when I press build and run, I'm pressing
| | 05:06 | Command+R in my keyboard again, now it
builds TestBedOSX, and it runs that OS X
| | 05:13 | application, and there it is, the
TestBed for OS X: Klaatu, barada, nikto.
| | 05:19 | So, I'm going to press Command+Q to
stop that from running, and now we've
| | 05:23 | successfully brought two different
projects in, so we can work on these
| | 05:26 | two projects together.
| | 05:27 | A workspace can be convenient for
working on two projects at once, but it's of
| | 05:31 | limited usefulness unless the
projects can share resources.
| | 05:35 | Unfortunately, currently, Xcode lacks
support for sharing resources between
| | 05:40 | projects, but there is a workaround,
so don't delete this workspace example
| | 05:44 | yet, and we'll explore this a
little deeper in the next movie.
| | Collapse this transcript |
| Sharing resources in a workspace| 00:00 | Xcode's new Workspace feature can be
convenient for working on two or more
| | 00:05 | projects at once, but it's of limited
usefulness unless the projects can share resources.
| | 00:09 | Currently, Xcode lacks support for
sharing resources between projects, but
| | 00:13 | there is a workaround.
| | 00:15 | The recommended way to do this is to
create a framework, and I can see how that
| | 00:19 | would have its advantages, but it's
also a more complex process than should be
| | 00:23 | necessary for a small
library of common functions.
| | 00:26 | The easier way would be to add files
to the target sources, and link them
| | 00:30 | without copying them.
| | 00:31 | This would allow you to work on those
files in the context of either project, or
| | 00:35 | separately, and have those files
remain in sync whenever they're used.
| | 00:39 | So, the first thing we would need to do
is to bring this small library into our
| | 00:44 | workspace, and so here in the Finder, in
our Exercise Files, I've made a working
| | 00:49 | copy of isPrime-working, and you see
it's got just a couple of small files in it,
| | 00:53 | which is just a simple function in C
or Objective-C, that checks a number to
| | 00:58 | see if it's prime or not.
| | 00:59 | So, I'm going to grab this whole folder,
and I'm going to bring it into my workspace.
| | 01:03 | Again, you see I need to bring it far
enough to the left, so that that TestBedOSX
| | 01:08 | is not highlighted,
and I'll let go of it there.
| | 01:11 | Here in Xcode, I get the nice little
dialog box. It allows me to link the files
| | 01:16 | instead of copying them.
| | 01:17 | So, I make sure that copy is
not checked, and I press Finish.
| | 01:23 | There we have our little
isPrime function in our workspace.
| | 01:27 | You notice it's not inside
either of these projects.
| | 01:30 | It's separately out in the workspace.
| | 01:32 | So, if I click on isPrime.m, you see this
simple little C function for checking if
| | 01:38 | a number is prime, and I want to bring
that into my different projects, but I
| | 01:43 | want to link it in in a way that I
can still work on it in one place, and the
| | 01:48 | changes will be affected in the other places, so
that I can manage this small library overtime.
| | 01:52 | There are two steps to this process.
| | 01:54 | The first is to add the
.m file to the project.
| | 01:57 | Now, I could just drag the
.m file into a project.
| | 02:00 | The problem is, if I did that, it would
not get added to the build for that project.
| | 02:06 | It wouldn't get built, and linked in with the
other files, so you wouldn't be able to use it.
| | 02:11 | You would think that you should be
able to, and in fact, if you drag the whole
| | 02:15 | folder in, it will pretend to allow that.
| | 02:17 | It will give you a little checkbox for
including that folder in the target, but
| | 02:21 | it doesn't actually work.
| | 02:23 | So, the way to do this is to select
the target, and here, when I select the
| | 02:28 | target up here, I get a choice of the
project or the target down here, and I
| | 02:33 | want to select the target.
| | 02:34 | I come over here to Build Phases, and
I open up this Compile Sources part.
| | 02:39 | Now, here I can press the plus sign
here, and I get to add items to the target.
| | 02:47 | The problem is is that the
whole workspace is not available.
| | 02:50 | The only thing that's available here
is that particular target and its files.
| | 02:54 | So, I have to click on Add Other, and
then I need to navigate over to my
| | 02:58 | Exercise Files, and select our
isPrime-working, and select that file
| | 03:02 | individually, and press Open.
| | 03:05 | Again, I get this dialog box, and I
want to make sure copy is unchecked, and
| | 03:09 | I'll select Finish.
| | 03:10 | Now you notice that it's
included in the Compile Sources.
| | 03:12 | There are three items now.
| | 03:14 | These three items will all be compiled
and linked together to create this target,
| | 03:18 | and that's great;
that's exactly what we want.
| | 03:20 | You notice that isPrime is now
displayed there in my project.
| | 03:24 | In fact, I can drag it here into TestBedOSX
into that little group if I want to,
| | 03:30 | and I can select it there, and
it's the same as selecting it here.
| | 03:34 | You'll notice the full path is out
there in my Desktop Exercise Files, and it's
| | 03:39 | exactly the same as if I select it here.
| | 03:41 | It's the exact same full path.
| | 03:43 | You'll also notice, when I select it
inside of the project, I have this Target
| | 03:48 | Membership, and it's
checked, and it says TestBedOSX.
| | 03:51 | That means that we've successfully
made this a part of that target.
| | 03:54 | Now we want to call it.
| | 03:56 | The problem is, in order to call it, we
have to bring in the .h file, and if I
| | 04:01 | select on my AppDelegate here, and I
come in here to the code, and I type #import
| | 04:05 | "isPrime.h", you see it even
comes up in the code completion;
| | 04:08 | you'll notice I'll get the red exclamation
point, and when I click on that, it
| | 04:13 | says isPrime.h file not found.
| | 04:15 | So, even though it knows it's there, and
it knows where it is, because it was able
| | 04:19 | to do the code completion, it's not able
to find it for this purpose, and so what
| | 04:24 | we're going to need to do is we're
going to need to tell it where it is, and we
| | 04:28 | do that again by selecting the target.
| | 04:30 | This time I'm going to come
over here to Build Settings.
| | 04:33 | I'm going to make sure that All and
Levels is selected, and I'm going to scroll
| | 04:37 | down here, and it's quite always
down; it's about halfway down.
| | 04:40 | You're going to find a
section that says Search Paths.
| | 04:44 | Here we have Header Search Paths, and
that's where we're going to want to put in
| | 04:49 | the search path where the compiler
can actually find that header file.
| | 04:52 | Now, in order to do that, I need
to know where the search path is,
| | 04:56 | so I'm going to click on the folder
that contains the .h file, and I'm going to
| | 05:00 | come over here to full path, and I'm
just going to triple-click on that, and then
| | 05:05 | right-click or Ctrl+click,
and select Copy.
| | 05:06 | Now when I come back over here to my
target, and I'm just going to select it up
| | 05:11 | here, and I'm going to get rid of the
utility, so I have a little bit more room,
| | 05:15 | and I have Search Paths highlighted
there, so I'm going to click over here on our
| | 05:19 | project, and I'm going to click again.
So, I'm in the Project column, and I've
| | 05:23 | clicked twice with a little space in
between, so it highlights that up, and I'm
| | 05:27 | going to press Command+V on my keyboard,
and it will paste in that path that we
| | 05:31 | copied, and then Enter, and there it is.
| | 05:33 | If I double-click on this, you see it
all comes up there in the little pop-up.
| | 05:37 | So, I can come back out here to my
AppDelegate, and you'll notice that little
| | 05:41 | exclamation point will go away.
| | 05:42 | It takes it a moment, and that goes away,
and now it's finding that header file
| | 05:46 | successfully, so I can write some code
that uses that function; that isPrime
| | 05:51 | function. So, I'm just going to
drop in a little bit of code here.
| | 05:55 | I'm going to put in a little for loop,
and create a long variable; long n = 0, and
| | 06:00 | the condition is, while n is less than a
100, and the increment is ++n, and then I
| | 06:07 | tab over here to statements,
and I have an If.
| | 06:10 | I'm going to check the condition, and
I'm going to say if (isPrime(n)) and
| | 06:15 | then tab over to statements, and I'm going to
put in a message string: self messageString.
| | 06:19 | So, I'm using the Objective-C function
that's declared right here in this code,
| | 06:25 | this messageString, just like up here.
And bracket NString, and stringWithFormat,
| | 06:31 | and @" Here I'm going to put in a %ld
for the long decimal, for the long
| | 06:37 | integer, and \n for a new line,
and end of line, and semicolon.
| | 06:44 | I need to come back in here after that
quote, and put in a comma, and n, so
| | 06:48 | that it knows what it's
printing that long integer of.
| | 06:52 | There we have our line of code.
| | 06:53 | I'm going to press Command+S to save.
| | 06:55 | So, what this will do is this will loop
through 100 iterations of n, starting at
| | 06:59 | zero, and ending at 99.
| | 07:00 | It will check each one to see if it's
prime, and if it is prime, it's going to
| | 07:04 | go ahead and print it.
| | 07:06 | So, I'm going to save, Command+S, and
Command+R to build and run, and there we
| | 07:11 | have our prime numbers: Klaatu, barada,
nikto, and a series of prime numbers, all
| | 07:16 | the way up to 97, which is the highest
prime number under 100. So that's great!
| | 07:21 | We now have this in our OS X code, and we
want to put it in the iOS code as well,
| | 07:26 | because it will work just as well in
both, because it's just a little C function.
| | 07:30 | So, I'm going to press
Command+Q to get out of the OS X.
| | 07:34 | I'm going to come up here to our
TestBedIOS, and do exactly the same thing.
| | 07:38 | So, I come into the target, and I
select Build Phases, and I come into Compile
| | 07:43 | Sources, and I Add > Add Other, ExerciseFiles >
isPrime-working > isPrime.m, and select Open.
| | 07:52 | Make sure this is unchecked, press
Finish, and now we have our isPrime.m in
| | 07:58 | our Compile Sources.
| | 08:00 | Now I want to come over to Build Settings,
and scroll down on ways, and under Search
| | 08:04 | Paths > Header Search Path, highlight that.
I'm going to bring up my utilities, and get
| | 08:11 | the path of this folder here. Triple-click,
Command+C to copy, and select my
| | 08:17 | target again. Get rid of that utility
pane, because I don't have room for it on
| | 08:22 | this screen, and I'm going to click on
this, and pause, and click again, and paste it
| | 08:26 | in with Command+V, press Enter, and then
I'm going to come over here to our view
| | 08:30 | controller in this one.
| | 08:32 | I'm going to #Import; import our header
isPrime.h, and you'll notice that I don't
| | 08:38 | get the exclamation point,
because it is finding it.
| | 08:41 | Then I'm going to take a shortcut here.
| | 08:43 | I'm just going to come down here into
our other code, and I'm going to take this
| | 08:47 | whole loop, because this will all work
just like it is in our view controller
| | 08:51 | over here, and under Run Test, I'm just
going to come right here, and paste it in
| | 08:55 | with Command+V, and I'm going to save.
So, I'm going to select the TestBedIOS
| | 08:59 | with the 6.1 Simulator, and I'm going
to build and run, pressing Command+R. No
| | 09:05 | issues, and there it is.
| | 09:07 | There's our prime numbers in our iOS app.
| | 09:10 | So here, we are sharing a piece of code,
and in fact, if I come back out here --
| | 09:15 | I'm just going to stop this from
running by selecting the project, and
| | 09:19 | Command+Period. Finished running.
| | 09:21 | Now I'm going to come out to our
prime.m, and I'm just going change it.
| | 09:25 | I'm just going to say always return
true, and save that, and then I'm going to
| | 09:30 | come back up here to our iOS in our
view controller, and I'm just going to --
| | 09:34 | we've got it selected in the scheme, so
I'm just going to press Command+R again,
| | 09:38 | and it will build and run.
| | 09:40 | You notice now it's
giving us all numbers.
| | 09:44 | It's telling us that all numbers are
prime, because I changed that code, and come
| | 09:48 | back out here, Command+Period
| | 09:50 | to stop that from running, and then
I'm going to just come over here to our
| | 09:54 | TestBedOSX, and Command+R to build and
run that, and we have the same result.
| | 09:59 | It's all 100 numbers. Command+Q to quit
that. Come back out here to our isPrime.
| | 10:05 | In fact, I can change this and any of these,
and take that parent line out, and save it.
| | 10:10 | You see that they all say they're
unsaved in all three of those places.
| | 10:14 | Command+S to save, and now when I build
again here, I just have the prime numbers again.
| | 10:20 | Command+Q to quit that, and then I
come out here to our iOS, and select the
| | 10:26 | simulator, and Command+R to build and run,
and there we have our prime numbers again.
| | 10:32 | So, we have a piece of code that's being
shared between these projects, and Xcode
| | 10:36 | knows when to rebuild it, and it's all
linking in fine, and it's the same code
| | 10:40 | working on both an OS X app
and an iOS app.
| | 10:44 | So, in my mind, Xcode's
Workspace feature seems incomplete.
| | 10:47 | It lacks easy support for easily referencing
resources between projects, and even
| | 10:52 | referring to files outside of a given
project is tedious, but it is useful for
| | 10:57 | some things; it can be convenient for
working on more than one project at a time,
| | 11:01 | and of course, it can be
finagled into making this work.
| | 11:04 | For another useful example of
an Xcode workspace, see the Xcode
| | 11:08 | Installation Lesson in my C and C++
Essential Training title here on the
| | 11:12 | lynda.Com Online Training Library.
| | Collapse this transcript |
|
|
9. Static Analysis ToolsUnderstanding Fix-it| 00:01 | The term static code
analysis is fairly broad and vague,
| | 00:05 | but in the context of Xcode,
it refers to two specific tools.
| | 00:09 | Fix-it is a feature that detects common
and simple potential errors as you type,
| | 00:14 | and offers suggestions
to fix them for you.
| | 00:17 | Analyze is a compile-time feature
that performs deep analysis on your code
| | 00:22 | looking for uninitialized variables,
null pointers, memory leaks, unused
| | 00:26 | variables, and API-usage flaws.
| | 00:29 | These things tend to be legal code that
would nevertheless cause bugs and crashes.
| | 00:34 | Both Fix-it and Analyze make use of
features exclusive to the LLVM compiler suite.
| | 00:40 | These features are not available for
projects that use the legacy GCC compiler.
| | Collapse this transcript |
| Correcting common errors with Fix-it| 00:00 | The Fix-it feature works as you
type to find common errors, and offer
| | 00:05 | suggestions for fixing them.
| | 00:06 | Here, I've opened a working copy of
TestBedOSX here in Xcode, and I'm going to go
| | 00:11 | ahead and enter some code that
will trigger the Fix-it feature.
| | 00:16 | I'll define an integer, and now I'm going
to refer to that integer, but I'm going
| | 00:21 | to type it a little bit wrong.
| | 00:23 | I'm going to give it a lowercase L in
the middle instead of that capital L.
| | 00:27 | Now, I typed the lowercase L; it's
showing the capital L, and it's giving me the
| | 00:32 | tooltip, the code completion for that,
| | 00:34 | but when I press Tab at this point -- I
didn't mean to press Tab. Tab was the
| | 00:39 | wrong thing to press, someLabel, and
I'm just going to move my cursor instead
| | 00:45 | with the arrow key, and you notice that
it left the lowercase L. So, I'm just
| | 00:51 | going to put in a little bit of code here,
so that it's legal, and you'll notice --
| | 00:55 | I haven't pressed save yet --
| | 00:56 | You'll notice I've got
this red circle over here.
| | 00:58 | It's actually a hexagon, and
it's got a little white dot in it.
| | 01:02 | If I click on that, it says Use of
undeclared identifier 'somelabel' because the
| | 01:06 | lowercase L is not the same as the
uppercase L, and you notice that it has this
| | 01:11 | Fix-it Replace 'somelabel with someLabel'.
| | 01:14 | Fix-it is actually not
very good at finding these,
| | 01:17 | but when it does, it will
give you this suggestion.
| | 01:20 | Capitalization is one of the things that
it can find easily, so it's saying, did
| | 01:24 | you mean someLabel,
with a capital L?
| | 01:27 | It's got that here as well.
| | 01:29 | If you do, then you can press Return,
or you can double-click on it with the
| | 01:33 | mouse, and it will correct it,
and give you the correct label.
| | 01:36 | That fixes it, and I can
save this, and I can run it.
| | 01:40 | I'm going to press Command+R to run it, and
you see it works just fine. There is our 25.
| | 01:44 | So, I'll press Command+Q to quit that,
and I'm going to give it a different kind
| | 01:49 | of a bug at this point.
| | 01:50 | I'm going to say, If someLabel is
equal to 25, which ought to be true, except
| | 01:55 | you'll notice I used a single
equals sign instead of a double. This is
| | 01:59 | perfectly legal C, and if I save
this, and build it and run it, it will
| | 02:04 | just run just fine. And there it is.
| | 02:07 | We have our 25.
| | 02:07 | So, we might not think there is anything
wrong with our code, and we might go along,
| | 02:11 | happily coding along until some
condition happens that that actually breaks.
| | 02:16 | I'm going to press Command+Q
to quit the TestBed there.
| | 02:19 | So, you'll notice we have the amber or
yellow instead of the red, and it's still
| | 02:24 | got the little white circle
in the middle of it.
| | 02:26 | If I click on that, using the result
of an assignment as a condition without
| | 02:31 | parentheses. I'm going to go ahead
and close the organizer, so we can see
| | 02:35 | that whole thing there.
| | 02:36 | So, that's actually a pretty common
error, and it may be that you intended an
| | 02:42 | assignment, because it's actually legal
code, because any expression is actually
| | 02:46 | legal in that context.
| | 02:47 | So, it's traditional to put extra
parentheses around an assignment when it's
| | 02:52 | used as a condition, and that will tell
Fix-it that it's intentional, and in fact,
| | 02:56 | even in the older GCC compiler, putting
those double parentheses around it will
| | 03:01 | make it not give you a warning. Otherwise,
it would give you a warning for this,
| | 03:04 | because it's such a common error. Or
you can use the double equals to turn it
| | 03:09 | into an quality comparison, which
is actually what we meant here.
| | 03:12 | So, I can use my arrow keys to go up and
down here, and you see that it shows the
| | 03:16 | change in the code, and I can press
the Enter key, and that will correct it.
| | 03:22 | Now it's actually testing that,
| | 03:25 | and if I change the value here to 24,
then the condition won't be true, and we
| | 03:29 | won't get our output message turning
up 25. So, I'll run that, and you see that
| | 03:33 | that's what we expect.
| | 03:35 | Pressing Command+Q to quit, and making
this 25 again, save, and Command+R to
| | 03:40 | build and run, and there we
have the 25 that we expected.
| | 03:44 | So, I'm going to press Command+Q to quit.
| | 03:46 | And so that's how Fix-it works.
| | 03:48 | There are a number of different
conditions that will trigger this.
| | 03:50 | They are mostly very simple errors, but
there are kinds of things that can
| | 03:54 | be a little bit insidious, and give you
bugs that would take a while to find.
| | 03:58 | So, Fix-it is extremely helpful,
| | 03:59 | and you don't really have to do anything
but use LLVM in order for it to work.
| | 04:04 | It watches your code as you type,
| | 04:06 | and when it detects something, it gives
you a handy red dot, or an amber triangle,
| | 04:10 | and offers good suggestions to fix it.
| | Collapse this transcript |
| Catching common errors with Analyze| 00:00 | The LLVM Static Analyzer is
tremendously powerful and useful.
| | 00:05 | It performs deep analysis on your code
looking for uninitialized variables, null
| | 00:10 | pointers, memory leaks, unused
variables, and API usage flaws.
| | 00:15 | Here I have a working copy of
TestBedOSX open in Xcode, and I'm going to
| | 00:20 | come down in here to runTest, and
I'm just going to put in some code to
| | 00:25 | cause some trouble.
| | 00:26 | I am going to come down here, and put in
some more trouble-making code, and I'll
| | 00:31 | go ahead and press Command+S
to save this.
| | 00:34 | This is all perfectly legal code,
and it does a number of things wrong.
| | 00:39 | First of all, here on line 26, we have
an integer that is not being initialized.
| | 00:44 | That is legal, but a generally bad idea.
And here we have a flag that is set to
| | 00:50 | zero, and I'm testing for that flag.
| | 00:52 | And if that flag is true, I go ahead
and I assign a value to that otherwise
| | 00:57 | uninitialized variable.
| | 00:59 | So, I don't want to make it tough for
to compiler; even throw a warning.
| | 01:03 | Now I create a new variable, and I
assign that uninitialized variable to it.
| | 01:08 | Now I declare a null pointer, and I finally
give a value to x, and I assign x to y,
| | 01:15 | and then I assign x to what is
pointed out by this null pointer.
| | 01:20 | And so here's a whole lot of bad stuff.
| | 01:23 | So, I've saved with Command+S, and now
I'm going to come up here to the Product
| | 01:27 | menu, and I'm going to run Analyze.
| | 01:29 | I could also press Shift+Command+B if I
choose, and it analyzes it, and you notice
| | 01:35 | it found three errors.
| | 01:37 | So, if I click on that, we get the
issue navigator over here on the left, and
| | 01:42 | we can see our issues.
| | 01:43 | We have a logic error,
| | 01:45 | and if I click on that,
there is our logic error.
| | 01:49 | Value stored to y during its
initialization is never red.
| | 01:53 | Assigned value is garbage or undefined.
| | 01:55 | Now if I click on this, you'll notice
that the display changes, and I've got
| | 02:00 | some arrows that show me the path through the
code wherein that value could be undefined.
| | 02:07 | So, this is really interesting.
| | 02:09 | It ran through the code with different
conditions, and it noticed that if F was
| | 02:14 | false, and it generally will be, that X
never gets a value assigned to it, and it
| | 02:20 | goes through the code some more, and it
gets to this assignment, and it says, oh,
| | 02:23 | that's a potential problem right there.
| | 02:26 | The assigned value is garbage or undefined.
| | 02:28 | Up here at the top, we get a little jump
bar that gives us these two errors that
| | 02:34 | are part of this one problem, and
we can highlight them individually.
| | 02:38 | If we come down here to
the next one, dead store,
| | 02:40 | the value stored to Y during
its initialization is never used.
| | 02:44 | So, in other words, this value, there
is something assigned to it even though
| | 02:48 | the assigned value is
garbage or undefined.
| | 02:51 | If I click on this, see,
we get a little arrow here.
| | 02:54 | It says there is the problem.
| | 02:55 | And down here where Y is assigned again,
in between there, it's never been used.
| | 03:00 | So, what it's saying is you might not
have meant to do this, because it doesn't
| | 03:05 | actually do anything useful.
| | 03:06 | If I click on the last one, we have another
instance of value stored to Y is never red.
| | 03:13 | Now, you'll notice, in this case, that
my assignment to a null variable is not
| | 03:19 | caught, so it's just important to know
that you can't get too lazy, because even
| | 03:23 | Analyze is not going to find all of
your errors. You know, here, we clearly
| | 03:28 | have a null pointer, and we're trying
to assign something to it, and that will
| | 03:32 | crash your program.
| | 03:34 | So, let's create some more trouble here.
| | 03:36 | I'm going to declare a new method.
| | 03:38 | I'm just going to call it foo, because I
don't have anything better to call it.
| | 03:45 | So again, we have perfectly legal
code here, but you'll notice that it's
| | 03:50 | going to return an undefined value,
because we assigned zero to x, and we test
| | 03:56 | if x is less than zero, which it's not, and we
test if x is greater than zero, and it's not,
| | 04:01 | and those are the only cases
where temp ever get initialized.
| | 04:04 | So again, I'm going to run the analyzer;
Product > Analyze, and here we have
| | 04:11 | this undefined or garbage value
returned to caller, and if I click on that,
| | 04:16 | again, we get this lovely little diagram of
the circumstances under which this could happen.
| | 04:22 | So, just one more thing about Analyze;
if you want to run an Analyze automatically
| | 04:27 | every time you compile, you
can set it up to do that.
| | 04:30 | Keep in mind that Analyze uses a lot
more resources than the compiler, even on
| | 04:34 | the highest optimization settings,
because what it is doing is it's actually
| | 04:38 | running through the code under a
multitude of different circumstances, so it's
| | 04:42 | like it's compiling it over and over again.
| | 04:44 | So, do this if you like, but just
know that it's very inefficient.
| | 04:48 | We can come up here to do the target, and
go into Build Settings, and down here in
| | 04:53 | Build Options, you see Run Static
Analyzer, and if we set this, we can set it to
| | 04:59 | Yes, and now every time we build, the
analyzer will be run before the compiler.
| | 05:05 | Analyze is very powerful
and very useful.
| | 05:07 | I strongly recommend running your code
through Analyze often; at the very least,
| | 05:12 | for one last pass before putting your
code into a testing or a release phase.
| | Collapse this transcript |
|
|
10. The iOS SimulatorUsing the iOS Simulator| 00:00 | The iOS simulator is capable of
running your iPhone and iPad applications
| | 00:05 | on your Mac desktop.
| | 00:06 | Here I've opened a working copy of
BWRSS here in Xcode, and I want to run the
| | 00:13 | simulator, but I'm going
to do it in a different way.
| | 00:16 | I'm going to come up here to the Xcode
menu, and I'm going to show you how to run
| | 00:21 | the simulator without
actually running your code in it.
| | 00:23 | From the Xcode menu, you can come
to Open Developer Tool, and select iOS
| | 00:26 | Simulator, and that'll just fire up the
simulator in whatever mode it was in last.
| | 00:30 | And here, it's in iPad mode.
| | 00:32 | Yours will probably be
different, but that's okay.
| | 00:34 | You can select a device here, and I'm
just going to select a plain old iPhone,
| | 00:39 | and there we have it.
| | 00:40 | And you'll notice, this is
just like a regular iOS device.
| | 00:45 | It's got all of the different things
that we've run on it throughout the course.
| | 00:50 | It's got Settings, it's got photos;
| | 00:53 | it's just a regular old iPhone type
device, except it's running on our desktop,
| | 00:59 | so it's not a phone.
| | 00:59 | It doesn't have a physical hardware to it.
| | 01:02 | Of course, the other way, and the more
common way to run the simulator, is to come
| | 01:06 | back in here to Xcode, and select the
simulator that you want from the Schemes
| | 01:12 | menu, and here we'll leave it at iPhone 6.1
Simulator, and then either press the
| | 01:17 | build and run button, or
press Command+R on your keyboard,
| | 01:20 | and this will build you application,
targeting the simulator, and load it up in
| | 01:26 | the simulator, and run it.
| | 01:28 | Likewise, I'm going to come back
here to Xcode, and press Command+Period
| | 01:31 | to stop the execution of
our program in the simulator.
| | 01:34 | I can select the iPad 6.1 Simulator instead,
and this will run it on an iPad-like device.
| | 01:41 | Press that build and run button again.
| | 01:44 | We're actually running the simulator.
If I scroll up here to the top, you can
| | 01:48 | see the top of it, and I scroll down
here to the bottom, and I can select feeds,
| | 01:52 | and I can select a feed, and this is all
running as we expect, or I can scale it.
| | 01:58 | I can scale it down to 50%, and then we
can see the simulator on the screen, but
| | 02:03 | it's just scaled down,
and it's a little blurry.
| | 02:05 | So, I'm going to quit
the simulator for now.
| | 02:08 | Actually, I'm going to switch it
back, for my purposes, to the iPhone, and
| | 02:13 | scale it back to a 100%.
| | 02:14 | It'll remember that
setting the next time it's run.
| | 02:18 | I'm going to quit that, and I'm going
to show you the one final way that you
| | 02:23 | can run the simulator.
| | 02:24 | So, I'm going to bring up the Finder
here, and we're going to actually find the
| | 02:28 | Simulator app, and drag it in to our dock.
| | 02:31 | So, if we go in here to Applications,
and scroll all the way down until we see
| | 02:35 | Xcode, you see Xcode.app, as
all apps, is actually a bundle.
| | 02:38 | And if I right-click or Ctrl+click on
this, I can actually show package contents,
| | 02:45 | and it opens up the inside of the bundle,
the contents of it, and I can navigate
| | 02:50 | around inside the bundle.
| | 02:51 | If I come over here to Developer, and
Platforms, and iPhoneSimulator.platform, and
| | 02:59 | again, Developer, and Applications, and
there's our iPhone iOS Simulator.app.
| | 03:05 | I can grab that, and I can actually drag it
down here into the dock, and I can close this.
| | 03:11 | Now, whenever I want to run the simulator,
just as if I had come into Xcode, and
| | 03:16 | gone into Open Developer Tool > iOS
Simulator, I can just come down here to my
| | 03:20 | dock, and I can click on the
Simulator, and there it is.
| | 03:23 | So, this can be handy for things like
testing a Web site in the simulator.
| | 03:28 | So, there are a couple of important
distinctions that make the simulator
| | 03:31 | different from an actual device.
| | 03:33 | An actual iOS device uses a different
processor than your desktop computer.
| | 03:38 | Your Mac uses an Intel processor,
where your device uses an ARM processor.
| | 03:43 | This means that code compiled for the
device is different than code compiled for
| | 03:47 | the simulator, and vice versa.
| | 03:49 | In most circumstances, this distinction
doesn't really matter, but in some
| | 03:54 | circumstances, especially when things
aren't working as they should anyway,
| | 03:58 | this means the code will actually
behave differently on the simulator than it
| | 04:03 | would on the device.
| | 04:04 | Usually, this manifesta as bugs that
behave differently on the simulator than
| | 04:08 | on the device, or maybe bugs that show up on
the device, and don't show up in the simulator.
| | 04:13 | What that means is, most of the time,
when you see a difference between the
| | 04:17 | device and the simulator, not all the
time, but most of the time, when you see a
| | 04:21 | difference in how things run,
that means there's a bug to be found.
| | 04:25 | The simulator has other limitations.
| | 04:26 | It has no camera, no gyroscope, push
notifications don't work, the file system
| | 04:30 | works a little bit differently,
and it has other differences.
| | 04:33 | But for the most part, it's an important
tool, and it works well, and even without
| | 04:38 | these limitations, it's always
important to test your app on a real device.
| | 04:42 | In the next movie, I'll show you some
of the features of the simulator as we
| | 04:46 | explore the simulator interface.
| | Collapse this transcript |
| Exploring the iOS Simulator interface| 00:00 | The iOS Simulator is used to
test your code on the desktop.
| | 00:04 | There are some circumstances where
you do need to test on a real device,
| | 00:08 | but for many purposes, running your
code in the simulator will be an important
| | 00:11 | part of the development cycle.
| | 00:13 | Here I've opened a working copy of BWRSS
here in Xcode, and I'm going to go ahead
| | 00:19 | and run this in the iPhone simulator,
so I'll select iPhone 6.1 Simulator from
| | 00:24 | Scheme, and I'm going to press Command+R
on my keyboard to build and run, and this
| | 00:30 | will fire up the
simulator, and run our code in it.
| | 00:33 | So, there's the simulator, and you
see it works just like an iOS device.
| | 00:40 | Let's take a look
around the interface here.
| | 00:42 | If I go into the Hardware menu, you see
there's a Device menu, and I can select
| | 00:48 | from a number of different devices.
| | 00:50 | I get iPhone, regular old
iPhone, which is what I've got here.
| | 00:53 | I've got a Retina 3.5-inc,h or Retina
4-inch inch, a regular iPad, which would
| | 00:59 | include the iPad mini, or a Retina
iPad, and I can select any of those.
| | 01:04 | If I select a different device right
now; for example, if I select this 4-inch
| | 01:08 | Retina device, you'll notice that it
starts up a new device, and this one is
| | 01:13 | actually way too big
for this little screen.
| | 01:15 | So, I have to scroll to see
the whole thing, or scale it.
| | 01:18 | You'll also notice that my code is no longer
running, because it actually had to shut
| | 01:23 | down the code in order to
start up this different device.
| | 01:27 | So, if you do want to change the device,
you're going to have to come back into
| | 01:32 | Xcode. You'll notice it says Finished
Running, and run it again, and now it'll
| | 01:36 | load up the simulator with the device
that you selected, and it'll have the
| | 01:40 | code running in it.
| | 01:41 | You'll notice here there's also a
Version menu, and this will select different
| | 01:45 | versions of iOS, and that's actually best
selected from the Scheme here in Xcode.
| | 01:51 | So, for example, if I wanted to run it on an iPhone
running iOS 5.1, I'm going to press Command+Period
| | 01:57 | here to stop it from running, and press
Run again, and now it'll load it up in
| | 02:02 | that 4-inch Retina device with iOS 5.1
on it instead of iOS 6.0, and there it is.
| | 02:11 | And actually, this is the 3.5-inch
Retina device, because iOS 5.1 doesn't
| | 02:15 | support the 4-inch device.
| | 02:16 | So, I'm just going to come back over
here, and I'm going to select the iPhone 6.1
| | 02:22 | again, and press Command+Period,
| | 02:24 | and run; Command+R. Now, of course,
you've also noticed that this device here
| | 02:30 | doesn't actually -- I'm scrolling
with my mouse --
| | 02:33 | this actually doesn't fit entirely, so
one thing that I can do is I can come here
| | 02:38 | to the Window menu, and I can select
Scale, and I can scale it down 50% or 75%.
| | 02:42 | I'm going to try 75% to
see if it fits.
| | 02:46 | It doesn't quite fit,
| | 02:47 | so I'll come down; 50%, and you notice
that it doesn't have to actually stop the
| | 02:52 | application, because it's
not changing the device.
| | 02:55 | It's just scaling it down
in software here on the Mac.
| | 03:00 | If I want to take a screen shot of
the device, and this is, of course, very
| | 03:04 | useful for submitting your device to
the app store, there are two different
| | 03:07 | ways I can do that.
| | 03:09 | I can come over here to the File menu,
and I can select Save Screen Shot, and
| | 03:13 | that'll save it to the Desktop. So, come
over here into the Finder, and I'll come
| | 03:17 | to the Desktop, and there is our screen
shot. Or, I can take a screen shot just to
| | 03:26 | the clipboard, and that's under the
Edit menu, and it's Copy Screen, or
| | 03:30 | Ctrl+Command+C. And so now that's on the
clipboard, so I can bring up preview, and
| | 03:36 | select New from Clipboard, and there it
is from the Clipboard. A quick preview
| | 03:42 | with Command+Q, and no, no thank you.
| | 03:45 | I'm not going to save it.
| | 03:47 | So, there are a few other
interesting things here.
| | 03:49 | I can rotate the device by pressing
Command+left arrow or Command+right arrow,
| | 03:55 | and that'll rotate the device.
| | 03:56 | I can make it upside down, which you
see the app doesn't support upside down.
| | 04:01 | I have a Home button, which is Shift+
Command+H, and that's also available here
| | 04:05 | from the Hardware menu.
| | 04:07 | You see we have Rotate Left,
Rotate Right, there is the Home button.
| | 04:10 | I can also lock the device with Command+L
and that gives us our lock screen, and
| | 04:15 | so I can slide to unlock,
just like I would on my phone.
| | 04:19 | And our app is still running.
| | 04:20 | I can press Shift+Command+H twice, and I
can see our app is still running, and you
| | 04:25 | see here in Xcode it still says running,
or just like on a real device, I can
| | 04:30 | press the Home button twice
with Shift+Command+H twice.
| | 04:33 | I have to do it a little bit more rapidly.
And I can hold down on our app, and it
| | 04:38 | jiggles, and gives me the little Kill
button, and that kills the device, and you
| | 04:42 | can see it got a signal SIGKILL, and Xcode
is still running it, but in its killed state.
| | 04:49 | I am going to press the
Command+Period (Command+.).
| | 04:52 | Notice I have this message here,
Simulator slow-motion animations are now on.
| | 04:56 | I probably hit that button by
mistake when I was trying to hit something
| | 05:00 | else, That does happen.
| | 05:01 | So, if I run this in the simulator again,
you'll see if I -- oh, they're not doing
| | 05:05 | the slow animations.
| | 05:06 | Slow animation is one of the features here.
| | 05:08 | We'll get to this when we talk about
hardware, but I'll just show you now,
| | 05:12 | because I mentioned it. Toggle Slow
Animations; now when I do the rotation, you
| | 05:15 | see the animations,
they are all very slow.
| | 05:18 | This is useful for developing games,
and things like that, and any circumstance
| | 05:22 | where you want to see the animations.
| | 05:24 | The iOS simulator can also simulate a
number of device hardware events, and we'll
| | 05:28 | cover these in the next movie.
| | Collapse this transcript |
| Simulating hardware events| 00:00 | The iOS simulator is used to
test your code on the desktop.
| | 00:04 | In addition to running your iOS apps,
it's also capable of simulating a number
| | 00:08 | of device hardware events.
| | 00:10 | Let's see how that's done.
| | 00:12 | I've opened a working copy of BWRSS here
in Xcode, and I'm going to go ahead and
| | 00:18 | run this in the iPhone 6.1 Simulator.
| | 00:20 | Press the build and run button, and this will
fire up the simulator, and load up our code in it.
| | 00:27 | You'll notice when I press the add
feed button here, I get the add feed modal
| | 00:32 | screen, and it gives me a place to type,
and I can type by pressing these buttons,
| | 00:36 | and that's kind of tedious.
| | 00:38 | I can actually type on my keyboard,
and that's a lot more convenient.
| | 00:44 | Or -- I'm pressing backspace here --
| | 00:47 | I can come up here to the Hardware
menu, and I can say, Simulate Hardware
| | 00:50 | Keyboard, and this simulates a condition
where a hardware keyboard is attached to
| | 00:55 | the device, usually with Bluetooth.
| | 00:58 | In that case, you actually don't get
that keyboard screen at all, and you'll
| | 01:02 | type on your Bluetooth keyboard.
| | 01:04 | So, it's important to be able to
simulate that, so you can see what your app
| | 01:08 | actually looks like
under those conditions.
| | 01:10 | I'm going to go ahead
and add a feed here.
| | 01:13 | This is my little test feed. and I can
press Add here, or I can just press Return
| | 01:17 | on my keyboard, and it will have the
same effect, and there is my test feed.
| | 01:22 | Now, I can simulate a memory warning,
and I can come up at the Hardware menu, and
| | 01:28 | I can press Simulate Memory Warning, and
you'll notice that here in Xcode, I get
| | 01:33 | a little message here in the log
that says Received memory warning.
| | 01:36 | And so that's useful for testing
the parts of your code that deal with
| | 01:40 | memory warnings in iOS.
| | 01:42 | I can also simulate a shake gesture, and I'm
running this right now in the 6.1 Simulator.
| | 01:48 | This code, actually, if I bring up one
of the feeds, and I do the little drag to
| | 01:52 | refresh, and I can do that in my test
feed, and we can see what that looks like.
| | 01:57 | In my test feed, every time I refresh it,
it gives me a random new set of items, so
| | 02:02 | there we get five more,
and we get five more.
| | 02:05 | But in earlier versions of iOS, where
that pull to refresh gesture wasn't
| | 02:10 | available, I had it set up to shake to
refresh, so I can simulate that shake
| | 02:15 | gesture,.I'm going to come back here
into my code, and I'm going to press
| | 02:20 | Command+Period to stop it from running.
| | 02:22 | Go ahead and drop down this debug view,
and I'm going to run it on the iPhone 5.1
| | 02:28 | Simulator, and we'll press the build and
run button there, and this will build it
| | 02:33 | for iOS 5.1, and it will
run in that simulator.
| | 02:37 | Now, this is iOS 5.1, and it doesn't
have that pull to refresh feature.
| | 02:41 | It also doesn't have my test feed in it,
so I'm going to go ahead and add that;
| | 02:45 | ios.bw.org/testfeed, and press Add.
| | 02:50 | And I'm going to press test feed
here, and there is our five items.
| | 02:55 | Now, we don't have that pull to
refresh feature here in iOS 5.1; instead, we
| | 03:00 | have a shake to refresh, and so I can
simulate shaking, the shake gesture,
| | 03:06 | Ctrl+Command+Z, and you see our little busy
indicator came up, and it loaded five more items.
| | 03:13 | So, I'm going to press Ctrl+Command+Z on
my keyboard, and it'll do it again; there
| | 03:18 | it is, and one more time.
| | 03:21 | So, that's simulating the shake gesture.
| | 03:23 | It doesn't actually shake on
the screen. That'd will be cool, huh?
| | 03:26 | There's another feature here called Slow
Animations, and it's under he Debug menu.
| | 03:30 | And once Slow Animation is on, you see
we get a little message there in the log
| | 03:34 | that slow animations are on.
| | 03:36 | And now, when I do anything that animates
the display, like for instance, I'll
| | 03:40 | press the back button there, and you
see it goes very slowly, so you can see
| | 03:44 | what these animations look like, and there are
circumstances where that would be important.
| | 03:47 | I'm going to rotate it to the right, and
we see it's very interesting to see how
| | 03:52 | these animations work.
| | 03:53 | Rotate it back to the left,
and that's what it looks like.
| | 03:57 | If we come back and turn that off again,
you'll see that we got our log message,
| | 04:01 | and the animations
are now at full speed.
| | 04:04 | I want to show you, also, the Location
menu, and that's under Debug, and that's for
| | 04:09 | simulating locations, for
location-aware applications.
| | 04:13 | This application doesn't actually have
location awareness, but if I press the
| | 04:17 | Home button, and bring up the
browser, and I'm just going to type in
| | 04:21 | maps.google.com. The simulator doesn't
have the Maps app on it, and it says,
| | 04:28 | would you like to use
my current location?
| | 04:30 | I'm going to say, OK. I'm going to say
no, thanks, go to the mobile site, and
| | 04:34 | it'll ask to use my location
again. I guess that's polite of it.
| | 04:38 | And our location is set to somewhere
in San Francisco, apparently, but I can
| | 04:41 | simulate other locations, and
it's got a menu of them here.
| | 04:45 | I can go to Apple itself at One Infinite
Loop, and I press the Location button in
| | 04:50 | the Maps app, and there it is, or I can
pick a Custom Location, and it allows me
| | 04:56 | to put in Longitude and Latitude.
| | 04:58 | There is also moving
locations, which is kind fun.
| | 05:02 | I can simulate a Freeway Drive, and it gets
there on the freeway, and it starts moving.
| | 05:07 | See, it's getting on the freeway, and
I can drag it along here, because it's
| | 05:11 | going to go that way.
| | 05:12 | So, you can simulate in the hardware as
if the GPS was moving along, and this is
| | 05:18 | useful for any location-aware applications,
and you've got a couple of choices;
| | 05:23 | you've got a city bicycle ride, and we
find it there, and you can see its going the
| | 05:29 | speed as if we're riding on a bicycle.
| | 05:31 | There is running, and there's also
one that toggles between different
| | 05:36 | Apple Stores, apparently.
| | 05:38 | So, this doesn't look really great in the
map app there, but you can see it takes
| | 05:43 | us to different Apple
Stores around the country.
| | 05:45 | So, the iOS simulator is capable of
simulating a number of device hardware events.
| | 05:51 | It's sufficient for many purposes, although
not all. It has no camera, it has no
| | 05:55 | gyroscope, push notifications don't
work, file system is a little different.
| | 05:59 | It has other limitations,
| | 06:00 | but for the most part, it's an important
tool, and it works well. Even without
| | 06:05 | these limitations, it's always
important to test your app on a real device.
| | Collapse this transcript |
|
|
ConclusionGoodbye| 00:00 | Thank you for watching
Xcode 4 Essential Training.
| | 00:03 | I've really enjoyed putting this course
together, and I hope you've gained the
| | 00:07 | necessary skills to effectively use
this powerful development environment for
| | 00:11 | building your own OS X
and iOS applications.
| | 00:14 | I look forward to seeing your apps out
there on the OS X and iOS App Stores, so
| | 00:19 | have fun, and happy coding!
| | Collapse this transcript |
|
|