NSArray,NSSet,NSEnumerator,NSDictionary及对应Mutable介绍
上述的这些都是 Objective-C 中的集合类,不带 Mutable 的是不可修改的集合类, 带 Mutable 的是可以修改的集合类。下面的例子中有足够的注释,非常清晰,不多说了。
1. NSArray与NSMutableArray
就是通常的数组,其中的每一个元素有各自确定的唯一序号,元素可以重复
2. NSSet与NSMutableSet
就是通常的集合,其中的元素没有确定的序号(即遍历的顺序与加入集合的顺序没有任何关系),但决不重复(若加入相同元素则会覆盖之前的)
3. NSEnumerator(没有NSMutableEnumerator )
类似于NSArray,一般不单独使用。只是作为 NSArray,NSSet 和 NSDictionary 的辅助类(它们都有相应的方法转换为NSEnumerator)。特别要注意 NSEnumerator 中的元素一旦遍历过之后就不能重新遍历,必须重新创建一个NSEnumerator。
4. NSDictionary与NSMutableDictionary
就是我们常说的键值对(Key-Value),每一个元素包含一个Key和一个Value。通过Key可以查到它对应的Value。
注意和NSSet一样,NSDictionary 中元素的顺序也是不确定的。
下面是例子:
1.
创建一个View-based Application,名称是test
2.
修改 TestViewController.m 如下:
//
// TestViewController.m
// Test
//
// Created by HuTao on 8/19/12.
// Copyright __MyCompanyName__ 2012. All rights reserved.
//
#import "TestViewController.h"
@implementation TestViewController
-(void)testNSArray
{
//创建一个 NSArray,必须以 nil 结尾
NSArray * array = [NSArray arrayWithObjects:@"Mike", @"Ben", @"Alice", @"Jane", @"Bob", @"Peter", @"Tom", @"Sam", nil];
//或:
//NSArray * array = [[NSArray alloc] initWithObjects:@"Mike", @"Ben", @"Alice", @"Jane", @"Bob", @"Peter", @"Tom", @"Sam", nil];
//得到 NSArray 的数量
int count = [array count];
NSLog(@"NSArray的数量: %d", count);
//判断 NSArray 中是否包含某项
if([array containsObject:@"Bob"])
{
NSLog(@"含有Bob");
}
else
{
NSLog(@"不含有Bob");
}
if([array containsObject:@"Linda"])
{
NSLog(@"含有Linda");
}
else
{
NSLog(@"不含有Linda");
}
//根据值判断在 NSArray 中的第一个的序号(从0开始算),不存在则返回 NSNotFound
NSLog(@"Bob在第%d项", [array indexOfObject:@"Bob"]);
if([array indexOfObject:@"Linda"] == NSNotFound)
{
NSLog(@"没有找到Linda");
}
else
{
NSLog(@"找到Linda");
}
//获取指定序号的项(从0开始算)
NSLog(@"第%d项是%@", 2, [array objectAtIndex:2]);
//遍历 NSArray
for(NSString * a in array)
{
NSLog(@"遍历NSArray: %@", a);
}
}
-(void)testNSMutableArray
{
//创建一个空的 NSMutableArray 数组,无需手动释放
NSMutableArray * array = [NSMutableArray array];
//已有元素时,也可以按如下创建:
//NSMutableArray * array = [NSMutableArray arrayWithObjects:@"One", @"Two", @"Three", nil];
//在最后添加项
[array addObject:@"Mike"];
[array addObject:@"Alice"];
[array addObject:@"Tom"];
[array addObject:@"Ben"];
[array addObject:@"Peter"];
[array addObject:@"Sam"];
//将另一个数组也添加进来
NSMutableArray * array2 = [NSMutableArray array];
[array addObject:@"Linda"];
[array addObject:@"John"];
[array addObjectsFromArray:array2];
//在指定位置添加项(在第atIndex项之前插入)
[array insertObject:@"Bob" atIndex:2];
//遍历 NSArray
for(NSString * a in array)
{
NSLog(@"插入后的NSArray: %@", a);
}
//替换项
[array replaceObjectAtIndex:4 withObject:@"David"];
//遍历 NSArray
for(NSString * a in array)
{
NSLog(@"替换后的NSArray: %@", a);
}
//删除最后的项
[array removeLastObject];
//加入重复项
[array addObject:@"Alice"];
//删除指定位置的项
[array removeObjectAtIndex:5];
//删除所有等于Alice的项
[array removeObject:@"Alice"];
//遍历 NSArray
for(NSString * a in array)
{
NSLog(@"删除后的NSArray: %@", a);
}
//删除数组中所有与数组array包含相同的元素
NSMutableArray * array3 = [NSMutableArray array];
[array3 addObject:@"Sam"];
[array3 addObject:@"Mike"];
[array3 addObject:@"John"];
[array removeObjectsInArray:array3];
//遍历 NSArray
for(NSString * a in array)
{
NSLog(@"删除指定数组项后的NSArray: %@", a);
}
//删除所有对象
[array removeAllObjects];
//得到 NSMutableArray 的数量
int count = [array count];
NSLog(@"NSMutableArray的数量: %d", count);
//将 NSArray 转成 NSMutableArray
NSArray * array4 = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", @"d", nil];
NSMutableArray * array5 = [NSMutableArray arrayWithArray:array4];
NSLog(@"转换后的 NSMutableArray: %@", array5);
}
-(void)testNSSet
{
//NSSet 中的元素是无序且唯一的
NSArray * array1 = [NSArray arrayWithObjects:@"Orange", @"Apple", @"Pear", @"Banana", @"Melon", nil];
NSArray * array2 = [NSArray arrayWithObjects:@"Melon", @"Banana", @"Orange", @"Pear", @"Apple", nil];
NSSet * set1 = [NSSet setWithArray:array1];
NSSet * set2 = [NSSet setWithArray:array2];
//也可以如下创建NSSet,但必须以 nil 结尾
NSSet * set3 = [NSSet setWithObjects:@"Banana", @"Pear", @"Apple", nil];
//得到NSSet的数量
int count = [set1 count];
NSLog(@"NSSet的数量: %d", count);
NSString * s;
//遍历NSSet,注意顺序是无序的,遍历结束返回nil
//虽然含有相同元素,但set1和set2的遍历顺序却是不同的(或者每次遍历的顺序都不是相同的)
for(s in set1)
{
NSLog(@"遍历NSSet1: %@", s);
}
for(s in set2)
{
NSLog(@"遍历NSSet2: %@", s);
}
//也可以这样遍历
NSEnumerator * enumerator = [set1 objectEnumerator];
for(s in enumerator)
{
NSLog(@"NSEnumerator遍历NSSet: %@", s);
}
//判断是否含有Banana字符串
if([set1 containsObject:@"Banana"])
{
NSLog(@"NSSet包含Banana");
}
else
{
NSLog(@"NSSet不包含Banana");
}
//判断 set1 是否等于set2(只要含有相同的元素且集合中的元素个数相等即这两个set相等)
if([set1 isEqualToSet:set2])
{
NSLog(@"set1 等于 set2");
}
else
{
NSLog(@"set1 不等于 set2");
}
//判断set3是否是set1的子集合
if ([set3 isSubsetOfSet:set1])
{
NSLog(@"set3 是 set1 的子集");
}
else
{
NSLog(@"set3 不是 set1 的子集");
}
//获取所有NSSet对象
NSArray * array4 = [set1 allObjects];
NSArray * array5 = [set2 allObjects];
for(s in array4)
{
NSLog(@"allObjects set1: %@", s);
}
for(s in array5)
{
NSLog(@"allObjects set2: %@", s);
}
}
-(void)testNSMutableSet
{
//定义 NSMutableSet
NSMutableSet * set1 = [NSMutableSet setWithObjects:@"Orange", @"Apple", @"Pear", @"Banana", @"Melon", nil];
NSMutableSet * set2 = [NSMutableSet setWithObjects:@"Orange", @"Apple", @"Pear", @"Banana", @"Melon", nil];
NSMutableSet * set3 = [NSMutableSet setWithObjects:@"Orange", @"Apple", @"Pear", @"Banana", @"Melon", nil];
//定义一个初始长度为6的NSMutableSet
NSMutableSet * set4 = [NSMutableSet setWithCapacity:6];
NSMutableSet * set5 = [NSMutableSet setWithObjects:@"Banana", @"Pear", @"Apple", nil];
NSString * s;
//遍历NSMutableSet
for(s in set1)
{
NSLog(@"遍历NSSet1: %@", s);
}
//加入元素
[set4 addObject:@"Pear"];
[set4 addObject:@"Banana"];
[set4 addObject:@"Orange"];
[set4 addObject:@"Pear"];
[set4 addObject:@"Melon"];
//加入重复的元素会被忽略
int count = [set4 count];
NSLog(@"set4的数量: %d", count);
//删除set1中所有包含在set5中的元素
[set1 minusSet:set5];
for(s in set1)
{
NSLog(@"minus后遍历set1: %@", s);
}
//将set2与set5的交集赋值到set2
[set2 intersectSet:set5];
for(s in set2)
{
NSLog(@"intersectSet后遍历set2: %@", s);
}
//将set3与set5并集赋值到set3
[set3 unionSet:set5];
for(s in set3)
{
NSLog(@"unionSet后遍历set3: %@", s);
}
//将NSArray中的数据加入NSMutableSet
NSMutableSet * set6 = [NSMutableSet setWithCapacity:3];
NSArray * array = [NSArray arrayWithObjects:@"Mike", @"Ben", @"Alice", @"Jane", @"Bob", @"Peter", @"Tom", @"Sam", nil];
[set6 addObjectsFromArray:array];
for(s in set6)
{
NSLog(@"addObjectsFromArray后遍历set6: %@", s);
}
//过滤NSMutableSet
NSMutableSet * set7 = [NSMutableSet setWithObjects:@"One", @"Two", @"Three", @"Four", nil];
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF beginswith 'T'"];
[set7 filterUsingPredicate:predicate];
//过滤后set7中只含有Two和Three
for(s in set7)
{
NSLog(@"过滤后遍历set7: %@", s);
}
//删除指定元素
[set1 removeObject:@"Melon"];
for(s in set1)
{
NSLog(@"删除Melon后遍历set1: %@", s);
}
//删除不存在的元素不会出错
[set1 removeObject:@"grape"];
for(s in set1)
{
NSLog(@"删除grape后遍历set1: %@", s);
}
//删除所有元素
[set1 removeAllObjects];
NSLog(@"删除所有元素后set1的数量: %d", [set1 count]);
}
-(void)testNSEnumerator
{
//集合类(如:NSArray、NSSet、NSDictionary等)均可获取到NSEnumerator
//NSEnumerator的nextObject方法可以遍历每个集合元素,结束返回nil,通过与while结合使用可遍历集合中所有项。
NSArray * array = [NSArray arrayWithObjects:@"Orange", @"Apple", @"Pear", @"Banana", @"Melon", nil];
//通过objectEnumberator获取集合的NSEnumerator,reverseObjectEnumerator反向获取
NSEnumerator * enumerator1 = [array objectEnumerator];
NSEnumerator * enumerator2 = [array reverseObjectEnumerator];
NSEnumerator * enumerator3 = [array objectEnumerator];
NSString * s;
//注意:使用nextObject遍历NSEnumerator是永久性的,即遍历后将不可重新遍历,只能重新创建新NSEnumerator
while(s = [enumerator1 nextObject])
{
NSLog(@"遍历NSEnumerator1: %@", s);
}
while(s = [enumerator2 nextObject])
{
NSLog(@"遍历NSEnumerator2: %@", s);
}
//注意:使用nextObject遍历NSEnumerator是永久性的,即遍历后将不可重新遍历,只能重新创建新NSEnumerator
[enumerator3 nextObject];
[enumerator3 nextObject];
[enumerator3 nextObject];
//allObjects 返回剩余没有被nextObject遍历到的元素
NSArray * a1 = [enumerator1 allObjects];
NSArray * a2 = [enumerator3 allObjects];
//由于之前已经遍历完enumerator1,因此不会打印
for(s in a1)
{
NSLog(@"NSEnumerator1 allObjects: %@", s);
}
//只会打印出最后两项
for(s in a2)
{
NSLog(@"NSEnumerator3 allObjects: %@", s);
}
}
-(void)testNSDictionary
{
/* 用 NSDictionary 创建不可变Dictionary */
NSString * s;
//初始化一个 NSDictionary,顺序是 value1, key1, value2, key2 ......,必须以 nil 结尾
NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Tom", @"name",
@"20", @"age",
@"Male", @"sex", nil];
//得到词典的数量
int count = [dict count];
NSLog(@"词典的数量: %d", count);
//将所有KEY储存在 NSEnumerator 中
NSEnumerator * enumeratorKey = [dict keyEnumerator];
//遍历所有KEY值
for(s in enumeratorKey)
{
NSLog(@"遍历KEY的值: %@", s);
}
//将所有Value储存在 NSEnumerator 中
NSEnumerator * enumeratorValue = [dict objectEnumerator];
//快速枚举遍历所有Value值
for(s in enumeratorValue)
{
NSLog(@"遍历Value的值: %@", s);
}
//通过KEY找到Value,没有找到返回nil
s = [dict objectForKey:@"age"];
if (s != nil)
{
NSLog(@"通过KEY找到的value是: %@", s);
}
else
{
NSLog(@"没有找到KEY对应的Value");
}
}
-(void)testNSMutableDictionary
{
/* 用 NSMutableDictionary 创建不可变Dictionary */
NSString * s;
//创建一个可变Dictionary,初始长度为10,如果后面动态添加的数据超过10,
//则 NSMutableDictionary 长度会自动增加,所以不用担心数组越界
NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithCapacity:10];
//或者 NSMutableDictionary * dict = [NSMutableDictionary initWithCapacity:10]; 也可以
//加入 Key-Value 对
[dict setObject:@"Tom" forKey:@"name"];
[dict setObject:@"20" forKey:@"age"];
[dict setObject:@"Male" forKey:@"sex"];
//通过KEY找到value
s = [dict objectForKey:@"sex"];
if (s != nil)
{
NSLog(@"修改前value: %@", s);
}
else
{
NSLog(@"没有找到KEY对应的Value");
}
//如果KEY相同则会直接覆盖
[dict setObject:@"Female" forKey:@"sex"];
s = [dict objectForKey:@"sex"];
if (s != nil)
{
NSLog(@"修改后value: %@", s);
}
else
{
NSLog(@"没有找到KEY对应的Value");
}
//也可以这样遍历
//获取包含所有KEY的数组
NSArray * keys = [dict allKeys];
for(s in keys)
{
NSLog(@"%@ is %@", s, [dict objectForKey:s]);
}
//删除Dictionary中指定项的数据
[dict removeObjectForKey:@"name"];
NSLog(@"删除前数量:%d", [dict count]);
//删除Dictionary中的所有数据
[dict removeAllObjects];
NSLog(@"删除后数量:%d", [dict count]);
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
//NSArray 与 NSMutableArray
[self testNSArray];
[self testNSMutableArray];
//NSSet 与 NSMutableSet
[self testNSSet];
[self testNSMutableSet];
//NSEnumerator
[self testNSEnumerator];
//NSDictionary 与 NSMutableDictionary
[self testNSDictionary];
[self testNSMutableDictionary];
}
- (void)dealloc
{
[super dealloc];
}
@end
运行程序,Console输出结果如下:
由于输出太多,无法全部截图,下面是全部输出:
[Session started at 2012-08-21 12:14:21 +0000.]
2012-08-21 12:14:26.907 Test[1501:207] ******* Accessibility Status Changed: On
2012-08-21 12:14:26.954 Test[1501:207] ********** Loading AX for: com.yourcompany.Test ************
2012-08-21 12:14:27.013 Test[1501:207] NSArray的数量: 8
2012-08-21 12:14:27.014 Test[1501:207] 含有Bob
2012-08-21 12:14:27.015 Test[1501:207] 不含有Linda
2012-08-21 12:14:27.016 Test[1501:207] Bob在第4项
2012-08-21 12:14:27.017 Test[1501:207] 没有找到Linda
2012-08-21 12:14:27.018 Test[1501:207] 第2项是Alice
2012-08-21 12:14:27.019 Test[1501:207] 遍历NSArray: Mike
2012-08-21 12:14:27.020 Test[1501:207] 遍历NSArray: Ben
2012-08-21 12:14:27.021 Test[1501:207] 遍历NSArray: Alice
2012-08-21 12:14:27.022 Test[1501:207] 遍历NSArray: Jane
2012-08-21 12:14:27.024 Test[1501:207] 遍历NSArray: Bob
2012-08-21 12:14:27.025 Test[1501:207] 遍历NSArray: Peter
2012-08-21 12:14:27.026 Test[1501:207] 遍历NSArray: Tom
2012-08-21 12:14:27.027 Test[1501:207] 遍历NSArray: Sam
2012-08-21 12:14:27.029 Test[1501:207] 插入后的NSArray: Mike
2012-08-21 12:14:27.030 Test[1501:207] 插入后的NSArray: Alice
2012-08-21 12:14:27.031 Test[1501:207] 插入后的NSArray: Bob
2012-08-21 12:14:27.032 Test[1501:207] 插入后的NSArray: Tom
2012-08-21 12:14:27.033 Test[1501:207] 插入后的NSArray: Ben
2012-08-21 12:14:27.034 Test[1501:207] 插入后的NSArray: Peter
2012-08-21 12:14:27.036 Test[1501:207] 插入后的NSArray: Sam
2012-08-21 12:14:27.037 Test[1501:207] 插入后的NSArray: Linda
2012-08-21 12:14:27.038 Test[1501:207] 插入后的NSArray: John
2012-08-21 12:14:27.039 Test[1501:207] 替换后的NSArray: Mike
2012-08-21 12:14:27.040 Test[1501:207] 替换后的NSArray: Alice
2012-08-21 12:14:27.041 Test[1501:207] 替换后的NSArray: Bob
2012-08-21 12:14:27.042 Test[1501:207] 替换后的NSArray: Tom
2012-08-21 12:14:27.043 Test[1501:207] 替换后的NSArray: David
2012-08-21 12:14:27.044 Test[1501:207] 替换后的NSArray: Peter
2012-08-21 12:14:27.045 Test[1501:207] 替换后的NSArray: Sam
2012-08-21 12:14:27.047 Test[1501:207] 替换后的NSArray: Linda
2012-08-21 12:14:27.048 Test[1501:207] 替换后的NSArray: John
2012-08-21 12:14:27.049 Test[1501:207] 删除后的NSArray: Mike
2012-08-21 12:14:27.051 Test[1501:207] 删除后的NSArray: Bob
2012-08-21 12:14:27.052 Test[1501:207] 删除后的NSArray: Tom
2012-08-21 12:14:27.053 Test[1501:207] 删除后的NSArray: David
2012-08-21 12:14:27.054 Test[1501:207] 删除后的NSArray: Sam
2012-08-21 12:14:27.056 Test[1501:207] 删除后的NSArray: Linda
2012-08-21 12:14:27.057 Test[1501:207] 删除指定数组项后的NSArray: Bob
2012-08-21 12:14:27.058 Test[1501:207] 删除指定数组项后的NSArray: Tom
2012-08-21 12:14:27.059 Test[1501:207] 删除指定数组项后的NSArray: David
2012-08-21 12:14:27.060 Test[1501:207] 删除指定数组项后的NSArray: Linda
2012-08-21 12:14:27.061 Test[1501:207] NSMutableArray的数量: 0
2012-08-21 12:14:27.062 Test[1501:207] 转换后的 NSMutableArray: (
a,
b,
c,
d
)
2012-08-21 12:14:27.063 Test[1501:207] NSSet的数量: 5
2012-08-21 12:14:27.066 Test[1501:207] 遍历NSSet1: Melon
2012-08-21 12:14:27.067 Test[1501:207] 遍历NSSet1: Apple
2012-08-21 12:14:27.068 Test[1501:207] 遍历NSSet1: Orange
2012-08-21 12:14:27.069 Test[1501:207] 遍历NSSet1: Pear
2012-08-21 12:14:27.070 Test[1501:207] 遍历NSSet1: Banana
2012-08-21 12:14:27.071 Test[1501:207] 遍历NSSet2: Banana
2012-08-21 12:14:27.072 Test[1501:207] 遍历NSSet2: Apple
2012-08-21 12:14:27.073 Test[1501:207] 遍历NSSet2: Orange
2012-08-21 12:14:27.074 Test[1501:207] 遍历NSSet2: Pear
2012-08-21 12:14:27.075 Test[1501:207] 遍历NSSet2: Melon
2012-08-21 12:14:27.077 Test[1501:207] NSEnumerator遍历NSSet: Melon
2012-08-21 12:14:27.078 Test[1501:207] NSEnumerator遍历NSSet: Apple
2012-08-21 12:14:27.080 Test[1501:207] NSEnumerator遍历NSSet: Orange
2012-08-21 12:14:27.081 Test[1501:207] NSEnumerator遍历NSSet: Pear
2012-08-21 12:14:27.082 Test[1501:207] NSEnumerator遍历NSSet: Banana
2012-08-21 12:14:27.084 Test[1501:207] NSSet包含Banana
2012-08-21 12:14:27.085 Test[1501:207] set1 等于 set2
2012-08-21 12:14:27.086 Test[1501:207] set3 是 set1 的子集
2012-08-21 12:14:27.087 Test[1501:207] allObjects set1: Melon
2012-08-21 12:14:27.088 Test[1501:207] allObjects set1: Apple
2012-08-21 12:14:27.089 Test[1501:207] allObjects set1: Orange
2012-08-21 12:14:27.091 Test[1501:207] allObjects set1: Pear
2012-08-21 12:14:27.092 Test[1501:207] allObjects set1: Banana
2012-08-21 12:14:27.093 Test[1501:207] allObjects set2: Banana
2012-08-21 12:14:27.094 Test[1501:207] allObjects set2: Apple
2012-08-21 12:14:27.095 Test[1501:207] allObjects set2: Orange
2012-08-21 12:14:27.096 Test[1501:207] allObjects set2: Pear
2012-08-21 12:14:27.097 Test[1501:207] allObjects set2: Melon
2012-08-21 12:14:27.098 Test[1501:207] 遍历NSSet1: Melon
2012-08-21 12:14:27.099 Test[1501:207] 遍历NSSet1: Apple
2012-08-21 12:14:27.100 Test[1501:207] 遍历NSSet1: Orange
2012-08-21 12:14:27.101 Test[1501:207] 遍历NSSet1: Pear
2012-08-21 12:14:27.102 Test[1501:207] 遍历NSSet1: Banana
2012-08-21 12:14:27.103 Test[1501:207] set4的数量: 4
2012-08-21 12:14:27.104 Test[1501:207] minus后遍历set1: Melon
2012-08-21 12:14:27.105 Test[1501:207] minus后遍历set1: Orange
2012-08-21 12:14:27.106 Test[1501:207] intersectSet后遍历set2: Apple
2012-08-21 12:14:27.108 Test[1501:207] intersectSet后遍历set2: Pear
2012-08-21 12:14:27.109 Test[1501:207] intersectSet后遍历set2: Banana
2012-08-21 12:14:27.110 Test[1501:207] unionSet后遍历set3: Melon
2012-08-21 12:14:27.111 Test[1501:207] unionSet后遍历set3: Apple
2012-08-21 12:14:27.112 Test[1501:207] unionSet后遍历set3: Orange
2012-08-21 12:14:27.114 Test[1501:207] unionSet后遍历set3: Pear
2012-08-21 12:14:27.114 Test[1501:207] unionSet后遍历set3: Banana
2012-08-21 12:14:27.115 Test[1501:207] addObjectsFromArray后遍历set6: Jane
2012-08-21 12:14:27.117 Test[1501:207] addObjectsFromArray后遍历set6: Sam
2012-08-21 12:14:27.117 Test[1501:207] addObjectsFromArray后遍历set6: Tom
2012-08-21 12:14:27.118 Test[1501:207] addObjectsFromArray后遍历set6: Alice
2012-08-21 12:14:27.120 Test[1501:207] addObjectsFromArray后遍历set6: Bob
2012-08-21 12:14:27.121 Test[1501:207] addObjectsFromArray后遍历set6: Ben
2012-08-21 12:14:27.122 Test[1501:207] addObjectsFromArray后遍历set6: Peter
2012-08-21 12:14:27.123 Test[1501:207] addObjectsFromArray后遍历set6: Mike
2012-08-21 12:14:27.133 Test[1501:207] 过滤后遍历set7: Three
2012-08-21 12:14:27.135 Test[1501:207] 过滤后遍历set7: Two
2012-08-21 12:14:27.135 Test[1501:207] 删除Melon后遍历set1: Orange
2012-08-21 12:14:27.136 Test[1501:207] 删除grape后遍历set1: Orange
2012-08-21 12:14:27.137 Test[1501:207] 删除所有元素后set1的数量: 0
2012-08-21 12:14:27.138 Test[1501:207] 遍历NSEnumerator1: Orange
2012-08-21 12:14:27.139 Test[1501:207] 遍历NSEnumerator1: Apple
2012-08-21 12:14:27.140 Test[1501:207] 遍历NSEnumerator1: Pear
2012-08-21 12:14:27.141 Test[1501:207] 遍历NSEnumerator1: Banana
2012-08-21 12:14:27.142 Test[1501:207] 遍历NSEnumerator1: Melon
2012-08-21 12:14:27.143 Test[1501:207] 遍历NSEnumerator2: Melon
2012-08-21 12:14:27.144 Test[1501:207] 遍历NSEnumerator2: Banana
2012-08-21 12:14:27.146 Test[1501:207] 遍历NSEnumerator2: Pear
2012-08-21 12:14:27.150 Test[1501:207] 遍历NSEnumerator2: Apple
2012-08-21 12:14:27.151 Test[1501:207] 遍历NSEnumerator2: Orange
2012-08-21 12:14:27.153 Test[1501:207] NSEnumerator3 allObjects: Banana
2012-08-21 12:14:27.154 Test[1501:207] NSEnumerator3 allObjects: Melon
2012-08-21 12:14:27.155 Test[1501:207] 词典的数量: 3
2012-08-21 12:14:27.156 Test[1501:207] 遍历KEY的值: age
2012-08-21 12:14:27.158 Test[1501:207] 遍历KEY的值: name
2012-08-21 12:14:27.159 Test[1501:207] 遍历KEY的值: sex
2012-08-21 12:14:27.160 Test[1501:207] 遍历Value的值: 20
2012-08-21 12:14:27.161 Test[1501:207] 遍历Value的值: Tom
2012-08-21 12:14:27.161 Test[1501:207] 遍历Value的值: Male
2012-08-21 12:14:27.163 Test[1501:207] 通过KEY找到的value是: 20
2012-08-21 12:14:27.164 Test[1501:207] 修改前value: Male
2012-08-21 12:14:27.164 Test[1501:207] 修改后value: Female
2012-08-21 12:14:27.165 Test[1501:207] name is Tom
2012-08-21 12:14:27.166 Test[1501:207] age is 20
2012-08-21 12:14:27.168 Test[1501:207] sex is Female
2012-08-21 12:14:27.169 Test[1501:207] 删除前数量:2
2012-08-21 12:14:27.170 Test[1501:207] 删除后数量:0
最后我把完整代码也上传上来了:
http://download.csdn.net/detail/htttw/4515104
完成!