📄 Page
1
(This page has no text content)
📄 Page
2
Objective-C Objective-C is a general-purpose, object-oriented programming language that extends the C programming language with Smalltalk-style messaging. While si- multaneously developing for OS X and iOS, Objective-C’s capabilities have been bolstered by the inclusion of a dynamic runtime and assistance for object-oriented programming. Objective-C: The Ultimate Guide walks developers and coders through a straight- forward and practical method of learning the Objective-C programming language. This book discusses the basics in brief, and then moves on to more advanced and detailed exercises to help readers quickly gain the required knowledge. The focus in this book remains on writing optimized and well-structured code in Objective-C. Key Features: • Follows a hands-on approach and offers practical lessons and tutorials related to Objective-C • Discusses Objective-C using real world industry concepts • Includes at-length discussion of Objective-C concepts to help build robust knowledge
📄 Page
3
(This page has no text content)
📄 Page
4
Objective-C The Ultimate Guide Sufyan bin Uzayr
📄 Page
5
First edition published 2024 by CRC Press 2385 NW Executive Center Drive, Suite 320, Boca Raton, FL 33431 and by CRC Press 4 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN CRC Press is an imprint of Taylor & Francis Group, LLC © 2024 Sufyan bin Uzayr Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, access www.copyright.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. For works that are not available on CCC please contact mpkbookspermissions@tandf. co.uk Trademark Notice: Product or corporate names may be trademarks or registered trademarks and are used only for identification and explanation without intent to infringe. ISBN: 9781032413211 (hbk) ISBN: 9781032413198 (pbk) ISBN: 9781003357506 (ebk) DOI: 10.1201/9781003357506 Typeset in Minion Pro by KnowledgeWorks Global Ltd.
📄 Page
7
(This page has no text content)
📄 Page
8
vii Contents About the Author, xix Acknowledgments, xx Chapter 1 ◾ Crash Course in Objective-C 1 OBJECTIVE-C SPECIFICS 2 WHY OBJECTIVE-C? 2 DIFFERENTIATING OBJECTIVE-C VERSUS SWIFT 3 DIFFERENTIATING OBJECTIVE-C FROM C++ 3 UPSIDES AND DOWNSIDES OF OBJECTIVE-C 4 Upsides 4 Downsides 4 FACTORS TO CONSIDER 5 OBJECTIVE-C OVERVIEW 5 FRAMEWORK FOR THE FOUNDATION 6 LEARNING OBJECTIVE-C 6 USING OBJECTIVE-C 6 SETUP OF THE OBJECTIVE-C ENVIRONMENT 6 LOCAL ENVIRONMENT CONFIGURATION 6 EDITOR OF TEXT 6 GCC COMPILER 7 UNIX/LINUX INSTALLATION 7 MAC OS INSTALLATION 8 WINDOWS INSTALLATION 9 STRUCTURE OF THE OBJECTIVE-C PROGRAM 9
📄 Page
9
viii ◾ Contents OBJECTIVE-C EXAMPLE OF HELLO EVERYONE 9 BASIC SYNTAX IN OBJECTIVE-C 11 OBJECTIVE-C TOKENS 11 SEMICOLONS; 11 COMMENTS 11 IDENTIFIERS 12 KEYWORDS 12 WHITESPACE IN OBJECTIVE-C 12 DATA TYPES IN OBJECTIVE-C 13 TYPES OF INTEGERS 14 TYPES OF FLOATING-POINT 14 VOID TYPE 15 VARIABLES IN OBJECTIVE-C 15 OBJECTIVE-C VARIABLE DEFINITION 16 OBJECTIVE-C VARIABLE DECLARATION 17 OBJECTIVE-C lvalues AND rvalues 18 CONSTANTS IN OBJECTIVE-C 19 INTEGER LITERALS 19 FLOATING-POINT LITERALS 19 CHARACTER CONSTANTS 20 STRING LITERALS 21 CONSTANT DEFINITION 21 #define Preprocessor 21 const Keyword 22 OPERATORS IN OBJECTIVE-C 22 ARITHMETIC OPERATORS IN OBJECTIVE-C 23 RELATIONAL OPERATORS 23 LOGICAL OPERATORS IN OBJECTIVE-C 24 BITWISE OPERATORS 24 ASSIGNMENT OPERATORS 25 MISC OPERATORS ↦ SIZEOF & TERNARY 26 OPERATORS’ PRECEDENCE IN THE OBJECTIVE-C 26
📄 Page
10
Contents ◾ ix LOOPS IN OBJECTIVE-C 27 CONTROL STATEMENTS FOR LOOPS 28 INFINITE LOOP 28 DECISION MAKING IN OBJECTIVE-C 29 THE ? : OPERATOR 30 FUNCTIONS IN OBJECTIVE-C 30 CREATING A METHOD 31 DECLARATIONS OF METHOD 32 CALLING A METHOD 32 FUNCTION ARGUMENTS 34 BLOCKS IN OBJECTIVE-C 34 Simple Block Declaration Syntax 35 Implementation of a Simple Block 35 BLOCKS TAKE ARGUMENTS AND RETURN VALUES 35 BLOCKS USING THE TYPE DEFINITIONS 35 NUMBERS IN OBJECTIVE-C 36 ARRAYS IN OBJECTIVE-C 38 DECLARING ARRAYS 38 ARRAYS INITIALIZATION 39 ACCESSING ARRAY ELEMENTS 39 ARRAYS IN OBJECTIVE-C IN DEPTH 40 POINTERS IN OBJECTIVE-C 41 WHAT EXACTLY ARE POINTERS IN OBJECTIVE-C? 41 How Do Pointers Work? 42 OBJECTIVE-C NULL POINTERS 43 DETAILS ABOUT OBJECTIVE-C POINTERS 43 STRINGS IN OBJECTIVE-C 44 STRUCTURES IN OBJECTIVE-C 46 CREATING A STRUCTURE 47 ACCESS TO STRUCTURE MEMBERS 47 FUNCTION ARGUMENTS AS STRUCTURES 48 POINTERS TO STRUCTURES 50
📄 Page
11
x ◾ Contents BIT FIELDS 52 PREPROCESSORS IN OBJECTIVE-C 52 EXAMPLES OF PREPROCESSORS 53 PREDEFINED MACROS 54 OPERATORS OF PREPROCESSORS 55 Macro Continuation (\) 55 Stringize (#) 55 Token Pasting (##) 55 defined() Operator 56 PARAMETERIZED MACROS 56 Typedef IN OBJECTIVE-C 57 typedef vs #define 58 TYPE CASTING IN OBJECTIVE-C 59 INTEGER PROMOTION 59 USUAL ARITHMETIC CONVERSION 60 LOG HANDLING IN OBJECTIVE-C 61 NSLog METHOD 61 DISABLING LOGS IN THE LIVE Apps 61 ERROR HANDLING IN OBJECTIVE-C 62 NSError 62 COMMAND-LINE ARGUMENTS 64 BIBLIOGRAPHY 65 Chapter 2 ◾ OOP in Objective-C 67 OBJECT-ORIENTED PROGRAMMING 67 OPERATIONS AND DATA 67 IMPLEMENTATION AND INTERFACE 68 THE OBJECT MODEL 71 THE METAPHOR OF MESSAGING 73 CHARACTERISTIC OF OBJECTIVE-C 75 DEFINITIONS OF OBJECTIVE-C CLASSES 75 ALLOCATING AND INITIALIZING OBJECTIVE-C OBJECTS 76
📄 Page
12
Contents ◾ xi ACCESSING DATA MEMBERS 76 Properties 77 Modularity 78 Reusability 79 INHERITANCE IN OBJECTIVE-C 80 BASE AND DERIVED CLASSES 81 ACCESS THE CONTROL AND INHERITANCE 83 Hierarchies of Class 83 Definitions of Subclass 84 DYNAMISM 85 POLYMORPHISM IN OBJECTIVE-C 85 DATA ENCAPSULATION IN OBJECTIVE-C 88 EXAMPLE OF DATA ENCAPSULATION 90 CREATING A STRATEGY 91 CATEGORIES IN OBJECTIVE-C 91 CATEGORY CHARACTERISTICS 92 POSING IN OBJECTIVE-C 93 POSING RESTRICTIONS 93 EXTENSIONS IN OBJECTIVE-C 94 EXTENSIONS’ CHARACTERISTICS 95 Example of Extensions 95 PROTOCOLS IN OBJECTIVE-C 96 DYNAMIC BINDING IN OBJECTIVE-C 99 COMPOSITE OBJECTS IN OBJECTIVE-C 101 CLASS CLUSTERS 101 WHAT EXACTLY IS A COMPOSITE OBJECT? 101 An Example of a Composite Object 102 FOUNDATION FRAMEWORK IN OBJECTIVE-C 104 Functionality-Based Foundation Classes 105 FAST ENUMERATION IN OBJECTIVE-C 106 COLLECTIONS IN THE OBJECTIVE-C 106 MEMORY MANAGEMENT IN OBJECTIVE-C 107
📄 Page
13
xii ◾ Contents “MANUAL RETAIN-RELEASE” OR MRR 108 Basic MRR Rules 109 “AUTOMATIC REFERENCE COUNTING” OR ARC 110 BIBLIOGRAPHY 112 Chapter 3 ◾ Interface and API 113 iOS IN OBJECTIVE-C 113 IMPLEMENTATION AND INTERFACE 113 OBJECT CREATION 114 METHODS 114 Class Methods 114 Instance Methods 115 IMPORTANT OBJECTIVE-C DATA TYPES 115 Printing Logs 115 CONTROL STRUCTURES 115 PROPERTIES 116 Properties of Accessing 116 CATEGORIES 116 Arrays 116 Dictionary 117 ENVIRONMENT SETUP 117 Installation of Xcode 117 INTERFACE BUILDER 118 SIMULATOR FOR iOS 118 FIRST iPHONE APPLICATION 118 FIRST iOS APPLICATION’S CODE 119 AppDelegate.h 120 AppDelegate.m 120 ViewController.h 123 ViewController.m 123 ACTIONS AND OUTLETS IN iOS 124 DELEGATES IN iOS 125 How to Create a Delegate 126
📄 Page
14
Contents ◾ xiii UI ELEMENTS 128 What Are UI Elements? 128 How Do We Insert UI Elements? 128 Our Focus 128 Our Strategy 128 LIST OF UI ELEMENTS 129 ACCELEROMETER IN iOS 130 UNIVERSAL APPLICATIONS IN iOS 131 CAMERA MANAGEMENT IN iOS 132 LOCATION HANDLING IN iOS 134 SQLite DATABASE IN iOS 137 SENDING EMAIL ON iOS 144 AUDIO AND VIDEO IN iOS 146 FILE HANDLING IN iOS 148 METHODS FOR FILE HANDLING 148 Check to See If a File in Objective-C Exists at a Given Path 148 Comparing the Contents of Two Files 148 Check to See If It Is Writable, Readable, and Executable 149 Move File 149 Copy File 149 Remove File 149 Read File 149 Write File 149 ACCESSING MAPS ON iOS 150 IN-APP PURCHASE IN iOS 152 iAd INTEGRATION IN iOS 158 GameKit IN iOS 159 STORYBOARDS IN iOS 162 AUTO LAYOUTS IN iOS 163 Aim of Our Example 163 Our Strategy 163 The Involved Steps 164
📄 Page
15
xiv ◾ Contents TWITTER AND FACEBOOK ON iOS 167 MEMORY MANAGEMENT IN iOS 170 MEMORY MANAGEMENT CHALLENGES 170 RULES FOR MEMORY MANAGEMENT 170 DEALING WITH MEMORY IN ARC 170 MEMORY MANAGEMENT TOOLS 170 ANALYTICAL METHODS FOR MEMORY ALLOCATIONS 171 APPLICATION DEBUGGING IN iOS 171 CHOOSING A DEBUGGER 171 HOW TO LOCATE CODING ERRORS 171 SET BREAKPOINTS 171 BREAKPOINT EXCEPTION 172 IN AN iOS App, WE MAY USE GOOGLE APIs 172 NOTE 174 BIBLIOGRAPHY 174 Chapter 4 ◾ Functional Programming 177 WHY OBJECT-FUNCTIONAL PROGRAMMING? 178 OBJECTIVE-C FUNCTIONAL PROGRAMMING 178 On Functional Programming 180 On the ObjC Runtime 180 On Objective-C and Language Design 181 WRITE OBJECTIVE-C CODE 181 CLASSES AND OBJECTS 183 METHODS AND COMMUNICATION 185 CLASS METHODS 188 Properties and Accessor Methods Are Declared 188 BLOCKS 190 PROTOCOLS AND CATEGORIES 192 Types and Coding Strategies Are Defined 193 CREATE THE VIDEO App 195 Set the App’s Audio Behavior 195 Build View Controller Class Declaration 198
📄 Page
16
Contents ◾ xv Import the Brightcove Player SDK Header File into the Program 198 Look at the Code 198 Construct the View Controller Implementation in Objective-C 198 Customize the Project to Reflect Our Values 198 Declare Properties 199 DEFINE INITIALIZATION METHOD 200 Setup Player 200 Configure Player 201 Use the Brightcove Library to Request Material 202 Look at the Code 203 NOTE 205 BIBLIOGRAPHY 205 Chapter 5 ◾ Code Management 207 WHY MUST WE PERFORM THIS? 208 ANATOMY OF A FRAMEWORK 209 STATIC AND DYNAMIC FRAMEWORKS 209 ARCHITECTURES AND SLICING OF PROCESSORS 210 DEVELOPING A DYNAMIC STRUCTURE 210 SETTING UP OUR PROJECT 210 DEVELOPING OUR CODE 211 ACCESS CONTROL 211 UMBRELLA HEADER 211 UNIVERSAL SUPPORT 212 UTILIZING OUR DYNAMIC FRAMEWORK 215 DEVELOPING A STATIC FRAMEWORK 215 SETTING UP OUR PROJECT 215 DEVELOPING OUR CODE 215 ACCESS CONTROL 215 UMBRELLA HEADER 216 PACKAGING 216
📄 Page
17
xvi ◾ Contents MODIFY BUILD SETTINGS TO SUPPORT STATIC FRAMEWORKS 216 MODULE SUPPORT 216 CREATING THE BUNDLE STRUCTURE 217 UNIVERSAL SUPPORT 218 UTILIZING OUR STATIC FRAMEWORK 220 RECOMMENDATIONS 220 COMPILING AND CONSTRUCTING THE FRAMEWORK 221 UPLOADING AN APPLICATION’S FRAMEWORK TO THE APP STORE 221 MEMORY MANAGEMENT IN OBJECTIVE-C 221 “MANUAL RETAIN-RELEASE” OR MRR 222 Basic MRR Rules 223 “AUTOMATIC REFERENCE COUNTING” OR ARC 225 Effective Procedures Prevent Memory-Related Issues 226 DEBUG MEMORY ISSUES USING ANALYSIS TOOLS 226 THE GOAL OF MEMORY MANAGEMENT 226 Avoid Crashing 227 Strong vs Weak 227 Atomic and Nonatomic 228 DESIGN PATTERNS IN iOS 228 FAÇADE 229 When to Use the Facade Pattern? 229 An Illustration of Facade Design Pattern 230 DECORATOR 230 When Should We Use a Decorator Pattern? 230 Example of Decorator Style Design 230 MEMENTO 231 ADAPTER 231 When to Use an Adapter? 231 Illustration of Adapter Pattern 231 OBSERVER 231
📄 Page
18
Contents ◾ xvii When Should We Use a Decorator Pattern? 232 Example of Decorator Style Design 232 STRATEGY 232 FACTORY 232 COMMAND 233 COMPOSITE 233 ITERATOR 233 MEDIATOR 233 SINGLETON 233 When Should We Use the Singleton Design Pattern? 234 Illustration of Singleton Pattern 234 MVC 234 MVP 235 MVVM 236 Feature Assessment 236 VIPER 237 WHAT ARE THE ADVANTAGES OF EMPLOYING iOS DESIGN PATTERNS? 237 Prepared to Develop iOS Applications Using iOS Design Patterns 238 BIBLIOGRAPHY 238 Chapter 6 ◾ Code Optimization 239 OBJECTIVE-C CODE OPTIMIZATION AT COMPILE TIME 239 OBJECTIVE-C PIPELINE 240 SECURE CODE 243 Security Breach through HTTPS Response Cache 244 RESUME BACKGROUND DISCLOSURE OF SCREENSHOT DATA 245 SSL PINNING 245 BEST PRACTICES WITH OBJECTIVE-C CODING CONVENTION 250 Operators 250
📄 Page
19
xviii ◾ Contents Types 250 Methods 250 Pragma Mark and Implementation Organization 251 Control Structures 251 Switch 252 For 253 While 253 Import 253 Header Prefix 254 Properties 254 Private Methods and Properties 254 Extern, Const, and Static 255 Naming 255 Enums 256 HARDENING OF SYSTEMS 256 Hardening of Systems to Reduce the “Attack Surface” 256 Advantages of System Hardening 257 NOTE 257 BIBLIOGRAPHY 257 APPRAISAL, 259 OBJECTIVE-C CHEAT SHEET, 305 INDEX, 309
📄 Page
20
xix About the Author Sufyan bin Uzayr is a writer, coder, and entrepreneur with over a decade of experience in the industry. He has authored several books in the past, pertaining to a diverse range of topics, ranging from History to Computers/IT. Sufyan is the Director of Parakozm, a multinational IT company spe- cializing in EdTech solutions. He also runs Zeba Academy, an online learning and teaching vertical with a focus on STEM fields. He special- izes in a wide variety of technologies such as JavaScript, Dart, WordPress, Drupal, Linux, and Python. He holds multiple degrees including ones in Management, IT, Literature, and Political Science. Sufyan is a digital nomad, dividing his time between four countries. He has lived and taught in numerous universities and educational institu- tions around the globe. Sufyan takes a keen interest in technology, politics, literature, history, and sports, and in his spare time, he enjoys teaching coding and English to young students. Learn more at sufyanism.com