ObjC.pdf

(911 KB) Pobierz
The Objective-C Programming Language
TheObjective-CProgrammingLanguage
Tools&Languages:Objective-C
2010-12-08
695712235.002.png
AppleInc.
©2010AppleInc.
Allrightsreserved.
ORWRITTEN,EXPRESSORIMPLIED.NoApple
dealer,agent,oremployeeisauthorizedtomake
anymodification,extension,oradditiontothis
warranty.
Somestatesdonotallowtheexclusionorlimitation
ofimpliedwarrantiesorliabilityforincidentalor
consequentialdamages,sotheabovelimitationor
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,iBook,iBooks,
Instruments,Mac,MacOS,andObjective-Care
trademarksofAppleInc.,registeredinthe
UnitedStatesandothercountries.
IOSisatrademarkorregisteredtrademarkof
CiscointheU.S.andothercountriesandisused
underlicense.
JavaisaregisteredtrademarkofOracleand/or
itsaffiliates.
TimesisaregisteredtrademarkofHeidelberger
DruckmaschinenAG,availablefromLinotype
LibraryGmbH.
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
695712235.003.png
Contents
Introduction Introduction 9
WhoShouldReadThisDocument 9
OrganizationofThisDocument 9
Conventions 10
SeeAlso 11
TheRuntimeSystem 11
MemoryManagement 11
Chapter1 Objects,Classes,andMessaging 13
TheRuntimeSystem 13
Objects 13
ObjectBasics 13
id 14
DynamicTyping 14
MemoryManagement 15
ObjectMessaging 15
MessageSyntax 15
SendingMessagestonil 17
TheReceiver’sInstanceVariables 18
Polymorphism 18
DynamicBinding 19
DynamicMethodResolution 19
DotSyntax 19
Classes 23
Inheritance 23
ClassTypes 26
ClassObjects 28
ClassNamesinSourceCode 32
TestingClassEquality 33
Chapter2 DefiningaClass 35
SourceFiles 35
ClassInterface 35
ImportingtheInterface 37
ReferringtoOtherClasses 37
TheRoleoftheInterface 38
ClassImplementation 38
ReferringtoInstanceVariables 39
TheScopeofInstanceVariables 40
2010-12-08 | © 2010 Apple Inc. All Rights Reserved.
3
695712235.004.png
CONTENTS
Messagestoselfandsuper 43
AnExample:Usingselfandsuper 44
Usingsuper 45
Redefiningself 46
Chapter3 AllocatingandInitializingObjects 49
AllocatingandInitializingObjects 49
TheReturnedObject 49
ImplementinganInitializer 50
ConstraintsandConventions 50
HandlingInitializationFailure 52
CoordinatingClasses 53
TheDesignatedInitializer 55
CombiningAllocationandInitialization 57
Chapter4 Protocols 59
DeclaringInterfacesforOtherstoImplement 59
MethodsforOtherstoImplement 60
DeclaringInterfacesforAnonymousObjects 61
NonhierarchicalSimilarities 61
FormalProtocols 62
DeclaringaProtocol 62
OptionalProtocolMethods 62
InformalProtocols 63
ProtocolObjects 64
AdoptingaProtocol 64
ConformingtoaProtocol 65
TypeChecking 65
ProtocolsWithinProtocols 66
ReferringtoOtherProtocols 67
Chapter5 DeclaredProperties 69
Overview 69
PropertyDeclarationandImplementation 69
PropertyDeclaration 70
PropertyDeclarationAttributes 70
PropertyImplementationDirectives 73
UsingProperties 74
SupportedTypes 74
PropertyRedeclaration 75
Copy 75
dealloc 76
CoreFoundation 77
4
2010-12-08 | © 2010 Apple Inc. All Rights Reserved.
695712235.005.png
CONTENTS
Example:DeclaringPropertiesandSynthesizingAccessors 77
SubclassingwithProperties 79
PerformanceandThreading 79
RuntimeDifference 80
Chapter6 CategoriesandExtensions 81
AddingMethodstoClasses 81
HowYouCanUseCategories 82
CategoriesoftheRootClass 83
Extensions 83
Chapter7 AssociativeReferences 87
AddingStorageOutsideaClassDefinition 87
CreatingAssociations 87
RetrievingAssociatedObjects 88
BreakingAssociations 88
CompleteExample 88
Chapter8 FastEnumeration 91
Thefor…inSyntax 91
AdoptingFastEnumeration 91
UsingFastEnumeration 92
Chapter9 EnablingStaticBehavior 95
DefaultDynamicBehavior 95
StaticTyping 95
TypeChecking 96
ReturnandParameterTypes 97
StaticTypingtoanInheritedClass 97
Chapter10 Selectors 99
MethodsandSelectors 99
SELand@selector 99
MethodsandSelectors 100
MethodReturnandParameterTypes 100
VaryingtheMessageatRuntime 100
TheTarget-ActionDesignPattern 101
AvoidingMessagingErrors 101
2010-12-08 | © 2010 Apple Inc. All Rights Reserved.
5
695712235.001.png
Zgłoś jeśli naruszono regulamin