Introduction

If you want to develop native apps for OS X or for iOS, the language in which you write them is Objective-C. (You can write web-based apps using Safari extensions and HTML5.)

Because Apple’s development environment builds on the frameworks of Cocoa and Cocoa Touch, what you have to write is the code that is specific to your own app. All the basic functionality is provided by Cocoa and the frameworks; you do not have to write any code to manage menus, for example (in most cases), and you don’t have to worry about implementing complex view structures on iOS such as navigation bars and split views. The code that you write is very specific code that fits into the existing frameworks. It might be code that overrides or extends an existing method, or it might be new methods that are unique to your own app.

Who Should Read This Book

Even though the code that you write might not be extensive, you do have to understand the syntax and structure of Objective-C in order to write it and to understand the code that Apple provides for you.

You should have some basic experience with programming in order to read this book, but it is designed to bring you up to speed on the major prerequisites to understanding Objective-C, including the concepts of object-oriented programming. The book assumes that you have at least a basic understanding of the C programming language on which Objective-C is based. Don’t worry if you are far from a C expert; the concepts used in Objective-C are the basics.

What This Book Covers

Objective-C has been the language of choice for the Cocoa environments and, before them, the NeXTSTEP and Rhapsody environments. Theoretically, it can be used in other contexts, but in practice, it is the language of these environments. Sometimes, drawing the line between Objective-C syntax and elements of the Cocoa environments and frameworks can be very difficult. This book focuses on the language itself. It provides a number of examples from code in the Cocoa frameworks as well as code in the examples from developer.apple.com and the templates built in to Xcode, but the primary focus is on the language. The book includes information on how to use the Xcode development tool because, for all intents and purposes, you have to use it to write Objective-C code.

Downloading the Sample Files

You can download examples from the author’s website at northcountryconsulting.com or from the publisher’s site at informit.com/title/9780672334498.

How This Book Is Organized

There are five parts to this book. You can focus on whichever one addresses an immediate problem, or you can get a good overview by reading straight through. As with all of the Sams Teach Yourself books, as much as possible each hour is made to stand on its own so that you can jump around to learn in your own way. Cross-references throughout the book help you find related material.

Part I: Getting Started with Objective-C

This part gives you the high-level view of Objective-C.

Image Hour 1, “Overview of the Developer Program—Here you see how to register to become an Apple developer and gain access to the resources on developer.apple.com. You can choose from various options, but you must register before you can do any meaningful development work.

Image Hour 2, “Object-Oriented Programming with Objective-C—This hour shows you how Objective-C builds on top of C. It also covers how Objective-C implements object-oriented concepts and manages inheritance.

Image Hour 3, “Using Object-Oriented Features in Objective-C—Objective-C is a dynamic language that relies on messaging. Those concepts are explained here so that you can begin to implement your own apps.

Image Hour 4, “Using Xcode 5—This is the integrated development environment (IDE) you use to develop apps. You’ll see how to work with new features that make your coding faster and catches errors even before you build your app. Xcode integrates the Git source code repository so that you can easily track your code on your own Mac, a networked Mac, or a public repository such as GitHub.

Image Hour 5, “Using Compiler Directives—Compiler directives help you manage files and use the preprocessor to prepare code for the compiler itself.

Part II: Working with the Objective-C Basics

This part of the book delves into the concepts of messaging, classes, selectors, building blocks, and memory.

Image Hour 6, “Exploring Messaging and a Testbed App—Messaging is the heart of Objective-C. You send messages rather than call methods or functions, and the difference between Objective-C and other languages is significant.

Image Hour 7, “Declaring a Class in an Interface File—This hour explores the basics of declaring a class. You see how to import other files and use forward declarations for classes and protocols.

Image Hour 8, “Declaring Instance Variables in an Interface File—You can assign instance variables to classes. You also see how to deal with static typing and the scope of instance variables.

Image Hour 9, “Declaring Properties in an Interface File—Objective-C lets you declare properties of a class in your interface file rather than declaring variables. The property declaration provides more features than a simple C variable declaration. Furthermore, the compiler can synthesize the accessors for the property based on the property declaration.

Image Hour 10, “Declaring Methods in an Interface File—Method declarations in Objective-C provide the same functionality that they do in other languages, but the details in this message-based environment are different.

