數據存儲及檢索是任何程式中最重要的一個操作。 在Objective-C中,通常不會依賴鏈表等結構,因為它會使工作變得複雜。一般使用像NSArray,NSSet,NSDictionary及其可變形式的集合。
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
在上面的程式中,可以看到NSMutableArray和NSArray之間的簡單區別,並在可變數組中分配後插入一個字串。
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)
