最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

Cocoa的Foundation Framework学习笔记

Cocoa crifan 2553浏览 0评论

上接:

【整理】Object-C学习总结


Cocoa就是一个Mac的基础类库,包括了一堆的库,API和运行(时所使用的)库。

Cocoa中包含很多子的框架,其中最基础的一个就是Foundation Framework。

 

现在记录一下,学习:Acquire Basic Programming Skills的心得:

 

1.root class是NSObject,是Object-C中的基类,对应还有一个同名的protocol

2.value class,包括string,number,date,binary data。对应的class,都会实例化为对应的object,该object其实就是原始数据的封装。

3. Collection classes,实例化为collection object,其管理着一组的对象。其包含的对象,一般常为value object。

4.NSObject protocol本身又包含了三个protocol:NSCopying, NSMutableCopyingNSCoding。

5.强引用:引用者拥有被引用者

弱引用:引用者 没有拥有 被引用者。

一个对象的生命周期是由多少个被(别人)强引用(自己)所决定的。

Object-C中默认是强引用。

如果对象没有被别人强引用,则可以被释放。如果有,则不能被释放。

多个对象之间的强引用,如果形成了环,那么就会导致任何一个对象都无法被释放,导致内存泄露。

6.Foundation Framework中包含的value object有:

  • NSString and NSMutableString

  • NSData and NSMutableData

  • NSDate

  • NSNumber

  • NSValue

7.collection是一个特定的对象,其(以某种方式)包含/保存了其他的对象,允许(其他)客户访问这些对象。

多数collection对于所包含对象都是强引用。

最常见的有三种:

  • 数组:NSArray and NSMutableArray
  • 字典:NSDictionary and NSMutableDictionary
  • 集合:NSSet and NSMutableSet

访问数组中的对象的速度,比访问集合中的对象的速度要慢。

collection允许你去枚举。

8.实现枚举有三种方式:

  • 快速枚举
  • 使用block进行枚举
  • 使用NSEnumerator对象

另外,此处Know the Core Objects of Your App中也有部分关于Foundation Framework的知识,摘录如下:

 

The role of objects in an iOS app

Object

Description

UIApplication object

You use the UIApplication object essentially as is—that is, without subclassing. This controller object manages the app event loop and coordinates other high-level app behaviors. Your own custom app-level logic resides in your app delegate object, which works in tandem with this object.

App delegate object

The app delegate is a custom object created at app launch time, usually by the UIApplicationMain function. The primary job of this object is to handle state transitions within the app. For example, this object is responsible for launch-time initialization and handling transitions to and from the background.

In iOS 5 and later, you can use the app delegate to handle other app-related events. The Xcode project templates declare the app delegate as a subclass of UIResponder. If the UIApplication object does not handle an event, it dispatches the event to your app delegate for processing.

Documents and data model objects

Data model objects store your app’s content and are specific to your app. For example, a banking app might store a database containing financial transactions, whereas a painting app might store an image object or even the sequence of drawing commands that led to the creation of that image. (In the case of the drawing commands, an image object is still a data object because it is just a container for the image data.)

Apps can also use document objects (custom subclasses of UIDocument) to manage some or all of their data model objects. Document objects are not required but offer a convenient way to group data that belongs in a single file or file package. For more information about documents, see “Defining a Document-Based Data Model.”

View controller objects

View controller objects manage the presentation of your app’s content on screen. A view controller manages a single view and its collection of subviews. When presented, the view controller makes its views visible by installing them in the app’s window.

The UIViewController class is the base class for all view controller objects. It provides default functionality for loading views, presenting them, rotating them in response to device rotations, and several other standard system behaviors. UIKit and other frameworks define additional view controller classes to implement standard system interfaces such as the image picker, tab bar interface, and navigation interface.

UIWindow object

A UIWindow object coordinates the presentation of one or more views on a screen. Most apps have only one window, which presents content on the main screen, but apps may have an additional window for content displayed on an external display.

To change the content of your app, you use a view controller to change the views displayed in the corresponding window. You never replace the window itself.

In addition to hosting views, windows work with the UIApplication object to deliver events to your views and view controllers.

View, control, and layer objects

Views and controls provide the visual representation of your app’s content. A view is an object that draws content in a designated rectangular area and responds to events within that area. Controls are a specialized type of view responsible for implementing familiar interface objects such as buttons, text fields, and toggle switches.

The UIKit framework provides standard views for presenting many types of content. You can also define your own custom views by subclassing UIView (or its descendants) directly.

In addition to incorporating views and controls, apps can also incorporate Core Animation layers into their view and control hierarchies. Layer objects are actually data objects that represent visual content. Views use layer objects extensively behind the scenes to render their content. You can also add custom layer objects to your interface to implement complex animations and other types of sophisticated visual effects.

 

Data classes in the Foundation framework

Data

Classes

Description

Strings and text

NSString (NSMutableString)

NSAttributedString (NSMutableAttributedString)

Strings in iOS are Unicode based. The string classes provide support for creating and manipulating strings in a variety of ways. The attributed string classes support stylized text and are used only in conjunction with Core Text.

Numbers

NSNumber

NSDecimalNumber

NSIndexPath

When you want to store numerical values in a collection, use number objects. The NSNumber class can represent integer, floating-point, Boolean, and char values. The NSIndexPath class stores a sequence of numbers and is often used to specify multilayer selections in hierarchical lists.

Raw bytes

NSData (NSMutableData)

NSValue

For times when you need to store raw streams of bytes, use data objects. Data objects are also commonly used to store objects in an archived form. The NSValue class is typically extended (using categories) and used to archive common data types such as points and rectangles.

Dates and times

NSDate

NSDateComponents

Use date objects to store timestamps, calendar dates, and other time-related information.

URLs

NSURL

In addition to their traditional use for referring to network resources, URLs in iOS are the preferred way to store paths to files. The NSURL class even provides support for getting and setting file-related attributes.

Collections

NSArray (NSMutableArray)

NSDictionary (NSMutableDictionary)

NSIndexSet (NSMutableIndexSet)

NSOrderedSet (NSMutableOrderedSet)

NSSet (NSMutableSet)

Use collections to group related objects together in a single place. The Foundation framework provides several different types of collection classes.

 


下接:

iOS Framework学习记录 + 理解iOS和OS X在Framework方面的区别和联系

转载请注明:在路上 » Cocoa的Foundation Framework学习笔记

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
85 queries in 0.184 seconds, using 22.02MB memory