Objective - c  Foundation 框架详解2

Collection Agency

Cocoa provides a number of collection classes such as NSArray and NSDictionary whose instances exist just to hold onto other objects.

cocoa 提供了一系列的集合类,例如,NSarray,NSdictionary。它们存在的目的就是为了保持其他对象。

1.1.1NSArray is a Cocoa class that holds an ordered list of objects. You can put any kind of objects in an NSArray: NSString, Car, Shape, Tire, or whatever else you want, even other arrays and dictionaries.

NSArray 是cocoa 类,它提供一个有序的对象列表。里面可以填任何对象。

NSArray has two limitations. First, it holds only Objective-C objects. You can't have primitive C types, like int, float, enum, struct, or random pointers in an NSArray. Also, you can't store nil (the zero or NULL value for objects) in an NSArray.

NSarray 有两个限制。第一,只能存objective -c 对象,不能存C 语言对象,如float等。第二,不能存Nil 。

You can create a new NSArray using the class method arrayWithObjects:. You give it a comma- separated list of objects, with nil at the end to signal the end of the list (which, by the way, is one of the reasons you can't store nil in an array):

arraywithobject 最后要用nil ,作为结束符。

NSArray *array = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];

1.1.2Once you have an array, you can get a count of the number of objects it contains:

- (NSUInteger)count;

And you can fetch an object at a particular index:

- (id)objectAtIndex:(NSUInteger)index;

for (NSInteger i = 0; i < [array count]; i++)

{

NSLog (@"index %d has %@.",i, [array objectAtIndex:i]);

}

You can also write the preceding code using the array literal syntax:

for (NSInteger i = 0; i < [array count]; i++)

{

NSLog (@"index %d has %@.",i, array[i]);

}

1.1.3 Mutable arrays

It uses a class method, arrayWithCapacity, to make a new mutable array:

+ (id) arrayWithCapacity: (NSUInteger) numItems;

NSMutableArray *array = [NSMutableArray arrayWithCapacity: 17];

Add objects to the end of the array by using addObject:.
- (void) addObject: (id) anObject;

You can add four tires to an array with a loop like this:

for (NSInteger i = 0; i < 4; i++)

{

Tire *tire = [Tire new];

[array addObject: tire];

}

You can remove an object at a particular index. For example, if you don't like the second tire, you can use removeObjectAtIndex: to get rid of it. Here's how the method is defined:

- (void) removeObjectAtIndex: (NSUInteger) index;

You use it like this:

[array removeObjectAtIndex:1];

1.2.1Enumeration Nation 枚举

NSEnumerator, which is Cocoa's way of describing this kind of iteration over a collection.

枚举,cocoa的方式,描述一个容器的迭代。

use NSEnumerator, you ask the array for the enumerator using objectEnumerator:

- (NSEnumerator *)objectEnumerator;

You use the method like this:例如:

NSEnumerator *enumerator = [array objectEnumerator];

After you get an enumerator, you crank up a while loop that asks the enumerator for its

nextObject every time through the loop:

- (id) nextObject;

When nextObject returns nil, the loop is done.

如果nextObject 返回nil,那么循环就结束了。

NSEnumerator *enumerator = [array objectEnumerator];

while (id thingie = [enumerator nextObject])

{

NSLog (@"I found %@", thingie);

}

There's one gotcha if you're enumerating over a mutable array: you can't change the container

如果你在操作一个可变数组的话,遍历的时候不要改动容器。

1.2.2 快速枚举 Fast Enumeration

for (NSString *string in array)

{

NSLog (@"I found %@", string);

}

To support the blocks feature, Apple has added a method to enumerate objects in NSArray using blocks, and it looks like this.

为了支持block,苹果增加了一个方法来支持数列用block。

- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block

[array enumerateObjectsUsingBlock:^(NSString *string, NSUInteger index, BOOL *stop) {

NSLog (@"I found %@", string);

}];

Now, the question is, "Why would we use this instead of fast enumeration?" With blocks, one of the options is that the loop can execute in parallel. With fast enumeration, execution proceeds through the items linearly.

这样做的目的就是为了并发执行。

1.3NSDictionary  字典

An NSDictionary stores a value (which can be any kind of Objective-C object) under a given key (usually an NSString).

字典 存储任意的一个值,在给定的一个关键字下(通常是NSString)

However, the NSMutableDictionary class lets you add and remove stuff at will.

NSMutableDictionary允许你自由的增加删除。

The easiest way to get started with a dictionary is to use the dictionary literal syntax, which is similar to the class method dictionaryWithObjectsAndKeys:.

构造一个词典用 逐字 最容易:

The literal syntax is defined as @{key:value,...};

字面是key:value。

NSDictionary *tires = [NSDictionary dictionaryWithObjectsAndKeys: t1,

@"front-left", t2, @"front-right", t3, @"back-left", t4, @"back-right", nil];

