发布于 2015-07-05 11:54:59 | 164 次阅读 | 评论: 0 | 来源: 网络整理

数据存储和检索是在任何程序中的最重要的。在Objective-C中,我们一般不会依赖于像链表结构,因为它使复杂的工作。相反,我们使用的NSArray,NSDictionary,NSSet及其可变形式等的集合。

NSArray & NSMutableArray

NSArray是用来装不可变的对象数组,NSMutableArray用于容纳一个可变数组对象。

Mutablility有助于改变数组中运行一个预分配的数组,但如果我们使用NSArray 只能更换现有数组,并不能改变现有数组的内容。

NSArray 的重要方法如下:

  • alloc/initWithObjects: 用来初始化一个数组对象。

  • objectAtIndex: 在特定索引allReturns对象。

  • count: 返回的对象的数量

NSMutableArray继承自NSArray,因此NSMutableArray有NSArray 所有实例方法

重要的 NSMutableArray 方法如下:

  • removeAllObjects: 清空数组。

  • addObject: 数组末尾插入一个给定的对象。

  • removeObjectAtIndex: 这是用来删除objectAt 指定索引的对象

  • exchangeObjectAtIndex:withObjectAtIndex: 改变阵列中的对象在给定的索引。

  • replaceObjectAtIndex:withObject: 替换的对象与对象在索引。

我们一定要记住,上述列表只是经常使用的方法,我们可以在我们的XCode跳进各自的类要知道更多的方法,在这些类。下面是一个简单的例子。


#import <Foundation/Foundation.h>

int main()
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   NSArray *array = [[NSArray alloc]
   initWithObjects:@"string1", @"string2",@"string3",nil];
   NSString *string1 = [array objectAtIndex:0];
   NSLog(@"The object in array at Index 0 is %@",string1);
   NSMutableArray *mutableArray = [[NSMutableArray alloc]init];
   [mutableArray addObject: @"string"];
   string1 = [mutableArray objectAtIndex:0];
   NSLog(@"The object in mutableArray at Index 0 is %@",string1); 
   [pool drain];
   return 0;
}

现在,当我们编译并运行程序,我们会得到以下的结果。


2013-09-29 02:33:23.195 demo[3487] The object in array at Index 0 is string1
2013-09-29 02:33:23.196 demo[3487] The object in mutableArray at Index 0 is string

在上面的程序中,我们已经看到 NSMutableArray 和 NSArray 的一个简单的区别,在这里我们可以插入一个字符串分配后可变数组。

NSDictionary & NSMutableDictionary

NSDictionary 用于容纳一个不可变的对象,字典 NSMutableDictionary 用于容纳一个可变的对象字典。

重要的NSDictionary方法如下

  • alloc/initWithObjectsAndKeys: 初始化一个新分配的字典带构建从指定的集合值和键的条目。

  • valueForKey: 返回与给定键关联的值。

  • count: 返回在字典中的条目的数量。

NSMutableDictionary 继承自 NSDictionary ,因此NSMutableDictionary 实例拥有 NSDictionary 的所有方法

重要的NSMutableDictionary方法如下:

  • removeAllObjects:清空字典条目。

  • removeObjectForKey: 从字典删除给定键及其关联值。

  • setValue:forKey: 添加一个给定的键 - 值对到字典中。

字典的一个简单的例子如下:


#import <Foundation/Foundation.h>

int main()
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
   @"string1",@"key1", @"string2",@"key2",@"string3",@"key3",nil];
   NSString *string1 = [dictionary objectForKey:@"key1"];
   NSLog(@"The object for key, key1 in dictionary is %@",string1);
   NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc]init];
   [mutableDictionary setValue:@"string" forKey:@"key1"];
   string1 = [mutableDictionary objectForKey:@"key1"];
   NSLog(@"The object for key, key1 in mutableDictionary is %@",string1); 
   [pool drain];
   return 0;
}

现在,当我们编译并运行程序,我们会得到以下的结果。


2013-09-29 02:34:50.528 demo[9135] The object for key, key1 in dictionary is string1
2013-09-29 02:34:50.528 demo[9135] The object for key, key1 in mutableDictionary is string

NSSet & NSMutableSet

NSSet 是用来保持一个不变集的不同对象,NSMutableDictionary 用于容纳一个可变设置的不同对象。

重要的NSSet方法如下:

  • alloc/initWithObjects: 初始化一个新分配的成员采取从指定的对象列表。

  • allObjects - 返回一个数组,包含集合的成员或一个空数组(如果该组没有成员)。

  • count: 返回集合中的成员数量。

NSMutableSet 继承自NSSet,因此所有NSSet方法的在NSMutableSet 的实例是可用的。

重要的 NSMutableSet方法如下:

  • removeAllObjects: 清空其所有成员的集合。

  • addObject: 添加一个给定的对象的集合(如果它还不是成员)。

  • removeObject: 从集合中删除给定的对象。

集合的一个简单的例子如下:


#import <Foundation/Foundation.h>

int main()
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   NSSet *set = [[NSSet alloc]
   initWithObjects:@"string1", @"string2",@"string3",nil];
   NSArray *setArray = [set allObjects];
   NSLog(@"The objects in set are %@",setArray);
   NSMutableSet *mutableSet = [[NSMutableSet alloc]init];
   [mutableSet addObject:@"string1"];
   setArray = [mutableSet allObjects];
   NSLog(@"The objects in mutableSet are %@",setArray);
   [pool drain];
   return 0;
}

现在,当我们编译并运行程序,我们会得到以下的结果。


2013-09-29 02:35:40.221 demo[12341] The objects in set are (string3, string2, string1)
2013-09-29 02:35:40.222 demo[12341] The objects in mutableSet are (string1)
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务