Image Hour 11, “Declaring Actions in an Interface File—Actions are typically triggered by user actions in the interface. You declare them in your interface files and manage their interactions with Interface Builder.

Image Hour 12, “Routing Messages with Selectors—The target-action pattern is the basis for message routing; it is explained in this hour. You also see how to use selectors and the SEL data type.

Image Hour 13, “Building on the Foundation—Throughout Objective-C, you find certain patterns repeated over and over. These include its data types and the concept of mutable and immutable versions of them.

Image Hour 14, “Defining a Class in an Implementation File—After all those declarations, you get down to actually defining the code behind the declarations in this hour.

Image Hour 15, “Organizing Data with Collections—Arrays are one of the core features of programming languages. Objective-C adds additional collection objects, including dictionaries and sets. This hour covers how to use them.

Image Hour 16, “Managing Memory and Runtime Objects—Whether you are running on a mainframe or supercomputer or a relatively small mobile device, memory is a critical resource. There are many tools to help you manage memory, but in most cases, you still need to do something and understand what your choices are. This hour introduces you to Automatic Reference Counting (ARC), the modern way to manage Objective-C memory; it also shows you how garbage collection has been used in the past.

Part III: Expanding and Extending Classes

One of the differences between Objective-C and other object-oriented languages is that you can expand and extend classes in a variety of ways and not just by subclassing them.

Image Hour 17, “Extending a Class with Protocols and Delegates—Next to the ability to override classes, protocols and delegates are the most commonly used way for adding functionality to a variety of classes.

Image Hour 18, “Extending a Class with Categories and Extensions—Categories and extensions let you add methods and even variables to existing classes—even those for which you might not have the source code.

Image Hour 19, “Using Associative References and Fast Enumeration—These relatively recent additions to Objective-C enable you to become a power programmer. You can add and remove associations at runtime, which gives you more flexibility with your instance variables. Fast enumeration also speeds your runtime performance and might mean that you have less code to write yourself.

Image Hour 20, “Working with Blocks—Blocks provide the ability to create and use sections of code that are portable and anonymous. You can use them as arguments of methods or functions, typically as completion handlers for completion routines, handlers, and errors, as well as for specific types of functionality, such as sorting and view manipulation. The concept is borrowed from languages such as C, Ruby, Python, and Lisp; it has been part of Objective-C since Mac OS X 10.6 (Snow Leopard) and iOS 4.0.

Part IV: Beyond the Basics

Exception and error handling, queues and threading, the debugger, and Xcode debug gauges allow you to analyze and improve your projects and their performance.

Image Hour 21, “Handling Exceptions and Errors—As in many languages, you can set up try/catch/finally structures to catch exceptions as close as possible to the place where they happen. In this hour, you see how to use them and how to integrate them with the debugger.

Image Hour 22, “Grand Central Dispatch: Using Queues and Threading—Even on small mobile devices, you have the ability to manage multiprocessing. This hour shows you the techniques that are available for multiprocessing.

Image Hour 23, “Working with the Debugger—During development with Xcode, you can use the debugger and console to improve your code. This hour shows you the basics and explores break points, preferences, and some advanced debugging techniques.

Image Hour 24, “Using Xcode Debug Gauges for Analysis—Although debugging can help you get rid of bugs, you need to monitor your app’s performance. Memory leaks aren’t bugs unless they crash your app, but with these tools you can spot them before the damage is done. Debug gauges are a simple graphical interface on top of the more sophisticated Instruments app; both are discussed here.

Part V: Appendixes

The appendixes provide additional reference material that can help you with the concepts in the book. There is even more material on developer.apple.com and on the author’s website at northcountryconsulting.com.

Image Appendix A, “C Syntax Summary—Objective-C is a thin layer built on top of C. This appendix reviews the basic C syntax that matters for Objective-C. Much of it is reimplemented for you in the frameworks of Cocoa, so this appendix walks you through what is left.

Image Appendix B, “Apps, Packages, and Bundles—Your app on Mac OS X or iOS is actually a collection of files, including code, resources such as data stores and images, and property lists. This is a high-level overview so that you can understand what it is you are building with Xcode and Objective-C.

Image Appendix C, “Archiving and Packaging Apps for Development and Testing—This overview gives you the basic information you need to know to share your apps with testers and through the App Store. It is not just a matter of copying files.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.160.43