ebook img

Objective-C Programmer's Reference PDF

375 Pages·2013·5.16 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Objective-C Programmer's Reference

www.it-ebooks.info For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. www.it-ebooks.info Contents at a Glance About the Author ��������������������������������������������������������������������������������������������������������������xvii About the Technical Reviewer �������������������������������������������������������������������������������������������xix Acknowledgments �������������������������������������������������������������������������������������������������������������xxi Introduction ���������������������������������������������������������������������������������������������������������������������xxiii ■ Part 1: The Language ������������������������������������������������������������������������������������1 ■ Chapter 1: The C in Objective-C �����������������������������������������������������������������������������������������3 ■ Chapter 2: Classes �����������������������������������������������������������������������������������������������������������29 ■ Chapter 3: Strings and Container Classes �����������������������������������������������������������������������53 ■ Chapter 4: Categories and Protocols �������������������������������������������������������������������������������75 ■ Chapter 5: Inheritance �����������������������������������������������������������������������������������������������������91 ■ Chapter 6: Blocks ����������������������������������������������������������������������������������������������������������113 ■ Chapter 7: Dynamic Binding ������������������������������������������������������������������������������������������127 ■ Chapter 8: Memory Management ����������������������������������������������������������������������������������149 ■ Chapter 9: Key-Value Coding �����������������������������������������������������������������������������������������173 ■ Chapter 10: The File System ������������������������������������������������������������������������������������������193 v www.it-ebooks.info vi Contents at a Glance ■ Part 2: Reference ��������������������������������������������������������������������������������������207 ■ Chapter 11: The Foundation Framework �����������������������������������������������������������������������209 ■ Part 3: The Tools ���������������������������������������������������������������������������������������239 ■ Chapter 12: The Compiler ����������������������������������������������������������������������������������������������241 ■ Chapter 13: The Preprocessor ���������������������������������������������������������������������������������������255 ■ Chapter 14: Unit Testing ������������������������������������������������������������������������������������������������267 ■ Chapter 15: Debugging ��������������������������������������������������������������������������������������������������281 ■ Part 4: Writing Apps for OS X and iOS ������������������������������������������������������295 ■ Chapter 16: Building OS X GUI Applications ������������������������������������������������������������������297 ■ Chapter 17: Building iOS Apps ���������������������������������������������������������������������������������������329 Index ���������������������������������������������������������������������������������������������������������������������������������351 www.it-ebooks.info Introduction Welcome to Objective-C Programmer’s Reference. This is a book targeted at developers who have prior experience with other languages such as Java, Python, Ruby, or C++ and would like to leverage that knowledge into the Objective-C world. Maybe you have played with the iPhone and would like to know how to develop that app that you wish were available on the AppStore. Or maybe you want to create a new desktop application in Mac OS X. Still, you may want to program in Objective-C just because you need to support iOS devices at your company. Whatever the reason, this book will help you to move from knowing concepts of programming in other languages to learning how to and (I hope) enjoying programing in Objective-C. This book is also written with the goal of reaching readers who already know Objective-C but would like a concise reference to the most commonly used techniques in the language. You will be able to understand topics that are of real importance when working with Cocoa and Cocoa Touch, the two largest and most important frameworks in Objective-C. You will also learn fundamental topics that can be leveraged into any programming situation where Objective-C is needed. You may not have the time to invest in more traditional books, which usually spend hundreds of pages to explain simple concepts that are already part of the standard vocabulary of working developers and advanced programming students. That’s why this book tries to convey only the most important topics with concise examples. We also include a quick reference to the most commonly used classes, so you don’t need to spend too much time looking at these documents online. Objective-C Programer’s Reference is an excellent choice for developers that want to use their knowledge effectively, while learning one of the most sought after and marketable skill sets for modern application and mobile development. Topics Discussed Objective-C Programmer’s Reference provides all the tools necessary to build working solutions in Objective-C. Starting from the basic building blocks of the C language, I discuss how to create correct and efficient applications, leveraging knowledge of object-oriented programming as well as structured programming. xxiii www.it-ebooks.info xxiv Introduction I make extensive use of concepts mastered by developers who are fluent in other languages such as C++, Java, Perl, and Python. In writing this book, my goal is to provide such readers a fast-paced path to achieve proficiency in Objective-C. On the other hand, this book introduces Objective-C in a logical and structured way, so that even novice programmers will have an easy time absorbing the most important topics and competencies necessary to develop applications that use the language. We work in an industry where typical books are thousands of pages long and where most of that length is filled up by introductory topics that are not of great concern for professional developers. In this book, I strive to provide a more concise and focused reference to the language, offering readers much more value for their reading efforts. This book does the following: • Takes you quickly through fundamental concepts such as interfaces and class implementations in Objective-C. • Provides a concise and handy reference to the Foundation framework that is all-important when programming in Objective-C. • Highlights key differences between Objective-C and other popular languages such as Java or Python. • Shows how to create full applications using advanced concepts such as container classes, selectors, and key-value interfaces. • Provides the fundamentals of writing Cocoa and Cocoa Touch applications, which are the standard for Mac OS X and iOS development. What You Will Learn Here are some of the topics that you will learn in this book: • The basic syntax of the Objective-C language. • How to create classes in Objective-C. • How methods and the message passing mechanism work to simplify your code and avoid deep class hierarchies. • The key-value system for storing and accessing dynamic data, and how it ties with Objective-C interfaces to simplify application programming. • The effective use of container classes in Objective-C, such as arrays and dictionaries, with their immutable and mutable versions. • Basic topics in Cocoa programming, which can be used to create simple applications in the Mac OS X and iOS environments. Here is a quick summary of the topics you will learn in each chapter. www.it-ebooks.info Introduction xxv Chapter 1: The C in Objective-C The Objective-C language was created as an extension of C, which means that any program written in C is also a valid Objective-C program. As a result, many of the basic expressions in Objective-C are identical to their equivalents in C. To become a good Objective-C programmer, you need to have a basic understanding of the C language. In this chapter, I provide a quick introduction to the most important concepts from the C language as used in Objective-C. Chapter 2: Classes In Objective-C, classes are the building blocks of applications. They are the syntactic construct used to define the methods and variables that become the components of an object. This chapter gives an introduction to the concept of Objective-C classes, and how to define them. One of the main features of classes is how they can be described in separate files, one for the interface and another for the implementation. This clear separation makes it possible to share only the important aspects of a class, while hiding any implementation information that is not needed by clients. Chapter 3: Strings and Container Classes One of the main characteristics of Objective-C programs is the constant use of dynamic objects to simplify the management of memory and other resources. Among the most fundamental classes in the Objective-C runtime are the classes used to handle strings, numbers, and collections of objects. In this chapter, I show how to use these basic classes and their methods. Chapter 4: Protocols and Categories Objective-C promotes a clear separation between implementation and interfaces. One of the techniques used to encourage this separation is the concept of protocols. A protocol allows classes to communicate by just describing a subset of the methods they respond to. Another way to promote this separation of concerns is to extend classes through categories: instead of making everyone aware of the methods available at a particular class, a programmer can add categories as they are needed. This results in a much cleaner implementation, which reduces dependencies on other parts of the application. Chapter 5: Inheritance Object-oriented programming is the main idea that motivated the creation of Objective-C. An important aspect of OOP is the ability to create new classes that inherit the methods, variables, and properties of existing classes. The proper use of inheritance, however, requires a certain care in both the design phase and the proper implementation of derived classes. In this chapter, I cover the main ideas that you need to be familiar with when creating class hierarchies. Chapter 6: Block Syntax Objective-C provides another technique to reduce programming effort through its block syntax. A block is a piece of code that can be passed around to other parts of the application. A block not only retains information about the code but also about variables that were in scope at the time the block was created. In this way, blocks are similar to the concept of closures available in other languages. By defining blocks, you can make the implementation of certain algorithms more extensible. You can also use blocks to implement code that will run in other threads, as accomplished by some Objective-C libraries. www.it-ebooks.info xxvi Introduction Chapter 7: Dynamic Binding One of the biggest advantages of Objective-C is that it allows programmers to choose the right combination of speed and flexibility, depending on their needs. The runtime system of Objective-C is the main mechanism that can be used by programmers to switch between dynamic and static programming. In this chapter, you learn how to explore the runtime system to achieve the best combination of static checking and dynamic flexibility in your software. Chapter 8: Memory Management One of the advantages of working with an object-oriented language with dynamic features is that memory management can be simplified, with system libraries doing most of the hard work. Objective-C uses a reference-based model for memory management, with rules that make it almost straightforward to allocate and release memory in any application. In this chapter, I review the simple rules that you need to master in order to write correct objective-C code. I also cover the latest techniques provided by Apple compilers, which practically remove the need for manual intervention in their memory management mechanisms. Chapter 9: Key-Value Programming A very common way to interact with objects in Objective-C is to set values for particular property names, also referred to as keys. Key-value programming is very important because it is used in so many libraries in Objective-C. It allows an object to be used through a simple interface, avoiding the need to create multiple subclasses when the only difference between objects is the set of values they contain. Chapter 10: The Filesystem Objective-C libraries provide a simplified mechanism for accessing system resources. One example is how the Foundation framework can be used to access files and directories. In this chapter, I show how the classes in this standard Objective-C framework can be used to create files, read and write data to existing files, and manipulate the content of directories. Chapter 11: The Foundation Framework Classes in Objective-C are organized into frameworks for ease of distribution and use. The Foundation framework provides classes needed to write programs interacting with the environment. The basic utility classes, such as strings, dictionaries, and arrays, are also contained in the Foundation framework. In this chapter, I provide a reference to the Foundation framework classes, with examples when necessary. Chapter 12: The Compiler To use Objective-C effectively, it is important to understand the basic infrastructure of the language. The compiler plays a special role in the software development cycle, since it determines how source code is translated into executable programs. In the Apple environment, two main compilers are used: gcc, the traditional compiler used by Mac OS X, and the new compiler based on the LLVM (Lower Level Virtual Machine), which has been developed by Apple in the last few years. In this chapter, I take a look at several options provided by the Objective-C compilers and how to use them to write fast and reliable code. www.it-ebooks.info Introduction xxvii Chapter 13: The Preprocessor Objective-C comes with a powerful preprocessor that can simplify the input of repetitive code into a program. The preprocessor, however, may be the source of numerous programming pitfalls if you are not careful with its usage. In this chapter, I present the main features of the preprocessor and how to avoid some of its rough edges. Chapter 14: Unit Tests Test-driven development is a software methodology that requires that every feature of a program be thoroughly tested with unit testing code. Test-driven development is a great way to employ the modularity of object-oriented languages such as Objective-C. In this chapter, you learn how to create unit tests in the Objective-C language. You also learn how to manage and run unit tests using the Xcode IDE. Chapter 15: Debugging Any programmer knows how difficult it is to write defect-free software. That is why every platform has a number of integrated debugging aids. Xcode provides a complete solution for writing and debugging programs, which makes it much easier to find bugs in a new or existing application. In this chapter, I explain how to use these debugging features. Using a tutorial style, I describe how to use breakpoints, watch windows, conditional expressions, and the logging terminal. Chapter 16: Building a OS X Application in Cocoa The most important use of Objective-C is in the implementation of applications that employ the Cocoa frameworks. As an Objective-C programmer, your may become involved in the creation of iOS apps or full Mac OS X applications. In this chapter, I introduce Cocoa, the framework used to develop desktop applications in the Mac OS X. Chapter 17: Building an iOS App on Cocoa Touch Cocoa Touch is the framework used to create rich applications for iOS, the operating system that powers the iPhone, iPad, and iPod touch. While using technologies similar to Cocoa, iOS provides a simplified architecture in comparison to the full-featured Cocoa framework. Cocoa Touch also provides access to mobile resources such as cameras, the accelerometer, and other hardware managed by iOS. Compiling the Code Samples The examples given in this book have all been compiled on OS X using the freely available Xcode IDE. To use Xcode you need to have a Mac OS X machine. If you don’t have Xcode installed in your computer, you can download it from Apple’s developer web site at http://developer.apple.com Another way to install Xcode is to use the App Store application on your Mac. Search for Xcode and click to install the app. The next time you start Xcode, create a new project by clicking File ➤ New ➤ Project. This will bring up a wizard window where you can give some basic options for the new project, such as its location, name, and target operating system (use Mac OS X as the target). With the new project, you can create files and enter the examples as displayed in the book. To download the complete set of examples included here, visit the web page for this book at http://coliveira.net or www.apress.com. www.it-ebooks.info 1 Part The Language 1 www.it-ebooks.info

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.