Objective-C 中的数据存储

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


NSArray & NSMutableArray

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

可变性有助于在运行时将数组更改为预分配的数组,但如果我们使用 NSArray,我们只是替换现有数组而不能更改现有数组的内容。

NSArray的重要方法如下

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

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

  • count − 返回对象的数量

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

NSMutableArray的重要方法如下 −

  • removeAllObjects − 清空数组。

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

  • removeObjectAtIndex − 这用于删除特定索引处的对象

  • 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,因此 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)
objective_c_foundation_framework.html