Objective-C
Notes for Professionals
Objective-C®
Notes for Professionals
100+ pages
of professional hints and tricks
GoalKicker.com
Free Programming Books
Disclaimer
This is an unocial free book created for educational purposes and is
not aliated with ocial 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: Basic Data Types
..................................................................................................................................
4
...............................................................................................................................................................
..........................................................................................................................................................
.................................................................................................................................................................
Section 2.1: SEL
Section 2.2: BOOL
Section 2.3: id
Section 2.4: IMP (implementation pointer)
Section 2.5: NSInteger and NSUInteger
.................................................................................................................
5
......................................................................................................................
6
Chapter 3: Enums
..........................................................................................................................................................
8
Section 3.1: typedef enum declaration in Objective-C
Section 3.2: Converting C++ std::vector<Enum> to an Objective-C Array
Section 3.3: Defining an enum
...............................................................................................
8
...............................................................
8
......................................................................................................................................
9
Chapter 4: Structs
......................................................................................................................................................
10
Section 4.1: Defining a Structure and Accessing Structure Members
Section 4.2: CGPoint
....................................................................................................................................................
10
....................................................................
10
Chapter 5: Classes and Objects
..........................................................................................................................
12
Section 5.1: Dierence between allocation and initialization
Section 5.2: Creating classes with initialization values
Section 5.3: Specifying Generics
Section 5.4: Singleton Class
Section 5.5: The "instancetype" return type
................................................................................................................................
........................................................................................................................................
.............................................................................................................
..................................................................................
12
............................................................................................
Chapter 6: Inheritance
.............................................................................................................................................
15
Section 6.1: Car is inherited from Vehicle
..................................................................................................................
15
Chapter 7: Methods
...................................................................................................................................................
17
Section 7.1: Class methods
Section 7.2: Pass by value parameter passing
Section 7.3: Pass by reference parameter passing
Section 7.4: Method parameters
Section 7.5: Create a basic method
Section 7.6: Return values
Section 7.7: Calling methods
Section 7.8: Instance methods
..........................................................................................................................................
........................................................................................................
.................................................................................................
17
................................................................................................................................
...........................................................................................................................
..........................................................................................................................................
......................................................................................................................................
...................................................................................................................................
Chapter 8: Properties
...............................................................................................................................................
21
Section 8.1: Custom getters and setters
Section 8.2: Properties that cause updates
Section 8.3: What are properties?
....................................................................................................................
21
..............................................................................................................
22
.............................................................................................................................
23
Chapter 9: Random Integer
..................................................................................................................................
26
Section 9.1: Basic Random Integer
Section 9.2: Random Integer within a Range
............................................................................................................................
26
...........................................................................................................
26
Chapter 10: BOOL / bool / Boolean / NSCFBoolean
................................................................................
27
Section 10.1: BOOL/Boolean/bool/NSCFBoolean
Section 10.2: BOOL VS Boolean
Chapter 11: Continue and Break!
.................................................................................................................................
.........................................................................................................................
....................................................................................................
27
Section 11.1: Continue and Break Statement
.............................................................................................................
28
4
4
Publicité
5
12
13
13
14
17
17
18
18
19
19
20
27
28
Chapter 12: Key Value Coding / Key Value Observing
............................................................................
29
Section 12.1: Most Common Real Life Key Value Coding Example
Section 12.2: Querying KVC Data
Section 12.3: Collection Operators
Section 12.4: Key Value Observing
..............................................................................................................................
.............................................................................................................................
............................................................................................................................
........................................................................
29
Chapter 13: NSString
.................................................................................................................................................
35
........................................................................................................................
..........................................................................................................................................
.................................................................................................................................
...................................................................................................................................................
.....................................................................................................................
..................................................................................................................................................
.......................................................................................................................................
Section 13.1: Encoding and Decoding
Section 13.2: String Length
Section 13.3: Comparing Strings
Section 13.4: Splitting
Section 13.5: Searching for a Substring
Section 13.6: Creation
Section 13.7: Changing Case
Section 13.8: Removing Leading and Trailing Whitespace
Section 13.9: Joining an Array of Strings
Section 13.10: Formatting
Section 13.11: Working with C Strings
Section 13.12: Reversing a NSString Objective-C
.........................................................................................................................
......................................................................................................
.....................................................................................
38
..................................................................................................................
............................................................................................................................................
Chapter 14: NSArray
.................................................................................................................................................
41
.................................................................................................
.......................................................
42
......................................................................................................................................
...............................................................................................................................
.......................................................................................................................................
..................................................................................................................................
Section 14.1: Creating Arrays
Section 14.2: Accessing elements
Section 14.3: Using Generics
Section 14.4: Reverse an Array
Section 14.5: Converting between Sets and Arrays
Section 14.6: Converting NSArray to NSMutableArray to allow modification
Section 14.7: Looping through
Section 14.8: Enumerating using blocks
Section 14.9: Comparing arrays
Section 14.10: Filtering Arrays With Predicates
Section 14.11: Sorting array with custom objects
Section 14.12: Sorting Arrays
Section 14.13: Filter NSArray and NSMutableArray
Section 14.14: Add objects to NSArray
Section 14.15: Finding out the Number of Elements in an Array
Section 14.16: Creating NSArray instances
....................................................................................................................................
....................................................................................................................
.................................................................................................................................
.........................................................................................................
......................................................................................................
......................................................................................................................................
..................................................................................................
.......................................................................................................................
................................................................................................................
.............................................................................
Chapter 15: NSMutableArray
Section 15.1: Sorting Arrays
Section 15.2: Creating an NSMutableArray
Section 15.3: Adding elements
Section 15.4: Insert Elements
Section 15.5: Deleting Elements
Section 15.6: Move object to another index
Section 15.7: Filtering Array content with Predicate
................................................................................................................................
.........................................................................................................................................
...............................................................................................................
....................................................................................................................................
......................................................................................................................................
..................................................................................................................................
46
..............................................................................................................
47
................................................................................................
47
Chapter 16: NSDictionary
........................................................................................................................................
48
......................................................................................................................................................
Section 16.1: Create
Section 16.2: Fast Enumeration
Section 16.3: Creating using literals
Section 16.4: Creating using dictionaryWithObjectsAndKeys:
Section 16.5: NSDictionary to NSArray
Section 16.6: NSDictionary to NSData
..................................................................................................................................
...........................................................................................................................
................................................................................
48
......................................................................................................................
.......................................................................................................................
29
30
32
35
35
35
36
37
37
38
Publicité
38
39
39
39
41
41
41
42
42
42
43
43
43
44
44
45
45
45
45
46
46
46
46
46
48
48
48
49
49
...........................................................................................................................
..............................................................................................................................
Section 16.7: NSDictionary to JSON
Section 16.8: Creating using plists
Section 16.9: Setting a Value in NSDictionary
Section 16.10: Getting a Value from NSDictionary
Section 16.11: Check if NSDictionary already has a key or not
Section 16.12: Block Based Enumeration
...........................................................................................................
...................................................................................................
...............................................................................
50
...................................................................................................................
50
Chapter 17: NSMutableDictionary
......................................................................................................................
52
Section 17.1: NSMutableDictionary Example
Section 17.2: Removing Entries From a Mutable Dictionary
.............................................................................................................
52
...................................................................................
53
Chapter 18: NSDate
....................................................................................................................................................
55
Section 18.1: Convert NSDate that is composed from hour and minute (only) to a full NSDate
Section 18.2: Converting NSDate to NSString
Section 18.3: Creating an NSDate
Section 18.4: Date Comparison
..................................................................................................................................
..............................................................................................................................
...........................................................................................................
........................
55
.....................................................................................................................................................
Chapter 19: NSURL
Section 19.1: Create
Section 19.2: Compare NSURL
Section 19.3: Modifying and Converting a File URL with removing and appending path
......................................................................................................................................................
...................................................................................................................................
....................................
58
Chapter 20: NSUrl send a post request
..........................................................................................................
60
Section 20.1: Simple POST request
Section 20.2: Simple Post Request With Timeout
............................................................................................................................
60
....................................................................................................
60
....................................................................................................................................................
......................................................................................................................................................
Chapter 21: NSData
Section 21.1: Create
Section 21.2: NSData and Hexadecimal String
Section 21.3: Get NSData length
Section 21.4: Encoding and decoding a string using NSData Base64
.........................................................................................................
................................................................................................................................
...................................................................
62
.........................................................................................................................................
.......................................................................................................................................
Chapter 22: NSPredicate
Section 22.1: Filter By Name
Section 22.2: Find movies except given ids
Section 22.3: Find all the objects which is of type movie
Section 22.4: Find Distinct object ids of array
Section 22.5: Find movies with specific ids
Section 22.6: Case Insensitive comparison with exact title match
Section 22.7: Case sensitive with exact title match
Section 22.8: Case Insensitive comparison with matching subset
..............................................................................................................
........................................................................................
..........................................................................................................
...............................................................................................................
.........................................................................
64
..................................................................................................
64
.........................................................................
65
Chapter 23: NSRegularExpression
.....................................................................................................................
66
Section 23.1: Check whether a string matches a pattern
Section 23.2: Find all the numbers in a string
........................................................................................
66
..........................................................................................................
66
Chapter 24: NSJSONSerialization
......................................................................................................................
67
Section 24.1: JSON Parsing using NSJSONSerialization Objective-C
.....................................................................
67
Chapter 25: NSCalendar
..........................................................................................................................................
69
Section 25.1: System Locale Information
Section 25.2: Initializing a Calendar
Section 25.3: Calendrical Calculations
Publicité
..................................................................................................................
69
...........................................................................................................................
......................................................................................................................
Chapter 26: NSAttributedString
..........................................................................................................................
71
Section 26.1: Using Enumerating over Attributes in a String and underline part of string
Section 26.2: Creating a string that has custom kerning (letter spacing) editshare
Section 26.3: Create a string with text struck through
Section 26.4: How you create a tri-color attributed string
............................................................................................
.....................................................................................
..................................
71
...........................................
49
49
49
50
55
56
56
58
58
58
61
61
61
62
63
63
64
64
64
64
69
69
71
71
72
Chapter 27: NSTimer
.................................................................................................................................................
73
Section 27.1: Storing information in the Timer
Section 27.2: Creating a Timer
Section 27.3: Invalidating a timer
Section 27.4: Manually firing a timer
...................................................................................................................................
...............................................................................................................................
.........................................................................................................................
..........................................................................................................
73
Chapter 28: NSObject
Section 28.1: NSObject
...............................................................................................................................................
75
................................................................................................................................................
75
Chapter 29: NSSortDescriptor
.............................................................................................................................
76
Section 29.1: Sorted by combinations of NSSortDescriptor
....................................................................................
76
Chapter 30: NSTextAttachment
..........................................................................................................................
77
Section 30.1: NSTextAttachment Example
................................................................................................................
77
Chapter 31: NSCache
Section 31.1: NSCache
.................................................................................................................................................
78
..................................................................................................................................................
Chapter 32: NSUserDefaults
Section 32.1: Simple example
Section 32.2: Clear NSUserDefaults
.................................................................................................................................
.....................................................................................................................................
..........................................................................................................................
79
Chapter 33: Subscripting
.........................................................................................................................................
80
Section 33.1: Subscripts with NSArray
Section 33.2: Custom Subscripting
Section 33.3: Subscripts with NSDictionary
.......................................................................................................................
............................................................................................................................
...............................................................................................................
80
Chapter 34: Low-level Runtime Environment
..............................................................................................
82
Section 34.1: Augmenting methods using Method Swizzling
Section 34.2: Attach object to another existing object (association)
Section 34.3: Calling methods directly
..................................................................................
82
....................................................................
83
......................................................................................................................
83
Chapter 35: Fast Enumeration
.............................................................................................................................
85
Section 35.1: Fast enumeration of an NSArray with index
Section 35.2: Fast enumeration of an NSArray
......................................................................................
85
........................................................................................................
85
Chapter 36: Categories
............................................................................................................................................
86
Section 36.1: Conforming to protocol
Section 36.2: Simple Category
Section 36.3: Declaring a class method
Section 36.4: Adding a property with a category
Section 36.5: Create a Category on XCode
........................................................................................................................
...................................................................................................................................
....................................................................................................................
....................................................................................................
87
..............................................................................................................
87
Chapter 37: Protocols
...............................................................................................................................................
91
...........................................................................................................
91
Section 37.1: Optional and required methods
Publicité
Section 37.2: Checking existence of optional method implementations
Section 37.3: Forward Declarations
Section 37.4: Conforming to Protocols
Section 37.5: Basic Protocol Definition
Section 37.6: Check conforms Protocol
Chapter 38: Protocols and Delegates
...........................................................................................................................
......................................................................................................................
......................................................................................................................
....................................................................................................................
..............................................................................................................
...............................................................
91
Section 38.1: Implementation of Protocols and Delegation mechanism
...............................................................
93
Chapter 39: Blocks
......................................................................................................................................................
94
......................................................................................................................................
Section 39.1: Block Typedefs
Section 39.2: Blocks as Properties
Section 39.3: Blocks as local variables
Section 39.4: Blocks as Method Parameters
Section 39.5: Defining and Assigning
.............................................................................................................................
......................................................................................................................
............................................................................................................
95
........................................................................................................................
95
73
73
74
78
79
79
80
80
86
86
86
91
92
92
92
93
94
94
95
Chapter 40: XML parsing
Section 40.1: XML Parsing
........................................................................................................................................
96
...........................................................................................................................................
96
Chapter 41: Declare class method and instance method
Section 41.1: How to declare class method and instance method
......................................................................
98
.........................................................................
98
Chapter 42: Predefined Macros
Section 42.1: Predefined Macros
..........................................................................................................................
99
................................................................................................................................
99
Chapter 43: Grand Central Dispatch
..............................................................................................................
100
Section 43.1: What is Grand central dispatch
.........................................................................................................
100
Chapter 44: Format-Specifiers
Section 44.1: Integer Example - %i
.........................................................................................................................
101
...........................................................................................................................
...............................................................................................................................................
................................................................................................................................................
.......................................................................................................................
Chapter 45: Logging
Section 45.1: Logging
Section 45.2: NSLog Output Format
Section 45.3: Removing Log Statements from Release Builds
Section 45.4: Logging Variable Values
Section 45.5: Empty message is not printed
Section 45.6: Using __FUNCTION __
Section 45.7: NSLog vs printf
Section 45.8: Logging NSLog meta data
Section 45.9: NSLog and BOOL type
Section 45.10: Logging by Appending to a File
...................................................................................................................
..........................................................................................................
.....................................................................................................................
...................................................................................................................................
................................................................................................................
.......................................................................................................................
......................................................................................................
............................................................................
102
101
102
102
102
102
103
103
103
104
104
105
Chapter 46: Error Handling
.................................................................................................................................
106
Section 46.1: Error & Exception handling with try catch block
Section 46.2: Asserting
..............................................................................................................................................
106
..............................................................................
106
Chapter 47: Modern Objective-C
......................................................................................................................
108
Section 47.1: Literals
Section 47.2: Container subscripting
..................................................................................................................................................
108
.......................................................................................................................
108
Chapter 48: Singletons
..........................................................................................................................................
110
Section 48.1: Using G...