Objective-C Language
Total Page:16
File Type:pdf, Size:1020Kb
Objective-C Language #objective-c Table of Contents About 1 Chapter 1: Getting started with Objective-C Language 2 Versions 2 Examples 2 Hello World 2 Compiling the program 3 Chapter 2: Basic Data Types 4 Syntax 4 Examples 4 BOOL 4 id 4 SEL 5 IMP (implementation pointer) 6 NSInteger and NSUInteger 7 Chapter 3: Blocks 9 Syntax 9 Remarks 9 Examples 9 Blocks as Method Parameters 9 Defining and Assigning 10 Blocks as Properties 10 Block Typedefs 10 Blocks as local variables 11 Chapter 4: BOOL / bool / Boolean / NSCFBoolean 12 Examples 12 BOOL/Boolean/bool/NSCFBoolean 12 BOOL VS Boolean 12 Chapter 5: Categories 14 Syntax 14 Remarks 14 Examples 14 Simple Category 14 Declaring a class method 14 Adding a property with a category 15 Conforming to protocol 15 Create a Category on XCode 16 Chapter 6: Classes and Objects 19 Syntax 19 Examples 19 Creating classes with initialization values 19 Singleton Class 19 The "instancetype" return type 21 Specifying Generics 21 Difference between allocation and initialization 22 Chapter 7: Continue and Break! 23 Examples 23 Continue and Break Statement 23 Chapter 8: Declare class method and instance method 25 Introduction 25 Syntax 25 Examples 25 How to declare class method and instance method. 25 Chapter 9: Enums 27 Syntax 27 Examples 27 Defining an enum 27 typedef enum declaration in Objective-C 27 Converting C++ std::vector to an Objective-C Array 28 Chapter 10: Error Handling 30 Syntax 30 Examples 30 Asserting 30 Error & Exception handling with try catch block 30 Chapter 11: Fast Enumeration 32 Examples 32 Fast enumeration of an NSArray 32 Fast enumeration of an NSArray with index. 32 Chapter 12: Format-Specifiers 33 Introduction 33 Syntax 33 Remarks 33 Examples 34 Integer Example - %i 34 Chapter 13: Grand Central Dispatch 35 Introduction 35 Examples 35 What is Grand central dispatch. 35 Chapter 14: Inheritance 36 Syntax 36 Examples 36 Car is inherited from Vehicle 36 Chapter 15: Key Value Coding / Key Value Observing 38 Examples 38 Most Common Real Life Key Value Coding Example 38 Key Value Observing 38 Querying KVC Data 40 Collection Operators 40 Chapter 16: Logging 44 Syntax 44 Remarks 44 Examples 44 Logging 44 NSLog vs printf 44 NSLog Output Format 45 Logging Variable Values 45 Empty message is not printed 45 Removing Log Statements from Release Builds 46 Using __FUNCTION __ 46 NSLog and BOOL type 46 Logging NSLog meta data 47 Logging by Appending to a File 47 Chapter 17: Low-level Runtime Environment 49 Remarks 49 Examples 49 Attach object to another existing object (association) 49 Augmenting methods using Method Swizzling 49 Calling methods directly 51 Chapter 18: Memory Management 53 Examples 53 Automatic Reference Counting 53 Strong and weak references 54 Manual Memory Management 54 Memory management rules when using manual reference counting. 55 Chapter 19: Methods 57 Syntax 57 Examples 57 Method parameters 57 Create a basic method 57 Return values 58 Class methods 58 Calling methods 58 Instance methods 59 Pass by value parameter passing 59 Pass by reference parameter passing 60 Chapter 20: Modern Objective-C 62 Examples 62 Literals 62 NSNumber 62 NSArray 62 NSDictionary 62 Container subscripting 63 Chapter 21: Multi-Threading 64 Examples 64 Creating a simple thread 64 Create more complex thread 64 Thread-local storage 65 Chapter 22: NSArray 66 Syntax 66 Examples 66 Creating Arrays 66 Finding out the Number of Elements in an Array 66 Accessing elements 66 Getting a single item 66 First and Last Item 67 Filtering Arrays With Predicates 67 Converting NSArray to NSMutableArray to allow modification 67 Sorting array with custom objects 67 Compare method 67 NSSortDescriptor 68 Blocks 68 Performance 68 Converting between Sets and Arrays 68 Reverse an Array 68 Looping through 69 Using Generics 69 Enumerating using blocks 69 Comparing arrays 70 Add objects to NSArray 70 Chapter 23: NSArray 71 Examples 71 Creating NSArray instances 71 Sorting Arrays 71 Filter NSArray and NSMutableArray 71 Chapter 24: NSAttributedString 73 Examples 73 Creating a string that has custom kerning (letter spacing) editshare 73 Create a string with text struck through 73 Using Enumerating over Attributes in a String and underline part of string 73 How you create a tri-color attributed string. 74 Chapter 25: NSCache 75 Examples 75 NSCache 75 Chapter 26: NSCalendar 76 Examples 76 System Locale Information 76 Initializing a Calendar 76 Calendrical Calculations 77 Chapter 27: NSData 78 Examples 78 Create 78 Get NSData lengh 78 Encoding and decoding a string using NSData Base64 78 NSData and Hexadecimal String 79 Chapter 28: NSDate 81 Remarks 81 Examples 81 Creating an NSDate 81 Date Comparison 81 Convert NSDate that is composed from hour and minute (only) to a full NSDate 82 Converting NSDate to NSString 83 Chapter 29: NSDictionary 84 Examples 84 Create 84 NSDictionary to NSArray 84 NSDictionary to NSData 84 NSDictionary to JSON 85 Block Based Enumeration 85 Fast Enumeration 85 Chapter 30: NSDictionary 86 Syntax 86 Remarks 86 Examples 86 Creating using literals 86 Creating using dictionaryWithObjectsAndKeys: 86 Creating using plists 87 Setting a Value in NSDictionary 87 Standard 87 Shorthand 87 Getting a Value from NSDictionary 87 Standard 88 Shorthand 88 Check if NSDictionary already has a key or not 88 Chapter 31: NSJSONSerialization 89 Syntax 89 Parameters 89 Remarks 89 Examples 89 JSON Parsing using NSJSONSerialization Objective c 89 Chapter 32: NSMutableArray 91 Examples 91 Adding elements 91 Insert Elements 91 Deleting Elements 91 Sorting Arrays 92 Move object to another index 92 Filtering Array content with Predicate 92 Creating an NSMutableArray 92 Chapter 33: NSMutableDictionary 94 Parameters 94 Examples 94 NSMutableDictionary Example 94 Removing Entries From a Mutable Dictionary 95 Chapter 34: NSObject 97 Introduction 97 Syntax 97 Examples 97 NSObject 97 Chapter 35: NSPredicate 99 Syntax 99 Remarks 99 Examples 99 Filter By Name 99 Find movies except given ids 100 Find all the objects which is of type movie 101 Find Distinct object ids of array 101 Find movies with specific ids 101 Case Insensitive comparison with exact title match 101 Case sensitive with exact title match 101 Case Insensitive comparison with matching subset 101 Chapter 36: NSRegularExpression 102 Syntax 102 Examples 102 Find all the numbers in a string 102 Check whether a string matches a pattern 102 Chapter 37: NSSortDescriptor 104 Examples 104 Sorted by combinations of NSSortDescriptor 104 Chapter 38: NSString 105 Introduction 105 Remarks 105 Examples 105 Creation 105 String Length 106 Changing Case 106 Comparing Strings 106 Joining an Array of Strings 107 Encoding and Decoding 107 Splitting 108 Searching for a Substring 109 Working with C Strings 109 Removing Leading and Trailing Whitespace 110 Formatting 110 Reversing a NSString Objective-C 110 Chapter 39: NSTextAttachment 112 Syntax 112 Remarks 112 Examples 112 NSTextAttachment Example 112 Chapter 40: NSTimer 113 Examples 113 Creating a Timer 113 Invalidating a timer 113 Manually firing a timer 113 Storing information in the Timer 114 Chapter 41: NSURL 115 Examples 115 Create 115 Compare NSURL 115 Modifying and Converting a File URL with removing and appending path 115 Chapter 42: NSUrl send a post request 117 Examples 117 Simple POST request 117 Simple Post Request With Timeout 117 Chapter 43: NSUserDefaults 118 Examples 118 Simple example 118 Clear NSUserDefaults 118 Chapter 44: Predefined Macros 119 Introduction 119 Syntax 119 Examples 119 Predefined Macros 119 Chapter 45: Properties 120 Syntax 120 Parameters 120 Examples 121 What are properties? 121 Custom getters and setters 122 Properties that cause updates 123 Chapter 46: Protocols 126 Examples 126 Basic Protocol Definition 126 Optional and required methods 126 Conforming to Protocols 126 Forward Declarations 127 Checking existance of optional method implementations 127 Check conforms Protocol 127 Chapter 47: Protocols and Delegates 129 Remarks 129 Examples 129 Implementation of Protocols and Delegation mechanism. 129 Chapter 48: Random Integer 131 Examples 131 Basic Random Integer 131 Random Integer within a Range 131 Chapter 49: Singletons 132 Introduction 132 Examples 132 Using Grand Central Dispatch (GCD) 132 Creating Singleton class and also preventing it from having multiple instances using alloc 132 Creating Singleton and also preventing it from having multiple instance using alloc/init, 133 Chapter 50: Structs 135 Syntax 135 Remarks 135 Examples 135 CGPoint 135 Defining a Structure and Accessing Structure Members 136 Chapter 51: Subscripting 138 Examples 138 Subscripts with NSArray 138 Subscripts with NSDictionary 138 Custom Subscripting 139 Chapter 52: Unit testing using Xcode 140 Remarks 140 Examples 140 Testing a block of code or some method: 141 Feed the dummy data to the method under test if required & then compare the expected & act 141 Testing asynchronous block of code: 141 Measuring Performance of a block of code: 141 Running Test Suits: 142 Note: 142 Chapter 53: XML parsing 144 Examples 144 XML Parsing 144 Credits 146 About You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: objective-c-language It is an unofficial and free Objective-C Language ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official Objective-C Language. The content is released under Creative Commons BY-SA, and the list