or

NSDictionary *tires = @{@"front-left" : ti, @"front-right" : t2, @"back-left" : t3,

@"back-right" : t4};

To access a value in the dictionary, use the objectForKey: method, giving it the key you previously stored the value under:

获取一个值用词典:

- (id) objectForKey: (id) aKey;

or

tires[key];

To make a new NSMutableDictionary, send the dictionary message to the NSMutableDictionary class.

+ (id) dictionaryWithCapacity: (NSUInteger) numItems;

You can add things to the dictionary using setObject:forKey:.

可以增加实物 用setObject:forKey

- (void)setObject:(id)anObject forKey:(id)aKey

NSMutableDictionary *tires = [NSMutableDictionary dictionary];

[tires setObject:tl forKey:@"front-left"];

[tires setObject:t2 forKey:@"front-right"];

[tires setObject:t3 forKey:@"back-left"];

[tires setObject:t4 forKey:@"back-right"];

If you want to take a key out of a mutable dictionary, use the removeObjectForKey:

删除数据:

method: - (void) removeObjectForKey: (id) aKey;

[tires removeObjectForKey:@"back-left"];

1.4 NSNumber 数字

Cocoa provides a class called NSNumber that wraps (that is, implements as objects) the primitive numeric types.

NSNumber 包装了原生的数据类型。

You can create a new NSNumber using these class methods:

+ (NSNumber *) numberWithChar: (char) value;

+ (NSNumber *) numberWithInt: (int) value;

+ (NSNumber *) numberWithFloat: (float) value;

+ (NSNumber *) numberWithBool: (BOOL) value;

You can also use the literal syntax to create these objects:

也可以逐字创建对象。

NSNumber *number;

number = @'X'; // char

number = @12345; // integer

number = @12345ul; // unsigned long

number = @12345ll; // long long

number = @123.45f; // float

number = @123.45; // double

number = @YES; // BOOL

After you create an NSNumber, you can put it into a dictionary or an array:

这样以后你就可以把它放进array 或dictionary了。

NSNumber *number = @42;

[array addObject number];

[dictionary setObject: number forKey: @"Bork"];

Once you have a primitive type wrapped in an NSNumber, you can get it back out using one of these instance methods:

一旦你获得了一个原生数据包装的NSNumber,可以通过下方还原:

- (char) charValue;

- (int) intValue;

- (float) floatValue;

- (BOOL) boolValue;

- (NSString *)stringValue;

1.5 NSValue 值

NSNumber is actually a subclass of NSValue, which wraps arbitrary values. You can use NSValue to put structures into NSArrays and NSDictionary objects.

NSValue 是NSNumber 的基类。NSValue 可以存储任意的值 。

Create a new NSValue using this class method:

+ (NSValue *) valueWithBytes: (const void *) value objCType: (const char *) type;

You pass the address of the value you want to wrap (such as an NSSize or your own struct). Usually, you take the address (using the & operator in C) of the variable you want to save.

你可能传递一个值得地址。

So, to put an NSRect into an NSArray, you do something like this:

如果你想把一个NSRect 放进NSArray中,

NSRect rect = NSMakeRect (1, 2, 30, 40);

NSValue *value = [NSValue valueWithBytes:&rect objCType:@encode(NSRect)];

[array addObject:value];

You can extract the value using getValue:
- (void)getValue:(void *)buffer;

You can extract the value using getValue:
- (void)getValue:(void *)buffer;
When you call getValue:, you pass the address of a variable that you want to hold the value:

当你调用getValue时,你可以传递地址到value。

value = [array objectAtIndex: 0];

[value getValue: &rect];

Convenience methods are provided for putting common Cocoa structs into NSValues, and we have conveniently listed them here:

为了方便 ,列出以下常用类:

+ (NSValue *)valueWithPoint:(NSPoint)aPoint;

+ (NSValue *)valueWithSize:(NSSize)size;

+ (NSValue *)valueWithRect:(NSRect)rect;

- (NSPoint)pointValue;

- (NSSize)sizeValue;

- (NSRect)rectValue;

To store and retrieve an NSRect in an NSArray, you do this:

存储或获取NSrect在NSarray 中:

value = [NSValue valueWithRect:rect];

[array addObject: value];

...

NSRect anotherRect = [value rectValue];

1.6 NSNull 空

We've told you that you can't put nil into a collection, because nil has special meaning to NSArray and NSDictionary. But sometimes, you really need to store a value that means "there's nothing here at all."

我们不能存储nil到容器中,因为nil有特殊意义。但是有时我们确实需要这么做。

NSNull is probably the simplest of all Cocoa classes. It has but a single method:

NSNull 很简单。有一个单独的类方法。

+ (NSNull *) null;

[contact setObject: [NSNull null]

forKey: @"home fax machine"];

id homefax = [contact objectForKey: @"home fax machine"];

