ObjectiveCNotesForProfessionals.pdf

(1265 KB) Pobierz
Objective-C
Objective-C
Notes for Professionals
®
Notes for Professionals
of professional hints and tricks
100+ pages
GoalKicker.com
Free Programming Books
Disclaimer
This is an unocial free book created for educational purposes and is
not aliated with ocial Objective-C
®
group(s) or company(s).
All trademarks and registered trademarks are
the property of their respective owners
Contents
About
................................................................................................................................................................................... 1
Chapter 1: Getting started with Objective-C Language
............................................................................ 2
Section 1.1: Hello World
................................................................................................................................................. 2
Chapter 2: Protocols
.................................................................................................................................................... 4
Section 2.1: Optional and required methods
............................................................................................................... 4
Section 2.2: Checking existance of optional method implementations
................................................................... 4
Section 2.3: Forward Declarations
............................................................................................................................... 4
Section 2.4: Conforming to Protocols
.......................................................................................................................... 5
Section 2.5: Basic Protocol Definition
.......................................................................................................................... 5
Section 2.6: Check conforms Protocol
........................................................................................................................ 5
Chapter 3: Blocks
.......................................................................................................................................................... 6
Section 3.1: Block Typedefs
.......................................................................................................................................... 6
Section 3.2: Blocks as Properties
................................................................................................................................. 6
Section 3.3: Blocks as local variables
.......................................................................................................................... 7
Section 3.4: Blocks as Method Parameters
................................................................................................................ 7
Section 3.5: Defining and Assigning
............................................................................................................................ 7
Chapter 4: Categories
................................................................................................................................................ 8
Section 4.1: Conforming to protocol
............................................................................................................................ 8
Section 4.2: Simple Category
....................................................................................................................................... 8
Section 4.3: Declaring a class method
........................................................................................................................ 8
Section 4.4: Adding a property with a category
........................................................................................................ 9
Section 4.5: Create a Category on XCode
.................................................................................................................. 9
Chapter 5: Key Value Coding / Key Value Observing
............................................................................. 13
Section 5.1: Most Common Real Life Key Value Coding Example
.......................................................................... 13
Section 5.2: Querying KVC Data
................................................................................................................................ 13
Section 5.3: Collection Operators
.............................................................................................................................. 14
Section 5.4: Key Value Observing
............................................................................................................................. 16
Chapter 6: Logging
.................................................................................................................................................... 19
Section 6.1: Logging
..................................................................................................................................................... 19
Section 6.2: NSLog Output Format
............................................................................................................................ 19
Section 6.3: Removing Log Statements from Release Builds
................................................................................. 19
Section 6.4: Logging Variable Values
........................................................................................................................ 19
Section 6.5: Empty message is not printed
.............................................................................................................. 20
Section 6.6: Using __FUNCTION __
......................................................................................................................... 20
Section 6.7: NSLog vs printf
........................................................................................................................................ 20
Section 6.8: Logging NSLog meta data
.................................................................................................................... 21
Section 6.9: NSLog and BOOL type
........................................................................................................................... 21
Section 6.10: Logging by Appending to a File
.......................................................................................................... 21
Chapter 7: NSArray
.................................................................................................................................................... 23
Section 7.1: Creating Arrays
........................................................................................................................................ 23
Section 7.2: Accessing elements
................................................................................................................................ 23
Section 7.3: Using Generics
........................................................................................................................................ 23
Section 7.4: Reverse an Array
.................................................................................................................................... 24
Section 7.5: Converting between Sets and Arrays
................................................................................................... 24
Section 7.6: Converting NSArray to NSMutableArray to allow modification
........................................................ 24
Section 7.7: Looping through
..................................................................................................................................... 24
Section 7.8: Enumerating using blocks
..................................................................................................................... 24
Section 7.9: Comparing arrays
.................................................................................................................................. 25
Section 7.10: Filtering Arrays With Predicates
.......................................................................................................... 25
Section 7.11: Sorting array with custom objects
....................................................................................................... 26
Section 7.12: Add objects to NSArray
........................................................................................................................ 26
Section 7.13: Finding out the Number of Elements in an Array
.............................................................................. 26
Chapter 8: Classes and Objects
.......................................................................................................................... 27
Section 8.1: Dierence between allocation and initialization
.................................................................................. 27
Section 8.2: Creating classes with initialization values
............................................................................................ 27
Section 8.3: Specifying Generics
................................................................................................................................ 28
Section 8.4: Singleton Class
........................................................................................................................................ 28
Section 8.5: The "instancetype" return type
............................................................................................................. 29
Chapter 9: NSString
................................................................................................................................................... 30
Section 9.1: Encoding and Decoding
......................................................................................................................... 30
Section 9.2: String Length
........................................................................................................................................... 30
Section 9.3: Comparing Strings
.................................................................................................................................. 30
Section 9.4: Splitting
.................................................................................................................................................... 31
Section 9.5: Searching for a Substring
...................................................................................................................... 32
Section 9.6: Creation
................................................................................................................................................... 32
Section 9.7: Changing Case
........................................................................................................................................ 33
Section 9.8: Removing Leading and Trailing Whitespace
...................................................................................... 33
Section 9.9: Joining an Array of Strings
................................................................................................................... 33
Section 9.10: Formatting
............................................................................................................................................. 34
Section 9.11: Working with C Strings
.......................................................................................................................... 34
Section 9.12: Reversing a NSString Objective-C
....................................................................................................... 34
Chapter 10: NSDictionary
........................................................................................................................................ 36
Section 10.1: Create
...................................................................................................................................................... 36
Section 10.2: Fast Enumeration
.................................................................................................................................. 36
Section 10.3: NSDictionary to NSArray
...................................................................................................................... 36
Section 10.4: NSDictionary to NSData
....................................................................................................................... 36
Section 10.5: NSDictionary to JSON
.......................................................................................................................... 37
Section 10.6: Block Based Enumeration
.................................................................................................................... 37
Chapter 11: NSDictionary
......................................................................................................................................... 38
Section 11.1: Creating using literals
............................................................................................................................. 38
Section 11.2: Creating using dictionaryWithObjectsAndKeys:
................................................................................. 38
Section 11.3: Creating using plists
............................................................................................................................... 38
Section 11.4: Setting a Value in NSDictionary
............................................................................................................ 38
Section 11.5: Getting a Value from NSDictionary
...................................................................................................... 39
Section 11.6: Check if NSDictionary already has a key or not
................................................................................. 39
Chapter 12: Low-level Runtime Environment
............................................................................................... 40
Section 12.1: Augmenting methods using Method Swizzling
................................................................................... 40
Section 12.2: Attach object to another existing object (association)
..................................................................... 41
Section 12.3: Calling methods directly
....................................................................................................................... 41
Chapter 13: NSArray
.................................................................................................................................................. 43
Section 13.1: Sorting Arrays
......................................................................................................................................... 43
Section 13.2: Filter NSArray and NSMutableArray
................................................................................................... 43
Section 13.3: Creating NSArray instances
................................................................................................................. 43
Chapter 14: NSURL
..................................................................................................................................................... 44
Section 14.1: Create
...................................................................................................................................................... 44
Section 14.2: Compare NSURL
................................................................................................................................... 44
Section 14.3: Modifying and Converting a File URL with removing and appending path
................................... 44
Chapter 15: Methods
.................................................................................................................................................. 46
Section 15.1: Class methods
........................................................................................................................................ 46
Section 15.2: Pass by value parameter passing
....................................................................................................... 46
Section 15.3: Pass by reference parameter passing
................................................................................................ 46
Section 15.4: Method parameters
.............................................................................................................................. 47
Section 15.5: Create a basic method
......................................................................................................................... 47
Section 15.6: Return values
......................................................................................................................................... 47
Section 15.7: Calling methods
..................................................................................................................................... 48
Section 15.8: Instance methods
.................................................................................................................................. 49
Chapter 16: Error Handling
.................................................................................................................................... 50
Section 16.1: Error & Exception handling with try catch block
................................................................................. 50
Section 16.2: Asserting
................................................................................................................................................. 50
Chapter 17: Enums
...................................................................................................................................................... 52
Section 17.1: typedef enum declaration in Objective-C
........................................................................................... 52
Section 17.2: Converting C++ std::vector<Enum> to an Objective-C Array
............................................................ 52
Section 17.3: Defining an enum
.................................................................................................................................. 53
Chapter 18: NSData
................................................................................................................................................... 54
Section 18.1: Create
...................................................................................................................................................... 54
Section 18.2: NSData and Hexadecimal String
......................................................................................................... 54
Section 18.3: Get NSData lengh
.................................................................................................................................. 55
Section 18.4: Encoding and decoding a string using NSData Base64
................................................................... 55
Chapter 19: Random Integer
................................................................................................................................. 56
Section 19.1: Basic Random Integer
........................................................................................................................... 56
Section 19.2: Random Integer within a Range
.......................................................................................................... 56
Chapter 20: Properties
............................................................................................................................................. 57
Section 20.1: Custom getters and setters
................................................................................................................. 57
Section 20.2: Properties that cause updates
............................................................................................................ 58
Section 20.3: What are properties?
........................................................................................................................... 59
Chapter 21: NSDate
.................................................................................................................................................... 62
Section 21.1: Convert NSDate that is composed from hour and minute (only) to a full NSDate
........................ 62
Section 21.2: Converting NSDate to NSString
........................................................................................................... 62
Section 21.3: Creating an NSDate
.............................................................................................................................. 63
Section 21.4: Date Comparison
.................................................................................................................................. 63
Chapter 22: NSPredicate
......................................................................................................................................... 65
Section 22.1: Filter By Name
....................................................................................................................................... 65
Section 22.2: Find movies except given ids
.............................................................................................................. 66
Section 22.3: Find all the objects which is of type movie
........................................................................................ 66
Section 22.4: Find Distinct object ids of array
.......................................................................................................... 66
Section 22.5: Find movies with specific ids
............................................................................................................... 66
Section 22.6: Case Insensitive comparison with exact title match
......................................................................... 66
Section 22.7: Case sensitive with exact title match
.................................................................................................. 66
Section 22.8: Case Insensitive comparison with matching subset
......................................................................... 67
Chapter 23: NSMutableArray
................................................................................................................................ 68
Section 23.1: Sorting Arrays
........................................................................................................................................ 68
Section 23.2: Creating an NSMutableArray
.............................................................................................................. 68
Section 23.3: Adding elements
................................................................................................................................... 68
Section 23.4: Insert Elements
..................................................................................................................................... 68
Section 23.5: Deleting Elements
................................................................................................................................. 68
Section 23.6: Move object to another index
............................................................................................................. 69
Section 23.7: Filtering Array content with Predicate
................................................................................................ 69
Chapter 24: NSRegularExpression
..................................................................................................................... 70
Section 24.1: Check whether a string matches a pattern
........................................................................................ 70
Section 24.2: Find all the numbers in a string
.......................................................................................................... 70
Chapter 25: NSJSONSerialization
....................................................................................................................... 71
Section 25.1: JSON Parsing using NSJSONSerialization Objective c
...................................................................... 71
Chapter 26: Memory Management
.................................................................................................................... 73
Section 26.1: Memory management rules when using manual reference counting
............................................ 73
Section 26.2: Automatic Reference Counting
........................................................................................................... 74
Section 26.3: Strong and weak references
............................................................................................................... 75
Section 26.4: Manual Memory Management
........................................................................................................... 75
Chapter 27: Singletons
............................................................................................................................................. 77
Section 27.1: Using Grand Central Dispatch (GCD)
.................................................................................................. 77
Section 27.2: Creating Singleton and also preventing it from having multiple instance using alloc/init, new
................................................................................................................................................................................ 77
Section 27.3: Creating Singleton class and also preventing it from having multiple instances using
alloc/init
............................................................................................................................................................... 78
Chapter 28: NSCalendar
.......................................................................................................................................... 79
Section 28.1: System Locale Information
.................................................................................................................. 79
Section 28.2: Initializing a Calendar
.......................................................................................................................... 79
Section 28.3: Calendrical Calculations
...................................................................................................................... 79
Chapter 29: NSMutableDictionary
..................................................................................................................... 81
Section 29.1: NSMutableDictionary Example
............................................................................................................ 81
Section 29.2: Removing Entries From a Mutable Dictionary
.................................................................................. 82
Chapter 30: Multi-Threading
................................................................................................................................. 84
Section 30.1: Creating a simple thread
...................................................................................................................... 84
Section 30.2: Create more complex thread
.............................................................................................................. 84
Section 30.3: Thread-local storage
........................................................................................................................... 85
Chapter 31: NSAttributedString
........................................................................................................................... 86
Section 31.1: Using Enumerating over Attributes in a String and underline part of string
................................... 86
Section 31.2: Creating a string that has custom kerning (letter spacing) editshare
............................................ 86
Section 31.3: Create a string with text struck through
............................................................................................. 86
Section 31.4: How you create a tri-color attributed string
...................................................................................... 87
Chapter 32: NSTimer
................................................................................................................................................. 88
Section 32.1: Storing information in the Timer
.......................................................................................................... 88
Section 32.2: Creating a Timer
................................................................................................................................... 88
Section 32.3: Invalidating a timer
............................................................................................................................... 88
Section 32.4: Manually firing a timer
......................................................................................................................... 89
Chapter 33: Structs
.................................................................................................................................................... 90
Section 33.1: Defining a Structure and Accessing Structure Members
.................................................................. 90
Section 33.2: CGPoint
.................................................................................................................................................. 90
Chapter 34: Subscripting
........................................................................................................................................ 92
Section 34.1: Subscripts with NSArray
....................................................................................................................... 92
Section 34.2: Custom Subscripting
............................................................................................................................ 92
Section 34.3: Subscripts with NSDictionary
............................................................................................................... 92
Chapter 35: Basic Data Types
.............................................................................................................................. 94
Section 35.1: SEL
........................................................................................................................................................... 94
Zgłoś jeśli naruszono regulamin