navigate site menu

Start learning with our library of video tutorials taught by experts. Get started

Xcode 4 Essential Training

Xcode 4 Essential Training

with Bill Weinman

 


This course covers the major features of Xcode, the powerful and well-designed integrated development environment (IDE) for developers of OS X and iOS apps. Author Bill Weinman explores the Xcode user interface; reviews the code editors, including version and assistant editors; and shows how to speed up your workflow with code snippets, keyboard shortcuts, and behaviors. The course also shows how to manage your changes with version control and snapshots, create a UI with Interface Builder and storyboards, compile and debug your projects, and perform statistical analysis with Fix-it and Analyze. The final chapter covers emulating hardware with the iOS Simulator.
Topics include:
  • What is Xcode?
  • Exploring the code editors
  • Adding Git version control to your project
  • Making interface connections to the code
  • Creating an iPhone or iPad storyboard interface
  • Understanding the LLVM compiler
  • Sharing resources in a workspace
  • Catching common errors with Analyze and Fix-It
  • Using the iOS Simulator

show more

author
Bill Weinman
subject
Developer, Mobile Apps, Desktop Apps
software
Xcode 4
level
Beginner
duration
4h 12m
released
Mar 28, 2013

Share this course

Ready to join? get started


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



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


Suggested courses to watch next:

Xcode 4 New Features (1h 58m)
Bill Weinman


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 104,069 instructional videos.

get started learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 2,024 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.

preview image of new course page

Try our new course pages

Explore our redesigned course pages, and tell us about your experience.

If you want to switch back to the old view, change your site preferences from the my account menu.

Try the new pages No, thanks

site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked