Objective-C 2.0 Essentials Third Edition
Total Page:16
File Type:pdf, Size:1020Kb
Objective-C 2.0 Essentials Third Edition i Objective-C 2.0 Essentials – Third Edition ISBN-13: 978-1480262102 © 2012 Neil Smyth. This book is provided for personal use only. Unauthorized use, reproduction and/or distribution strictly prohibited. All rights reserved. The content of this book is provided for informational purposes only. Neither the publisher nor the author offers any warranties or representation, express or implied, with regard to the accuracy of information contained in this book, nor do they accept any liability for any loss or damage arising from any errors or omissions. This book contains trademarked terms that are used solely for editorial purposes and to the benefit of the respective trademark owner. The terms used within this book are not intended as infringement of any trademarks. Find more eBooks online at http://www.eBookFrenzy.com. Rev. 3.0 ii Table of Contents 1. About Objective-C Essentials ...................................................................................................... 1 1.1 Why are you reading this? .................................................................................................... 1 1.2 Supported Platforms ............................................................................................................. 2 2. The History of Objective-C .......................................................................................................... 3 2.1 The C Programming Language .............................................................................................. 3 2.2 The Smalltalk programming Language .................................................................................. 3 2.3 C meets Smalltalk .................................................................................................................. 4 2.4 Objective-C and Apple ........................................................................................................... 4 2.5 Modern Objective-C .............................................................................................................. 5 3. Installing Xcode and Compiling Objective-C on Mac OS X .......................................................... 7 3.1 Identifying if you have an Intel or PowerPC based Mac ....................................................... 7 3.2 Installing the Xcode Development Environment .................................................................. 8 3.3 Starting Xcode ....................................................................................................................... 8 3.4 Writing an Objective-C Application with Xcode .................................................................. 11 3.5 Compiling Objective-C from the Command Line ................................................................ 12 3.6 Summary ............................................................................................................................. 14 4. Objective-C 2.0 Data Types ....................................................................................................... 15 4.1 int Data Type ....................................................................................................................... 16 4.2 char Data Type .................................................................................................................... 17 4.2.1 Special Characters/Escape Sequences ......................................................................... 17 4.3 float Data Type .................................................................................................................... 18 4.4 double Data Type ................................................................................................................ 18 4.5 id Data Type ......................................................................................................................... 18 4.6 BOOL Data Type .................................................................................................................. 18 4.7 Objective-C Data Type Qualifiers ........................................................................................ 19 4.7.1 long ............................................................................................................................... 19 4.7.2 long long ....................................................................................................................... 19 iii 4.7.3 short .............................................................................................................................. 19 4.7.1 signed / unsigned.......................................................................................................... 20 4.8 Summary ............................................................................................................................. 20 5. Working with Variables and Constants in Objective-C ............................................................. 21 5.1 What is an Objective-C Variable .......................................................................................... 21 5.2 What is an Objective-C Constant? ....................................................................................... 22 5.3 Type Casting Objective-C Variables ..................................................................................... 23 5.4 Summary ............................................................................................................................. 24 6. Objective-C Operators and Expressions ................................................................................... 25 6.1 What is an Expression? ....................................................................................................... 25 6.2 The Basic Assignment Operator .......................................................................................... 25 6.3 Objective-C Arithmetic Operators ....................................................................................... 26 6.4 Compound Assignment Operators ...................................................................................... 27 6.5 Increment and Decrement Operators ................................................................................. 27 6.6 Comparison Operators ........................................................................................................ 28 6.7 Boolean Logical Operators .................................................................................................. 29 6.8 The Ternary Operator .......................................................................................................... 30 6.9 Bitwise Operators ................................................................................................................ 30 6.9.1 Bitwise AND .................................................................................................................. 31 6.9.2 Bitwise OR ..................................................................................................................... 32 6.9.3 Bitwise XOR ................................................................................................................... 32 6.9.4 Bitwise Left Shift ........................................................................................................... 33 6.9.5 Bitwise Right Shift ......................................................................................................... 33 6.10 Compound Bitwise Operators ........................................................................................... 34 6.11 Summary ........................................................................................................................... 34 7. Objective-C 2.0 Operator Precedence ...................................................................................... 35 7.1 An Example of Objective-C Operator Precedence .............................................................. 35 7.2 Objective-C Operator Precedence and Associativity .......................................................... 35 iv 7.3 Overriding Operator Precedence ........................................................................................ 37 7.4 Summary ............................................................................................................................. 38 8. Commenting Objective-C Code ................................................................................................. 39 8.1 Why Comment your Code? ................................................................................................. 39 8.2 Single Line Comments ......................................................................................................... 40 8.3 Multi-line Comments .......................................................................................................... 40 8.4 Summary ............................................................................................................................. 41 9. Objective-C Flow Control with if and else ................................................................................. 43 9.1 Using the if Statement......................................................................................................... 43 9.2 Using if ... else … Statements .............................................................................................. 44 9.3 Using if ... else if ... Statements ..........................................................................................