if (homefax == [NSNull null])

{

// ... no fax machine. rats.

}

Objective - c Foundation 框架详解2的更多相关文章

  1. object -c OOP , 源码组织 ,Foundation 框架 详解1

     object -c  OOP ,  源码组织  ,Foundation 框架 详解1 1.1 So what is OOP? OOP is a way of constructing softwar ...

  2. [Cocoa]深入浅出 Cocoa 之 Core Data(1)- 框架详解

    Core data 是 Cocoa 中处理数据,绑定数据的关键特性,其重要性不言而喻,但也比较复杂.Core Data 相关的类比较多,初学者往往不太容易弄懂.计划用三个教程来讲解这一部分: 框架详解 ...

  3. mapreduce框架详解

    hadoop 学习笔记:mapreduce框架详解 开始聊mapreduce,mapreduce是hadoop的计算框架,我学hadoop是从hive开始入手,再到hdfs,当我学习hdfs时候,就感 ...

  4. 深入浅出 Cocoa 之 Core Data(1)- 框架详解

    深入浅出 Cocoa 之 Core Data(1)- 框架详解 罗朝辉(http://blog.csdn.net/kesalin) CC 许可,转载请注明出处 Core data 是 Cocoa 中处 ...

  5. jQuery Validate验证框架详解

    转自:http://www.cnblogs.com/linjiqin/p/3431835.html jQuery校验官网地址:http://bassistance.de/jquery-plugins/ ...

  6. mina框架详解

     转:http://blog.csdn.net/w13770269691/article/details/8614584 mina框架详解 分类: web2013-02-26 17:13 12651人 ...

  7. lombok+slf4j+logback SLF4J和Logback日志框架详解

    maven 包依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lomb ...

  8. iOS 开发之照片框架详解(2)

    一. 概况 本文接着 iOS 开发之照片框架详解,侧重介绍在前文中简单介绍过的 PhotoKit 及其与 ALAssetLibrary 的差异,以及如何基于 PhotoKit 与 AlAssetLib ...

  9. Quartz.NET作业调度框架详解

    Quartz.NET作业调度框架详解 http://www.cnblogs.com/lmule/archive/2010/08/28/1811042.html

随机推荐

  1. [办公应用]让WORD自动显示到四级目录

    一般情况下,word的目录默认显示到三级目录.如果需要显示到四级目录,你会怎么操作呢? 只要按下图所示,单击“插入”-“引用”-“索引和目录” 然后单击“目录”选项卡,将“显示级别”处的3改为4即可. ...

  2. Hibernate 之 Locking

    在我们业务实现的过程中,往往会有这样的需求:保证数据访问的排他性,也就是我正在访问的数据,别人不能够访问,或者不能对我的数据进行操作.面对这样的需求,就需要通过一种机制来保证这些数据在一定的操作过程中 ...

  3. github相关

    1 某次release的源码 某次release的源码在release列表中,不在branch中,tag和release是在一起的.所以,下载某个release的源码应该去release中找,而不应该 ...

  4. 判断一个包是否可以安装是一个NP-complete问题

    1 checking whether a single package P can be installed, given a repository R,is NP-complete

  5. Effective C++学习笔记(Part Four:Item 18-25)

     近期最终把effectvie C++细致的阅读了一边.非常惊叹C++的威力与魅力.近期会把近期的读书心得与读书笔记记于此,必备查找使用,假设总结有什么不 当之处,欢迎批评指正: 如今仅仅列出框架 ...

  6. 设计模式-(6)适配器 (swift版)

    用来解决接口适配问题的三种模式:适配器模式,桥接模式,外观模式. 一,概念 适配器模式,将一个类的结构转换成用户希望的另一个接口,使得原本接口不兼容的类能在一起工作.换句话说,适配器模式就是链接两种不 ...

  7. mac下安装eclipse+CDT

    测试文件test.cpp #include <iostream>using namespace std; int main() {    cout << "!!!He ...

  8. 并不对劲的cdq分治解三维偏序

    为了反驳隔壁很对劲的太刀流,并不对劲的片手流决定与之针锋相对,先一步发表cdq分治解三维偏序. 很对劲的太刀流在这里->  参照一.二维偏序的方法,会发现一位偏序就是直接排序,可以看成通过排序使 ...

  9. rsync+inotify 实现实时同步

    inotify:这个可以监控文件系统中的添加,修改,删除,移动等事件 inotify的特性需要linux内核2.6.13以上的支持 [root@test1 inotify-tools-3.13]# u ...

  10. Python机器学习算法 — 关联规则(Apriori、FP-growth)

    关联规则 -- 简介 关联规则挖掘是一种基于规则的机器学习算法,该算法可以在大数据库中发现感兴趣的关系.它的目的是利用一些度量指标来分辨数据库中存在的强规则.也即是说关联规则挖掘是用于知识发现,而非预 ...