目录

Data storage

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

NSArray和NSMutableArray

NSArray用于保存不可变对象数组,NSMutableArray用于保存可变对象数组。

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

NSArray的重要方法如下

  • alloc/initWithObjects - 用于使用对象初始化数组。

  • objectAtIndex - 返回特定索引处的对象。

  • count - 返回对象数

NSMutableArray继承自NSArray,因此NSArray的所有实例方法都可在NSMutableArray中使用

NSMutableArray的重要方法如下 -

  • removeAllObjects - 清空数组。

  • addObject - 在数组末尾插入给定对象。

  • removeObjectAtIndex - 用于删除objectAt特定索引

  • exchangeObjectAtIndex:withObjectAtIndex - 在给定索引处交换数组中的对象。

  • replaceObjectAtIndex:withObject - 用Object替换索引处的对象。

我们必须记住,上面的列表只是常用的方法,我们可以跳转到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,因此NSDictionary的所有实例方法都可以在NSMutableDictionary中使用

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)
↑回到顶部↑
WIKI教程 @2018