Objective-C数据存储

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

1. 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;
}

执行上面示例代码,得到以下结果:

2018-11-16 04:06:24.683 main[69957] The object in array at Index 0 is string1
2018-11-16 04:06:24.684 main[69957] The object in mutableArray at Index 0 is string

在上面的程序中,可以看到NSMutableArrayNSArray之间的简单区别,并在可变数组中分配后插入一个字符串。

2. 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;
}

执行上面示例代码,得到以下结果 -

2018-11-16 04:11:49.006 main[100527] The object for key, key1 in dictionary is string1
2018-11-16 04:11:49.007 main[100527] The object for key, key1 in mutableDictionary is string

3. 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:@"yii", @"bai",@".com",nil];
   NSArray *setArray = [set allObjects];
   NSLog(@"The objects in set are %@",setArray);

   NSMutableSet *mutableSet = [[NSMutableSet alloc]init];
   [mutableSet addObject:@"zaixian"];
   setArray = [mutableSet allObjects];
   NSLog(@"The objects in mutableSet are %@",setArray);

   [pool drain];
   return 0;
}

执行上面示例代码,得到以下结果 -

2018-11-16 04:46:06.667 main[196448] The objects in set are (".com", bai, yii)
2018-11-16 04:46:06.669 main[196448] The objects in mutableSet are (zaixian)

上一篇: Objective_C基础框架 下一篇: Objective-C快速枚举