Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
Xcode Tools Sensei 3
Table of ContentsAcknowledgments 21
Introduction 22Getting Xcode 4 ...................... 22Accessing Other Developer Tools .............................23The Book’s Contents ................ 23Xcode Preferences ................... 24What the Reader Needs to Know .............................25Some Things to Keep in Mind as You Read This Book................ 25
Chapter1:XcodeProjects 26Creating a Project .................... 26
Step 1: Choose the Type of Project You Want to Create ....................... 26Step 2: Choose a Product Name .........................27Step 3: Save the Project.......................28The Project’s Contents .........................28Application Projects ............................29
Cocoa Projects ..............................29Document and Non-Document Applications................. 30Core Data Applications .................30Command-Line Tool Projects .........................30
Framework and Library Projects ..........................31Libraries ....................... 31Frameworks .................. 31Bundles and XPC Services .............................31
Application Plug-in Projects ................................32System Plug-in Projects .......................32Other Projects .................... 33iOS Application Projects .....................34
Devices ........................ 35Core Data ..................... 35Storyboarding ................................35
iOS Library Projects ............................35Project Window ...................... 36
Toolbar .............................. 36Navigator ........................... 37
Project Navigator ..........................38Project Navigator Groups ..............................38Symbol Navigator ..........................39Filtering the Symbol Navigator’s Contents ...................... 40Search Navigator ...........................40
4 Table of Contents
Customizing Your Search ..........................41Find Scopes .............................41Find and Replace .....................42
Issue Navigator ..............................43Debug Navigator ...........................43Breakpoint Navigator ....................43Log Navigator ................................43
Editor ................. 44Utility Area ........................ 44
File Inspector ................ 44Identity and Type .....................45Localization .............................45Target Membership ..................45Text Settings .............................46Source Control .........................46
Quick Help Inspector ....................46Library .......................... 46
Debug Area ....................... 47Adding Files and Frameworks to Your Project ........................... 47
Creating New Files for the Project .......................47Choosing a File Type .....................48Naming the File .............................48Mac File Types ...............................49
Cocoa ..................... 49C and C++ ...............................50User Interface ..........................50Core Data ............... 50Resource ................. 50Other ...................... 51
iOS File Types ................................51Cocoa Touch ...........................51C and C++ ...............................52User Interface ..........................53Core Data ............... 53Resource ................. 53Other ...................... 54
Fixing the Copyright Notice ................54Adding Files You’ve Already Created ...................55
Adding a Folder of Files .................56Adding Files to Targets ...................56
Adding Frameworks and Libraries to a Project .................... 56Source Trees ............................ 57Removing Files from a Project ..................57Renaming a Project ................. 58Modernizing a Project ..............................58
Xcode Tools Sensei 5
Workspaces ............................ 58Creating a Workspace .........................59Adding Projects to a Workspace .........................59
Organizer ................ 59Opening the Organizer .......................59Organizer for iOS Applications ...........................60
Developer Profile ..........................61Provisioning Profiles ......................61Software Images ............................61Device Logs ................. 62Screenshots .................. 62Devices ........................ 62
Chapter2:EditingSourceCode 64The Editor Pane ...................... 64
Jump Bar ............................ 64Editor and Gutter ............... 65Focus Ribbon ..................... 65
Assistant Editor ........................ 66Code Completion .................... 67Customizing Code Editing ........................67
Fonts and Colors Preferences ..............................68Color Themes ............... 68Categories .................... 68Setting Colors for Non-Text Items ..................68
Text Editing Preferences ......................68Indentation Preferences ......................69
Tabs.............................. 69Line Wrapping...............................70Syntax-Aware Indenting .................70
Key Bindings ...................... 70Code Snippets ......................... 71
Using a Code Snippet .........................71Creating a Code Snippet .....................72Completion Shortcuts .........................72Completion Scope ..............................72Placing Tokens in Your Code Snippets .................73Examining a Code Snippet ..................74
Tab Bar.................... 74Refactoring Tools ..................... 74
Converting Your Project to ARC ...........................75Converting to Modern Objective-C Syntax .......................... 76
Fix-it ....................... 77Reading Developer Documentation .........................77
6 Table of Contents
Browsing Documentation ...................78Searching Documentation ..................79Bookmarks ......................... 80Quick Help ........................ 80
Invoking Quick Help .....................80What Quick Help Displays ............................81
Updating Documentation ...................81Installing Third-Party Documentation ............................. 82Removing Documentation Sets ......................82
Chapter3:CreatingUserInterfacesforMacApplications 83Starting with Interface Builder ..................83Creating the User Interface .......................84Modifying the Interface ............................84
Selecting an Element ...........................85Selecting an Element in a Hierarchy ....................85Moving and Resizing Elements ...........................85Deleting an Element ...........................86Changing the Text of Titles and Labels .................86Making Other Modifications ...............................86
Making Connections ............... 86Testing the Interface ................ 87Creating a Xib File ................... 87Object List .............................. 87
File’s Owner ...................... 87First Responder .................. 88Application ........................ 88
Object Library ......................... 89Controls ............................ 89
Buttons ......................... 89Text Controls ................ 90Miscellaneous Controls .................90Formatters .................... 91
Data Views ........................ 92Cells .................. 93Layout Views ...................... 93Objects and Controllers ......................94Windows and Menus ..........................95
Windows ...................... 95Menus .......................... 96Toolbar ......................... 97
Address Book ..................... 97Automator ......................... 97Disc Recorder .................... 98
Xcode Tools Sensei 7
Image Kit ........................... 98OSAKit .............................. 99PDFKit ............................... 99QTKit ................. 99Quartz Composer ...............................99SceneKit ............................ 100WebKit .............................. 100Custom Objects ................. 100
Media Library .......................... 100Inspectors ................ 101
File Inspector ..................... 101Quick Help Inspector .........................102Identity Inspector ............... 102
Custom Class ................ 102Identity ......................... 102Tool Tip ........................ 103Accessibility Identity .....................103User Defined Runtime Attributes ...................103Document .................... 104
Attributes Inspector .............................104Size Inspector .................... 105
Sizing Controls ..............................105Setting an Element’s Size and Position ........................... 105Autosizing .................... 105
Springs .................... 106Struts ....................... 106
Aligning Elements ..........................106Positioning Items in a Containing View .......................... 107Sizing Windows ............................107
Connections Inspector ........................108Bindings Inspector ..............................108View Effects Inspector .........................109
Turning on Core Animation Effects ................109Appearance Section ......................109Content Filters ...............................109Background Filters .........................110Compositing Filters ........................110Transitions for Subviews ................110
Working with Menus ................................111Adding Menus to the Menu Bar ..........................111Adding Items to a Menu ......................111
Keyboard Equivalents ....................112Adding Submenus .........................112
Creating Contextual Menus .................112Creating Dock Menus .........................113
8 Table of Contents
Attaching Menus to Buttons ................113Bindings .................. 114
Creating the Model Class ....................114Creating the Controller .......................114Binding the Model to the Controller ....................115Binding the View to the Controller ......................115Value Transformers ..............................116
Connecting to Your Classes .......................116Grouping Objects ................... 118Creating a Matrix of Controls ...................118Setting Tab Order .................... 118Toolbars .................. 119
Adding a Toolbar ............... 119Image and Custom View Toolbar Items ................119Adding Items to the Toolbar ................119Adding Images and Labels to Toolbar Items ......................... 120
Split Views .............................. 120Adding and Removing Views ..............................121Arranging and Sizing Subviews ...........................121Embedding a Split View ......................121Dividers ............................. 121
Source Lists ............................. 122Auto Layout ............................. 122
Turning on Auto Layout .......................122Constraints ......................... 123
Editing Constraints .........................123Content Priorities ...........................124Adding Constraints ........................125
Chapter4:CreatingUserInterfacesforiOSApplications 126Starting with Interface Builder ..................126Creating the User Interface .......................127Modifying the Interface ............................128
Selecting an Element ...........................128Selecting an Element in a Hierarchy ....................128Moving and Resizing Elements ...........................128Deleting an Element ...........................128Changing the Text of Titles and Labels .................128Making Other Modifications ...............................129
Making Connections ............... 129Testing the User Interface .........................129Creating a Xib File ................... 130Object List .............................. 130
File’s Owner ...................... 130
Xcode Tools Sensei 9
First Responder .................. 131Object Library ......................... 131
Controls ............................. 131Data Views ........................ 132Gesture Recognizers ...........................133Objects and Controllers ......................133Windows and Bars ..............................133
Media Library .......................... 134Inspectors ................ 134
File Inspector ..................... 135Quick Help Inspector .........................135Identity Inspector ............... 136
Custom Class ................ 136Identity ......................... 136User Defined Runtime Attributes ...................136Document .................... 137Accessibility ................. 137
Attributes Inspector .............................138Size Inspector .................... 138
Setting an Element’s Size and Position ........................... 138Autosizing .................... 139
Springs .................... 139Struts ....................... 139
Aligning Elements ..........................140Positioning Items in a Containing View .......................... 140Sizing Windows ............................140
Connections Inspector ........................141Connecting to Your Classes .......................141Grouping Objects ................... 143Storyboarding .......................... 143
Creating a Storyboard .........................143The Storyboard Canvas .......................144Working with Scenes and Segues ........................144Creating a Table in Interface Builder ...................145
Auto Layout ............................. 145Turning on Auto Layout .......................145Constraints ......................... 146
Editing Constraints .........................146Content Priorities ...........................147Adding Constraints ........................147
10 Table of Contents
Chapter5:ModelingTools 149Data Models ........................... 149
Adding a Data Model File to Your Project ........................... 149XML Data Models ...............................150Data Model Editor ..............................150
Top-Level Components ..................151Detail Area ................... 151Bottom Area ................. 151Graph View .................. 152
Adding Entities ................... 152Adding Attributes ............... 153
Setting an Attribute’s Name and Data Type .................... 153Setting Additional Attribute Information ......................... 154
Adding Relationships ..........................154Adding Fetched Properties ..................156Adding Fetch Requests ........................156
Editing the Fetch Request’s Predicate with the Predicate Builder .... 156Data Model Inspector for Fetch Requests ....................... 157Advanced Checkboxes ..................158
Setting Information Dictionary Entries .................158Adding Configurations ........................159Versioning ......................... 159Advanced Checkboxes for Attributes and Relationships ...................... 160Synchronizing Data Models ................160
Syncing an Entity ...........................160Syncing an Attribute ......................161Syncing a Relationship ..................161
Creating Source Code .........................162Mapping Models ..................... 163
Versioned and Non-Versioned Data Models ....................... 163Adding a New Version of Your Data Model ......................... 163Adding a Mapping Model to Your Project ............................ 164Mapping Model Editor ........................165
Entity Mappings .............................165Property Mappings ........................166Changing Attribute Mapping Data .................166Changing Relationship Mapping Data ........................... 167Creating a User Dictionary ............................167
Migrating the Data ..............................167Enabling Automatic Migration .......................167Migrating a Document-Based Application ..................... 168Migrating a Regular Application ....................168
Xcode Tools Sensei 11
Chapter6:BuildingProjects 169Project Editor .......................... 169Targets..................... 169
Inspecting and Configuring Target Settings .......................... 170Summary ...................... 170
Mac Target Summary ................................170iOS Target Summary .................172Version and Build Numbers .....................174Adding an Icon to Your Application .......................... 174
Info .............................. 176Custom Target Properties ..........................176Document Types .....................177iOS Document Types ...............................177Exported and Imported UTIs ....................178URL Types ................................179Services .................. 180
Build Settings ............... 180Target Build Phases........................181
Target Dependencies................................181Adding Build Phases ................182Reordering Build Phases ..........................183
Build Rules ................... 183Adding Targets ................... 183
Aggregate Targets ...........................184Unit Testing Bundles ......................184
Adding a Target Dependency ...................184Configuring the Unit Test Bundle .............................. 185Adding Unit Testing Classes .....................185Writing and Running Unit Tests ................186
Project Settings ........................ 186Deployment Target ..............................187
What Should My Deployment Target Be? ....................... 187Deployment Targets and SDKs .......................188When Should You Use an Earlier SDK? .......................... 188
Build Configurations ...........................189Localizations ..................... 190Xcode Build Settings ...........................190
Architectures ................ 191Build Locations .............................193Build Options ................................193Picking a Compiler ........................194Code Signing ................ 194
Code Signing iOS Applications ................195Code Signing Mac Applications ............................... 195Creating a Code Signing Identity .............................. 196
12 Table of Contents
Code Signing Build Settings .....................196Deployment ................. 197
Deployment Target ...................197Targeted Device Family ............................197Stripping Symbols ....................197
Kernel Module ..............................198Linking ......................... 198Packaging ..................... 199Search Paths ................. 199Unit Testing .................. 199Versioning .................... 200Code Generation ...........................200
Optimization Level ..................201Generate Debug Symbols ........................201
Language ...................... 201Choosing the Language Compiler ............................ 201Choosing the Language Standard ............................. 202Enabling Exception Handling ...................202Setting Compiler Flags .............................202Enabling Objective-C Automatic Reference Counting .............. 202
Preprocessing ............... 203Warnings ...................... 203Data Model Version Compiler .......................204Interface Builder Compiler ............................204Static Analyzer ..............................204Conditional Build Settings .............................205Adding Your Own Build Settings ....................205
Configuration Settings Files ......................206Creating a Configuration Settings File .................206What Goes in a Configuration Settings File? ........................ 207Telling Your Project to Use a Configuration Settings File ...................... 208Overriding the Configuration Settings File ........................... 208
Compiling Your Program ..........................208Schemes ............................ 208
Choosing a Scheme .......................209Opening the Scheme Editor ...........................209
Build ....................... 210Run ......................... 210Test ......................... 211Profile ..................... 211Analyze .................. 211Archive ................... 212Pre and Post-Actions ................212
Adding and Managing Schemes ....................212Precompiled Headers .........................213
Xcode Tools Sensei 13
Cleaning Targets ................. 214Building Your Project ..........................214Where’s My Application? ....................215Seeing More Build Details ..................216
Message Bubbles ...........................216Opening the Build Results Window ............................... 216
Showing the Build Transcript ....................217Filtering the Build Results .........................217
Customizing Xcode Behaviors ............................218Tips for Correcting Build Errors ...........................218
Add All Necessary Frameworks .....................218Include Necessary Header Files .....................219The Error May Not Be Where Xcode Says It Is ................ 219One Error Can Cause Multiple Syntax Errors .................. 219Look for Typographical Errors ........................220Check Function Arguments ............................220
Building for Unsupported Languages ..................220Static Analysis .................... 221
Generating Output Files ...........................222Creating Applications that Run on iPhones and iPads ............................... 223
Creating a New Universal Project .......................223Upgrading an Existing iPhone Project .................223Universal Application Build Settings ...................223Creating Two Device-Specific Applications ......................... 224
Chapter7:Debugging 225Before You Debug ................... 225
Configuring Your Scheme for Debugging ............................. 225Info .............................. 225Arguments .................... 226Options ........................ 226Diagnostics .................. 227
Memory Management ..............................227Logging ................... 227Debugger ................ 228
Setting Environment Variables for Debugging ...................... 228Choosing a Debugging Format ............................230
Breakpoints ............................. 231Setting Breakpoints .............................231Breakpoint Actions .............................232
Debugger Command .....................233Log ............................... 233Sound ........................... 233Shell Command .............................233
14 Table of Contents
AppleScript .................. 234Capture OpenGL ES Frame ............................234
Sharing Breakpoints ................ 234Launching the Debugger ..........................234
Opening a Separate Console Window ................235Debug Bar ......................... 235Debug Navigator ............... 236Floating Debugger Window ................237Variables View ................... 237
Setting Watchpoints .......................238Custom Data Formatters ................239Datatips ........................ 240
Using Datatips .........................240Using Step Controls in the Editor .............................. 240
Viewing Shared Libraries ...............................241Tracking Expressions ......................241Viewing Dynamic Arrays ...............................242
Stepping Through Your Code ....................242Viewing Memory .................... 243OpenGL ES Debugging ............................245
Enabling OpenGL ES Frame Capture ...................245Capturing the Frame When Reaching a Breakpoint ............................. 245Capturing the Frame ...........................245Framebuffer Area ............... 246Debug Navigator ............... 247Variables View ................... 247Assistant Editor .................. 248Labeling OpenGL ES Objects in the Debugger .................... 249
Using the GDB Console ...........................249Stopping Program Execution ...............................249
Setting Breakpoints ........................250Setting Watchpoints .......................251Setting Catchpoints ........................251Examining Your Breakpoints ..........................251Setting Conditional Breakpoints .....................251Disabling and Deleting Breakpoints .............................. 253Command Lists ..............................255
Examining Data ................. 256Examining Dynamic Arrays ...........................256Displaying Data Automatically ......................257
Executing Shell Commands .................259Defining Your Own Commands ..........................259
Conditional Commands .................260Documenting Your Commands ......................261Reading Commands from a File .....................262
Xcode Tools Sensei 15
Command Hooks ..........................263Using the LLDB Console ..........................264
Getting Help ...................... 264Setting Breakpoints .............................265Setting Watchpoints ............................265Examining Breakpoints .......................266Disabling and Deleting Breakpoints ....................267Breakpoint Commands .......................268Command Aliases ...............................269Examining Variables ............................269Examining Memory .............................271
Executing Shell Commands ......................271LLDB Expressions ...............................273Logging .............................. 274
Chapter8:VersionControl 275Creating a Repository ...............................276
Creating a Local git Repository ...........................276Creating a Local Subversion Repository .............................. 276Creating a Remote git Repository ........................276Creating a Remote Subversion Repository ........................... 277How Many Repositories Should You Make? ........................ 277
Ignoring Files .......................... 277Naming the Ignore File .......................278What Files Should Be Ignored? ...........................278What to Do with the Ignore File? ........................278
Configuring the Repository for Xcode .......................279Cloning Repositories ............... 280Repositories Window ...............................280
Repository List ................... 281Detail View ........................ 281History ............................... 282
Importing Your Project to the Repository ...................282Importing to a git Repository ...............................282Importing to a Subversion Repository ..................283
Checking Out Files from a Subversion Repository .................... 284Seeing Which Files Have Changed in Your Project .................... 284Adding Files to the Repository ..................285Removing Files from the Repository .........................286Seeing the Changes You Made to a File ....................286Committing Changes You Made ................................287Discarding Changes ................ 288Viewing Annotations ............... 288Viewing a File’s Revisions ........................289
16 Table of Contents
Branching ............... 289Creating a Branch ...............................290Removing a Branch.............................290Switching Branches.............................290Merging ............................ 291Tracking Branches ...............................291
Pushing and Pulling ................ 292Snapshots ................ 292
Taking a Snapshot ...............................293Looking at a Project’s Snapshots .........................293Restoring a Snapshot ...........................293Deleting Snapshots .............................294Accessing Your Snapshots ...................294
Chapter9:Instruments 295Tracing From Xcode ................ 295Creating and Setting up a Trace Document ............................... 296
Creating a Trace Document .................296Mac OS X Templates .....................296iOS Templates ...............................297iOS Simulator Templates ................................298
Trace Document Window ...................298Adding and Removing Instruments .....................299Customizing the Track Pane ................299Showing the Detail View .....................300
Running a Trace ...................... 300Determining What to Trace .................300Choosing a Program to Trace ..............................301Tracing .............................. 301Recording Options ..............................302Alternate Trace Document Views ........................302
Examining Trace Results ...........................302Track Pane ......................... 303Detail View ........................ 303Console ............................. 303Source View ....................... 304Searching in the Detail View ...............................305Extended Detail View ..........................305Filtering Information ...........................306
Filtering by Time ............................306Searching ..................... 306Flagging Samples ...........................307Call Tree Data Mining ...................307
Call Tree Checkboxes ...............................308
Xcode Tools Sensei 17
Call Tree Constraints ................309Specific Data Mining ...............................309Data Mining Inside the Call Tree .............................. 310Call Tree Tips ............................310
Run Browser ...................... 311Exporting Trace Data ...........................311
Instrument-Specific Results .......................311Leaks ................. 311
Before You Trace ............................312Leaked Blocks ...............................312History ......................... 313Call Tree ....................... 313Cycles and Roots ...........................314Track Pane .................... 315
Allocations ........................ 315Before You Trace ............................315Statistics ....................... 316Object Summary ...........................316Instances ...................... 317History ......................... 318Call Trees ..................... 318Objects List .................. 319Heapshots .................... 319
Time Profiler ...................... 321Call Tree ....................... 321Finding Heavy Paths ......................322Focusing on a Subtree ...................322Finding Where a Function Spends Its Time .................... 323Sample List ................... 323Strategy Bar .................. 324
OpenGL ES Analyzer ..........................325Expert ........................... 325Frame Statistics ..............................326Trace ............................ 327Call Trees ..................... 327API Statistics ................. 328Single Frame Navigation ................328Overriding the Pipeline .................328
Activity Monitor ................. 329Summary ...................... 329Parent Child ................. 330Samples ........................ 330Trace Highlights ............................331
Creating a Custom Instrument ..................331Parts of an Instrument .........................331
18 Table of Contents
Parts of a Probe .................. 332Determining When the Probe Fires ................332Performing the Action ....................334
Custom Instrument Example ...............................334Start .............................. 334DATA ........................... 335BEGIN .......................... 335First Probe .................... 335Second Probe ............... 336Running a Trace.............................336Improvements to the Custom Instrument ....................... 337
Editing an Instrument ...............................337
Chapter10:Command-LineDebuggingTools 338A Command Line Primer ..........................338
Executing Commands as root ..............................338Navigating Directories ........................338Getting Help ...................... 340Finding Your Application’s Process ID .................340
fs_usage .................. 340Running fs_usage ................................341What fs_usage Tells You ......................341fs_usage Options ............... 343
-e Option ..................... 343-f Option ...................... 344-w Option .................... 344
sc_usage ................. 345What sc_usage Tells You ......................345
Program Summary Information ......................345System Call List .............................346
sc_usage Options ................................347-c Option ..................... 347-e Option ..................... 348-E Option ..................... 348-l Option ...................... 348-s Option ...................... 349
vmmap .................... 349What vmmap Tells You ........................349
Non-Writable Memory Regions .....................349Region Purpose ........................350Permissions ..............................351Sharing Modes .........................351
Writable Memory Regions .............................352Summary Report ............................353
Xcode Tools Sensei 19
vmmap Options ................. 353-d Option ..................... 354-w Option .................... 354-resident Option ............................354-pages Option ...............................355-interleaved Option .......................355-submap Option ............................355-allSplitLibs Option .......................355-noCoalesce Option ......................356-v Option ..................... 356
heap ....................... 356heap’s Output .................... 356heap Options ..................... 357
-guessNonObjects Option .............................358-sumObjectFields Option ..............................358-showSizes Option ........................358-addresses Option .........................358
leaks ....................... 359Running leaks .................... 359What leaks Tells You ............................359leaks Options ..................... 360
-nocontext Option .........................360-nostacks Option ...........................360-exclude Option ............................361
malloc_history ........................ 361Running malloc_history ......................361Running malloc_history on a Specific Memory Area ........................... 362Showing All Allocation Events.............................362
Chapter11:OpenGLTools 363OpenGL Profiler ...................... 363
Choosing a Program to Profile ............................364Custom Pixel Formats ....................364Setting Environment Variables ........................365Remote Profiling ............................365
Setting Breakpoints .............................366Multithread Control ......................367Breakpoint Actions ........................367
Profiling Your Program ........................367Viewing the Profiling Data ..................368
Trace Window ...............................368Statistics Window ..........................369Buffers Window .............................369Resources Window ........................370
20 Table of Contents
Textures and Renderbuffers ......................370Programs and Shaders ..............................371
Scripts Window .............................371Breakpoints Window .....................372Pixel Format Window ....................372Messages Window .........................372
OpenGL Driver Monitor ...........................373Getting Started ................... 373Customizing the Graph .......................374Table View ......................... 374Renderer Info ..................... 375
OpenGL Shader Builder ...........................375Creating a Project ...............................376Adding Shaders .................. 377Writing a Shader ................ 378Adding Textures ................. 378Looking at Variables ............................378Compiling a Project ............................379Testing a Shader ................. 380Benchmarking ................... 380Window Layouts ................ 380Using Your Shaders in an OpenGL Program ........................ 380
Creating a Shader ..........................381Creating a Shader Object .........................381Loading a Shader .....................382Reading Shader Source ............................382Compiling the Shader ..............................382
Creating a Program Object ............................383Cleanup ....................... 383
OpenGL ES Performance Detective ..........................384Before You Run Performance Detective ............................... 384Running Performance Detective ..........................384Viewing the Results ............................385