Collections.pdf

(907 KB) Pobierz
Collections Programming Topics
CollectionsProgrammingTopics
DataManagement:DataTypes&Collections
2010-09-01
695673699.002.png
AppleInc.
©2010AppleInc.
Allrightsreserved.
exclusionmaynotapplytoyou.Thiswarrantygives
youspecificlegalrights,andyoumayalsohave
otherrightswhichvaryfromstatetostate.
Nopartofthispublicationmaybereproduced,
storedinaretrievalsystem,ortransmitted,in
anyformorbyanymeans,mechanical,
electronic,photocopying,recording,or
otherwise,withoutpriorwrittenpermissionof
AppleInc.,withthefollowingexceptions:Any
personisherebyauthorizedtostore
documentationonasinglecomputerfor
personaluseonlyandtoprintcopiesof
documentationforpersonaluseprovidedthat
thedocumentationcontainsApple’scopyright
notice.
TheApplelogoisatrademarkofAppleInc.
Nolicenses,expressorimplied,aregranted
withrespecttoanyofthetechnologydescribed
inthisdocument.Appleretainsallintellectual
propertyrightsassociatedwiththetechnology
describedinthisdocument.Thisdocumentis
intendedtoassistapplicationdevelopersto
developapplicationsonlyforApple-labeled
computers.
AppleInc.
1InfiniteLoop
Cupertino,CA95014
408-996-1010
Apple,theApplelogo,Cocoa,CocoaTouch,
Mac,MacOS,andObjective-Caretrademarks
ofAppleInc.,registeredintheUnitedStates
andothercountries.
IOSisatrademarkorregisteredtrademarkof
CiscointheU.S.andothercountriesandisused
underlicense.
EventhoughApplehasreviewedthisdocument,
APPLEMAKESNOWARRANTYORREPRESENTATION,
EITHEREXPRESSORIMPLIED,WITHRESPECTTO
THISDOCUMENT,ITSQUALITY,ACCURACY,
MERCHANTABILITY,ORFITNESSFORAPARTICULAR
PURPOSE.ASARESULT,THISDOCUMENTIS
PROVIDED“ASIS,”ANDYOU,THEREADER,ARE
ASSUMINGTHEENTIRERISKASTOITSQUALITY
ANDACCURACY.
INNOEVENTWILLAPPLEBELIABLEFORDIRECT,
INDIRECT,SPECIAL,INCIDENTAL,OR
CONSEQUENTIALDAMAGESRESULTINGFROMANY
DEFECTORINACCURACYINTHISDOCUMENT,even
ifadvisedofthepossibilityofsuchdamages.
THEWARRANTYANDREMEDIESSETFORTHABOVE
AREEXCLUSIVEANDINLIEUOFALLOTHERS,ORAL
ORWRITTEN,EXPRESSORIMPLIED.NoApple
dealer,agent,oremployeeisauthorizedtomake
anymodification,extension,oradditiontothis
warranty.
Somestatesdonotallowtheexclusionorlimitation
ofimpliedwarrantiesorliabilityforincidentalor
consequentialdamages,sotheabovelimitationor
695673699.003.png
Contents
AboutCollections 7
AtaGlance 7
AccessingIndexesandEasilyEnumeratingElements:Arrays 8
AssociatingDatawithArbitraryKeys:Dictionaries 8
OfferingFastInsertion,Deletion,andMembershipChecks:Sets 8
StoringSubsetsofArrays:IndexSets 8
StoringPathsThroughNestedArrays:IndexPaths 8
CustomizingMemoryandStorageOptions:PointerCollectionClasses(MacOSX) 9
WorkingwithCollections:CopyingandEnumerating 9
SeeAlso 10
Arrays:OrderedCollections 11
ArrayFundamentals 11
MutableArrays 12
UsingArrays 13
SortingArrays 14
SortingwithSortDescriptors 14
SortingwithBlocks 16
SortingwithFunctionsandSelectors 17
FilteringArrays 18
PointerArrays 19
Dictionaries:CollectionsofKeysandValues 21
DictionaryFundamentals 22
UsingMutableDictionaries 23
SortingaDictionary 24
UsingCustomKeys 24
UsingMapTables 25
Sets:UnorderedCollectionsofObjects 27
SetFundamentals 27
MutableSets 28
UsingSets 29
HashTables 30
IndexSets:StoringIndexesintoanArray 33
IndexSetFundamentals 33
2010-09-01 | © 2010 Apple Inc. All Rights Reserved.
3
695673699.004.png
MutableIndexSets 34
IteratingThroughIndexSets 34
IndexSetsandBlocks 35
IndexPaths:StoringaPathThroughNestedArrays 37
IndexPathFundamentals 37
UsingIndexPaths 38
CopyingCollections 39
ShallowCopies 39
DeepCopies 40
CopyingandMutability 40
Enumeration:TraversingaCollection’sElements 41
FastEnumeration 41
UsingBlock-BasedEnumeration 41
UsinganEnumerator 42
PointerFunctionOptions 45
PointerCollectionFundamentals 45
ConfiguringPointerCollectionstoHoldObjects 46
ConfiguringPointerCollectionsforArbitraryPointerUse 46
DocumentRevisionHistory 49
4
2010-09-01 | © 2010 Apple Inc. All Rights Reserved.
695673699.005.png
FiguresandListings
Arrays:OrderedCollections 11
Figure1 Arrays 11
Figure2 Sortingarrays 14
Figure3 Pointerarrayobjectownership 19
Listing1 Addingtoandremovingfromarrays 12
Listing2 Accessinganobjectafteritsremovalisdangerous 13
Listing3 Retainingobjectsbeforeremovalgivesyouownership 13
Listing4 Searchingforanobjectinanarray 14
Listing5 Creatingandsortinganarrayofdictionaries 15
Listing6 Sortingbyfirstname,lastname 16
Listing7 Sortingwithafunctionislessflexible 16
Listing8 Blockseasecustomsortingofarrays 16
Listing9 Sortingusingselectorsandfunctions 17
Listing10 Filteringarrayswithpredicates 18
Listing11 Pointerarrayconfiguredfornonobjectpointers 20
Dictionaries:CollectionsofKeysandValues 21
Figure1 Dictionaries 21
Figure2 Maptableobjectownership 25
Listing1 Accessinganobjectafteritsremovalisdangerous 23
Listing2 Retainingobjectsbeforeremovalgivesyouownership 23
Listing3 Addingobjectstoadictionary 23
Listing4 Addingentriesfromanotherdictionary 23
Listing5 Sortingdictionarykeysbyvalue 24
Listing6 Blockseasecustomsortingofdictionaries 24
Listing7 Maptableconfiguredfornonobjectpointers 26
Sets:UnorderedCollectionsofObjects 27
Figure1 Sets 27
Figure2 Hashtableobjectownership 30
Listing1 Accessinganobjectafteritsremovalisdangerous 29
Listing2 Retainingobjectsbeforeremovalgivesyouownership 29
Listing3 Hashtableconfiguredfornonobjectpointers 31
IndexSets:StoringIndexesintoanArray 33
Figure1 Indexsetandarrayinteraction 33
Listing1 Addingindexestoamutableindexset 34
Listing2 Forwarditerationthroughanindexset 34
2010-09-01 | © 2010 Apple Inc. All Rights Reserved.
5
695673699.001.png
Zgłoś jeśli naruszono regulamin