Effective Objective-C 2.0: Item 47: Familiarize Yourself with the System Frameworks

来源:互联网 发布:信捷plc编程手册 编辑:程序博客网 时间:2024/06/11 08:50

Item 47: Familiarize Yourself with the System Frameworks

When writing an application in Objective-C, you will almost certainly use the system frameworks, which provide many of the common classes, such as collections, that you need to be able to write applications. If you don’t understand what the system frameworks provide, you may end up writing something that has already been written. When they upgrade their operating systems, users of your application obtain the latest versions of the system frameworks. So if you use classes from these frameworks, you benefit from any improvements made to them, without having to update your application.

A framework is a set of code packaged as a dynamic library along with header files describing its interface. Sometimes, a third-party framework built for iOS uses a static library, since iOS applications are not allowed to ship dynamic libraries with them. These are not true frameworks but are often referred to as such. However, all system frameworks use dynamic libraries on iOS still.

If you’re developing a graphical application for Mac OS X or iOS, you will come across the framework called Cocoa, which is also referred to as Cocoa Touch for iOS. Cocoa is not a framework in itself but rather a collection of other frameworks commonly used when creating applications.

The main framework you will come across is called Foundation, where classes such as NSObjectNSArray, and NSDictionary are found. The class prefix used throughout the Foundation framework is NS, which was first formulated when Objective-C was being used for work on the NeXTSTEP operating system. The Foundation framework truly is the foundation of all Objective-C applications; without it, most of this book would be irrelevant.

Foundation provides not only core features that you would expect, such as collections, but also complex features, such as string processing. For example, NSLinguisticTagger provides the ability to parse a string and find all the nouns, verbs, pronouns, and so on. In short, Foundation goes far beyond the basics.

Alongside Foundation is another framework: CoreFoundation. Although not technically Objective-C, CoreFoundation is another important framework to be familiar with when writing Objective-C applications and is a C API that mirrors much of the functionality of the Foundation framework. The CoreFoundation and Foundation frameworks are closely linked in more than name. A feature known as toll-free bridging allows seamless casting from the C data structures of CoreFoundation to the Objective-C objects of Foundation, and vice versa. For example, a string from Foundation is anNSString, which can be cast to its equivalent in CoreFoundation:CFString. Toll-free bridging works through some rather intricate code that makes CoreFoundation objects appear to the runtime as though they are Objective-C objects. Unfortunately, toll-free bridging is very complex, so replicating it yourself in your own code is tricky. This feature should be used, but copied only if you really know what you’re doing.

Along with Foundation and CoreFoundation are many other system libraries. They include but are not limited to the following:

Image CFNetwork This provides C-level networking facilities for talking to networks through an easy-to-use abstraction over BSD sockets. Foundation wraps parts of this framework to provide an Objective-C interface for networking, such as NSURLConnection for downloading data from a URL.

Image CoreAudio This provides a C-level API for interfacing with audio hardware on a device. This is one of the harder frameworks to work with, owing to the complex nature of audio processing. Fortunately, Objective-C abstractions can be used make audio processing easier.

Image AVFoundation This provides Objective-C objects for dealing with audio and video playback and recording, such as UI view classes for presenting video.

Image CoreData This provides Objective-C interfaces for persisting objects to a database. CoreData handles data fetching and saving and can be used cross-platform between Mac OS X and iOS.

Image CoreText This provides a C interface for high-performance text typesetting and rendering.

Other frameworks are available, but looking at the few listed here highlights an important feature of programming in Objective-C: Often, you will need to drop down to use C-level APIs. APIs written in C benefit from the speed improvement of bypassing the Objective-C runtime. Of course, more care needs to be taken with memory management in those APIs, since ARC (see Item 30) is available only to Objective-C objects. Being familiar with the basics of C is important if you need to use one of these frameworks.

You will likely be writing Mac OS X or iOS applications that make use of the UI frameworks. The core UI frameworks, called AppKit and UIKit, respectively, both provide Objective-C classes built on top of Foundation and CoreFoundation. They provide UI elements and the glue to put everything together to create an application. Underneath these main UI frameworks are the CoreAnimation and CoreGraphics frameworks.

CoreAnimation is written in Objective-C and provides the tools that the UI frameworks use to render graphics and perform animations. You may never need to drop down to this level, but it is good to know that it is there. CoreAnimation is not a framework on its own but rather is part of the QuartzCore framework. However, CoreAnimation should still be considered as a first-class citizen.

CoreGraphics is written in C and provides data structures and functions that are essential for 2D rendering. For example, this is where the CGPoint,CGSize, and CGRect data structures are defined, and all are used by the UIKit class UIView to indicate where views should be positioned relative to one another.

Many other frameworks are built on top of the UI frameworks, such asMapKit, which provides mapping functionality to iOS, or the Social framework, which provides social networking facilities to both Mac OS X and iOS. You will usually work with these frameworks and the core UI framework for the platform on which you are working.

Overall, many frameworks come as standard with Mac OS X and iOS installations. So, if you need to write a new utility class, for example, consider searching for it first in the system frameworks. Often, it will have already been written for you.

Things to Remember

Image Many system frameworks are available to you. The most important ones, Foundation and CoreFoundation, provide the core functionality on which much of an application is built.

Image Frameworks exist for many common tasks, such as audio and video processing, networking, and data managing.

Image Remember that frameworks written in pure C rather than Objective-C are just as important to you, so to be a good Objective-C developer, you should understand the core concepts of C.

0 0
原创粉丝点击