ptgCocoa®ProgrammingDeveloper’sHandbookFromtheLibraryofALESSANDROCAROLLOptgThispageintentionallyleftblankFromtheLibraryofALESSANDROCAROLLOptgCocoa®ProgrammingDeveloper’sHandbookDavidChisnallUpperSaddleRiver,NJ•Boston•Indianapolis•SanFranciscoNewYork•Toronto•Montreal•London•Munich•Paris•MadridCapetown•Sydney•Tokyo•Singapore•MexicoCityFromtheLibraryofALESSANDROCAROLLOptgCocoa®ProgrammingDeveloper’sHandbookCopyright©2010PearsonEducation,Inc.Allrightsreserved.Nopartofthisbookshallbereproduced,storedinaretrievalsystem,ortransmittedbyanymeans,electronic,mechanical,photocopying,recording,orotherwise,withoutwrittenpermissionfromthepublisher.Nopatentliabilityisassumedwithrespecttotheuseoftheinformationcontainedherein.Althougheveryprecautionhasbeentakeninthepreparationofthisbook,thepublisherandauthorassumenoresponsibilityforerrorsoromissions.Norisanyliabilityassumedfordamagesresultingfromtheuseoftheinformationcontainedherein.ISBN-13:978-0-321-63963-9ISBN-10:0-321-63963-4LibraryofCongressCataloging-in-PublicationDataChisnall,David.Cocoaprogrammingdeveloper’shandbook/DavidChisnall.p.cm.Includesindex.ISBN978-0-321-63963-9(pbk.:alk.paper)1.Cocoa(Applicationdevelopmentenvironment)2.Object-orientedprogramming(Computerscience)3.Applicationprograminterfaces(Computersoftware)4.MacOS.I.Title.QA76.64.C4852010005.26’8—dc222009042661PrintedintheUnitedStatesonrecycledpaperatEdwardsBrothersinAnnArbor,Michigan.FirstPrintingDecember2009TrademarksAlltermsmentionedinthisbookthatareknowntobetrademarksorservicemarkshavebeenappropriatelycapitalized.Pearsoncannotattesttotheaccuracyofthisinformation.Useofaterminthisbookshouldnotberegardedasaffectingthevalidityofanytrademarkorservicemark.WarningandDisclaimerEveryefforthasbeenmadetomakethisbookascompleteandasaccurateaspossible,butnowarrantyorfitnessisimplied.Theinformationprovidedisonan“asis”basis.Theauthorandthepublishershallhaveneitherliabilitynorresponsibilitytoanypersonorentitywithrespecttoanylossordamagesarisingfromtheinformationcontainedinthisbook.BulkSalesPearsonoffersexcellentdiscountsonthisbookwhenorderedinquantityforbulkpurchasesorspecialsales.Formoreinformation,pleasecontactusbyphoneoremail:U.S.CorporateandGovernmentSales1-800-382-3419corpsales@pearsontechgroup.comForsalesoutsideoftheU.S.,pleasecontacttheInternationalSalesgroup:InternationalSalesinternational@pearson.comEditor-in-ChiefMarkL.TaubManagingEditorJohnFullerFull-ServiceProductionManagerJulieB.NahilTechnicalReviewerGregoryCasamentoBookDesignerGaryAdairCompositionDavidChisnallFromtheLibraryofALESSANDROCAROLLOptgContentsListofFiguresxviiListofTablesxxiiiPrefacexxvIIntroducingCocoa11CocoaandMacOSX31.1UnderstandingWhentoUseCocoa................31.1.1Carbon.............................31.1.2Java...............................51.1.3Cocoa..............................51.1.4UNIXAPIs...........................71.1.5OtherChoices.........................81.2UnderstandingCocoa’sRoleinMacOSX............91.2.1Cocoa..............................91.2.2Quartz..............................111.2.3CoreFoundation........................131.2.4Darwin.............................141.2.5XNU...............................151.3Overview...............................162CocoaLanguageOptions172.1ObjectOrientation.........................172.2Objective-C.............................192.2.1Objective-CCompilerChoices................202.2.2DifferencesfromJavaandC++................222.2.3Objective-C2.0.........................232.3RubyandPython..........................24vFromtheLibraryofALESSANDROCAROLLOptgviContents2.4Summary...............................253UsingApple’sDeveloperTools273.1ObtainingApple’sDeveloperTools................283.2InterfaceBuilder...........................283.2.1OutletsandActions......................303.2.2CocoaBindings.........................313.2.3DrawingaSimpleApplication.................343.3XCode................................373.3.1CreatingaSimpleProject...................373.3.2OpenStepBundles.......................393.3.3DeveloperExamples......................413.3.4BuildingWithoutXCode...................423.4Objective-C.............................433.4.1WhyLearnObjective-C?...................443.4.2AdditionstoC.........................443.4.3ExceptionsandSynchronization...............523.4.4Introspection..........................553.4.5Objective-CinC........................583.4.6Objective-C2.0.........................603.4.7Blocks..............................633.4.8Objective-C++.........................663.5CocoaConventions.........................663.5.1Naming.............................663.5.2MemoryManagement.....................703.5.3ConstructorsandInitializers.................723.6Summary...............................74IITheCocoaFrameworks774Foundation:TheObjective-CStandardLibrary794.1GeneralConcepts..........................794.1.1Mutability............................804.1.2ClassClusters..........................804.2CoreFoundationTypes.......................854.3BasicDataTypes..........................8