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 NSObject
, NSArray,
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:
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.
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.
AVFoundation This provides Objective-C objects for dealing with audio and video playback and recording, such as UI view classes for presenting video.
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.
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
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.
Frameworks exist for many common tasks, such as audio and video processing, networking, and data managing.
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.
- Effective Objective-C 2.0: Item 47: Familiarize Yourself with the System Frameworks
- Effective Objective-C 2.0: Item 17: Implement the description Method
- Effective Objective-C 2.0: Item 22: Understand the NSCopying Protocol
- Effective Objective-C 2.0: Item 21: Understand the Objective-C Error Model
- Effective Objective-C 2.0: Item 32: Beware of Memory Management with Exception-Safe Code
- 条款54:让自己熟悉包括TR1在内的标准程序库(Familiarize yourself with the standard library,including TR1.)
- Effective Objective-C 2.0: Item 3: Prefer Literal Syntax over the Equivalent Methods
- 条款55:让自己熟悉Boost(Familiarize yourself with Boost.)
- Item27 Familiarize yourself with alternatives to overloading on universal references
- Item30 Familiarize yourself with perfect forwarding failure cases
- Effective Objective-C 2.0: Item 6: Understand Properties
- Effective Objective-C 2.0:Item 7: Access Instance Variables
- Effective Objective-C 2.0: Item 12: Understand Message Forwarding
- Effective Objective-C 2.0:Item 14 Class Object
- Effective Objective-C 2.0: Item 16:Designated Initializer
- Effective Objective-C 2.0:Item 20: Prefix Private Method Names
- Effective Objective-C 2.0:Item 26: Avoid Properties in Categories
- Effective Objective-C 2.0: Item 29: Understand Reference Counting
- java ByteBuffer
- 【Hadoop技术博客推荐】站在风口的Hadoop技术研究者
- linux普通用户获得root权限:xxx is not in the sudoers file. This incident will be reported.
- 2014趋势预测,大数据将成主流
- 黑马程序员_javascript学习心得
- Effective Objective-C 2.0: Item 47: Familiarize Yourself with the System Frameworks
- 其它内置变量(4)
- 入门HTML之有序列表
- openoffice.org3.4 源代码 编译
- Java 语言实现的八大排序算法
- 一些初级错误
- ERP项目实施记录03
- 黑马程序员_IO输入与输出之RandomAccessFile类
- python实现哈弗曼树进行数据压缩