字典:NSDictionary
字典就是关键字及其定义(描述)的集合。Cocoa中的实现字典的集合NSDictionary在给定的关键字(通常是一个NSString)下存储一个数值(可以是任何类型的对象)。然后你就可以用这个关键字来查找相应的数值。
不同于数组,字典(也被称为散列表或关联数组)使用的是键查询的优化存储方式。它可以立即找出要查询的数据,而不需要遍历整个数组进行查找。
可使用dictionaryWithObjectsAndKeys来创建字典
查询字典的值:objectForKey
NSMutableDictionary的dictionary方法可以创建一个可变字典,也可以使用dictionaryWithCapaticy:。
使用 setObject:forkey: 方法添加字典元素,如果关键字已存在,则用新植替换旧值。
类似的,NSMutableDictionary类允许随意添加或删除字典元素。
添加元素:setObject:forkey:
删除元素:removeObjectForKey:
Example:
//
// MyClass.h
// FoundationKit4
//
// Created by Elf Sundae on 10/22/10.
// Copyright 2010 Control-Strength. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface MyClass : NSObject
{
NSString *firstName;
NSString *lastName;
}
- (void) setFirstName:(NSString *)m_firstName;
- (NSString *) firstName;
- (void) setLastName: (NSString *) m_lastName;
- (NSString *) lastName;
@end
//
// MyClass.m
// FoundationKit4
//
// Created by Elf Sundae on 10/22/10.
// Copyright 2010 Control-Strength. All rights reserved.
//
#import "MyClass.h"
@implementation MyClass
- (void) setFirstName:(NSString *)m_firstName{
firstName = m_firstName;
}
- (NSString *) firstName{
return firstName;
}
- (void) setLastName: (NSString *) m_lastName{
lastName = m_lastName;
}
- (NSString *) lastName{
return lastName;
}
- (NSString *) description
{
if (firstName == nil || lastName == nil) {
return @"No Name found.";
} else {
return [NSString stringWithFormat:@"%@ %@",
firstName,lastName];
}
}
@end
//
// 示例字典(NSDictionary,NSMutableDictionary)操作
// Elf Sundae 10/22/2010
//
#import <Foundation/Foundation.h>
#import "MyClass.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// 创建字典:dictionaryWithObjectsAndKeys:
MyClass *my1 = [MyClass new];
MyClass *my2 = [MyClass new];
MyClass *my3 = [MyClass new];
MyClass *my4 = [MyClass new];
NSDictionary *myClassDict;
myClassDict = [NSDictionary dictionaryWithObjectsAndKeys:
my1, @"my1",
my2, @"my2",
my3, @"my3",
my4, @"my4", nil];
// 获取值 objectForKey
MyClass *sub = [myClassDict objectForKey: @"my3"];
if (sub == nil) {
exit(1);
}
[sub setFirstName:@"Elf"];
[sub setLastName:@"Sundae"];
NSLog(@"修改数据: %@",sub);
// 遍历字典
NSLog(@"***遍历字典myClassDict如下:");
for (id key in myClassDict)
{
NSLog(@"key: %@ ,value: %@",key,[myClassDict objectForKey:key]);
}
NSLog(@"***遍历字典myClassDict结束。");
// MARK: *** 添加新元素 ***
// NSDictionary无法添加或删除元素,可以使用NSMutableDictionary.
NSMutableDictionary *myNewDict = [NSMutableDictionary dictionary];
// 将原有字典添加到新字典的一对元素
//[myNewDict setObject:myClassDic forKey:@"旧的不可变字典myClassDic"];
// 遍历添加已有数据(原字典)
for (id key in myClassDict)
{
[myNewDict setObject: [myClassDict objectForKey:key]
forKey:key];
}
NSString *newkey = @"newKey";
NSString *newValue = @"This is a new Value.";
[myNewDict setObject:newValue forKey:newkey];
// 遍历myNewDict
NSLog(@"***遍历字典myNewDict如下:");
for (id key in myNewDict)
{
NSLog(@"key: %@ ,value: %@",key,[myNewDict objectForKey:key]);
}
NSLog(@"***遍历字典myNewDict结束。");
// 删除元素
[myNewDict removeObjectForKey: @"newKey"];
// 遍历myNewDict
NSLog(@"***遍历字典myNewDict如下:");
for (id key in myNewDict)
{
NSLog(@"key: %@ ,value: %@",key,[myNewDict objectForKey:key]);
}
NSLog(@"***遍历字典myNewDict结束。");
[pool drain];
return 0;
}
// 输出结果(省略日期 时间等信息)
修改数据: Elf Sundae
***遍历字典myClassDict如下:
key: my3 ,value: Elf Sundae
key: my4 ,value: No Name found.
key: my1 ,value: No Name found.
key: my2 ,value: No Name found.
***遍历字典myClassDict结束。
***遍历字典myNewDict如下:
key: newKey ,value: This is a new Value.
key: my3 ,value: Elf Sundae
key: my4 ,value: No Name found.
key: my1 ,value: No Name found.
key: my2 ,value: No Name found.
***遍历字典myNewDict结束。
***遍历字典myNewDict如下:
key: my3 ,value: Elf Sundae
key: my4 ,value: No Name found.
key: my1 ,value: No Name found.
key: my2 ,value: No Name found.
***遍历字典myNewDict结束。