Objective-C Crash Course

Click here to load reader

  • date post

    10-Apr-2018
  • Category

    Documents

  • view

    215
  • download

    0

Embed Size (px)

Transcript of Objective-C Crash Course

  • 8/8/2019 Objective-C Crash Course

    1/33

    1 The Objective-C Crash Course

  • 8/8/2019 Objective-C Crash Course

    2/33

    Legal Notices

    This publication is protected under the US Copyright Act of 1976 and all

    other applicable international, federal, state and local laws, and all rights are

    reserved, including resale rights: you are not allowed to give or sell this

    book to anyone else. If you received this publication from anyone other than

    How to Make iPhone Apps or App Shop, LLC, you've received a pirated copy.

    Please contact us via e-mail at support@app-shop.com and notify us of the

    situation.

    Please note that much of this publication is based on personal experience

    and anecdotal evidence. Although the author and publisher have made every

    reasonable attempt to achieve complete accuracy of the content in this book,

    they assume no responsibility for errors or omissions. Also, you should use

    this information as you see fit, and at your own risk. Your particular situation

    may not be exactly suited to the examples illustrated here; in fact, it's likely

    that they won't be the same, and you should adjust your use of the

    information and recommendations accordingly.

    Any trademarks service marks, product names or named features are

    assumed to be the property of their respective owners, and are used only for

    reference. There is no implied endorsement if we use one of these terms.

    iPhone, iPod and iTunes are trademarks of Apple Inc., registered in the

    U.S. and other countries.

    Finally, use your head. Nothing in this book is intended to replace common

    sense, legal, medical, metaphysical or other professional advice, and is

    meant to inform and entertain the reader. So have fun with The Objective-C

    Crash Course, and get your stuff done.

    Copyright 2009 App Shop, LLC. All rights reserved worldwide.

    2 The Objective-C Crash Course

    mailto:support@app-shop.commailto:support@app-shop.commailto:support@app-shop.com
  • 8/8/2019 Objective-C Crash Course

    3/33

    ......................................................................Introduction 6

    ..............................................................................A Note About NSLog 6

    ................................................How to Use the Included Source Code 7

    ......................................................................................Patterns of Use 8

    .......................................................................Methods and Messaging 9

    .................................................................................Types vs. Objects 10

    .......................................................Objective-C How To 12

    ..............................................................................How to Use Strings 12

    ......................................................................................................Using strings 12

    ...............................................................Inserting a string into another string 12

    .......................................................................Inserting a number into a string 12

    ......................................Inserting a string and a number into another string 12

    ...........................................................................How to Use Numbers 13

    ....................................................................................................Using integers

    13

    ....................................................................................................Using doubles 13

    ................................................................................................Doing arithmetic 13

    .................................................................................................Using NSInteger 13

    ..............................................................................................Using NSUInteger 13

    ..............................................................................Using the NSNumber Class 13

    ..................................How to Use Objects, Properties and Methods 14

    ........................................................................................Using a class method 14

    ....................................................................Instantiate an object from a class 14

    ...........Send a message to an object to evoke a method (calling a method) 14

    3 The Objective-C Crash Course

  • 8/8/2019 Objective-C Crash Course

    4/33

    ..............................................Send a message to an object with a parameter 14

    ........................................Send a message to an object with two parameters 14

    .............................................Using a value returned from an object function 14

    ...............................................Assign a value to a property using dot syntax 14

    ................................................Accessing a property value using dot syntax 15

    ...........................................................Release an objects memory allocation 15

    ...............................................................................How to Use Arrays 16

    ............................................................................................Instantiate an array 16

    ..................................................................................Add elements to an array 16

    ...................................................................Retrieve an element from an array 16

    ..................................................................Retrieve the last object in an array 16

    ..........................................................Move through each element in an array 16

    ......................................................................How to Use Dictionaries 17

    ......................................................................................Instantiate a dictionary 17

    ..................................................Add objects to a dictionary indexed by keys 17

    .............................................Retrieve an object from a dictionary with a key 17

    ..........................................................................................Release a dictionary 17

    ..........................................................How to Use If-Then Statements 18

    ........................How to Use Switch Statements or Case Statements 18

    ................................................................................How to Use Loops 19

    ..................................................................................................Using for loops 19

    ..............................................Working with Classes and Subclasses 21

    ...................................................How to Add Properties to a Class Definition 22

    ..............................................................................How to Add a Class Method 23

    4 The Objective-C Crash Course

  • 8/8/2019 Objective-C Crash Course

    5/33

    .......................................................................How to Add an Instance Method 24

    ..........................................................How to Add a Method with a Parameter 25

    ..............................................................Complete Code for a Class Definition 27

    .................................................................................Interface File (myClass.h) 27

    .....................................................................Implementation File (myClass.m) 28

    ...........................................................................Memory Management 29

    ...........................................................................................Reference Counting 29

    ......................................................................How Reference Counting Works 30

    ........................................................................................................Autorelease 31

    ....................................................................................How to Use Autorelease 31

    .........................................................................Memory Management Pointers 32

    .............................................................................................Final Word 33

    5 The Objective-C Crash Course

  • 8/8/2019 Objective-C Crash Course

    6/33

    Introduction

    Objective-C is the primary programming language used in iPhone

    programming. Objective-C is similar to other programming languages; it

    has loops, if-then statements, objects and arrays. However, the syntax is

    different than many other languages.

    In order to help you get over the disorientation of jumping into working with

    the iPhone this crash course will show you the Objective-C approaches to

    using common programming methods like looping and if-then logic.

    This document will get you up to speed with the tasks that you are used to

    as a programmer. However, if you do not have much experience as a

    programmer and would like an introduction to programming concepts and a

    deeper knowledge of Objective-C then I recommend the book Learn

    Objective-C on the Mac.

    A Note About NSLog

    In the examples here, the NSLog function is used to demonstrate snippets of

    code. For example,

    NSLog(@Write this to log);

    This function writes whatever string is presented between the parentheses tothe log window. Click on this button in XCode (this only wo