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.htm
广告

© . All rights reserved.