//
// main.m
// Foundation框架(9)
//
// Created by XinYou on 15-1-21.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import
#import "Student.h"
#pragma mark 字典的初始化
void dictCreate(){
// 第一种初始化方式,NSDictionary是不可变的
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"v1" forKey:@"k1"];
// 最常用的初始化方式
dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3", nil];
NSArray *values = [NSArray arrayWithObjects:@"v7", @"v8", @"v9", nil];
NSArray *keys = [NSArray arrayWithObjects:@"k7", @"k8", @"k9", nil];
// 第三种初始化方式
dict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
NSLog(@"dict = %@", dict);
}
#pragma mark 字典的基本用法
void dictUse(){
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3", nil];
// count是计算有多少键值对
NSLog(@"count = %zi", [dict count]);// count = 3
// 取出某个键对应的值
id obj = [dict objectForKey:@"k2"];
NSLog(@"obj = %@", obj);// obj = v2
// 将字典写入文件中
NSString *path = @"/Users/xinyou/Desktop/dict.xml";
[dict writeToFile:path atomically:YES];
// 从文件中读取内容
dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"dict = %@", dict);
}
#pragma mark 字典的其他用法
void dictUse2(){
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3", nil];
// 返回所有的key
NSArray *keys = [dict allKeys];
NSLog(@"keys = %@", keys);
// 返回所有的value
NSArray *values = [dict allValues];
NSLog(@"values = %@", values);
// 根据多个key取出对应的多个value
// 当key找不到对应的value时,取默认值"noValue"
values = [dict objectsForKeys:[NSArray arrayWithObjects:@"k1", @"k2", @"k6", nil] notFoundMarker:@"noValue"];
NSLog(@"values = %@", values);
}
#pragma mark 遍历字典
void dictFor(){
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3", nil];
// 注意:这里遍历的是字典的所有key,而不是键值对
for(id key in dict){
id value = [dict objectForKey:key];
NSLog(@"%@ = %@", key, value);
}
}
#pragma mark 遍历字典2
void dictFor2(){
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3", nil];
// 获取key迭代器
NSEnumerator *enume = [dict keyEnumerator];
id key = nil;
while (key = [enume nextObject]) {
id value = [dict objectForKey:key];
NSLog(@"%@ = %@", key, value);
}
// 获取value迭代器
// [dict objectEnumerator];
}
#pragma mark 遍历字典3
void dictFor3(){
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3", nil];
// 使用block对字典进行遍历
[dict enumerateKeysAndObjectsUsingBlock:
^(id key, id obj, BOOL *stop) {
NSLog(@"%@ = %@", key, obj);
}];
}
#pragma mark 字典的内存管理
void dictMemory(){
Student *stu1 = [Student studentWithName:@"zhangsan"];
Student *stu2 = [Student studentWithName:@"lisi"];
Student *stu3 = [Student studentWithName:@"wangwu"];
// 一个对象成为字典的key或者value时,会做一次retain操作,也就是这个对象的计数器会+1
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
stu1, @"k1",
stu2, @"k2",
stu3, @"k3", nil];
// 当字典被销毁时,里面的所有key和value都会做一次release操作,也就是计数器会-1
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
// dictCreate();
// dictUse();
// dictUse2();
// dictFor();
// dictFor2();
// dictFor3();
dictMemory();
}
return 0;
}
//
// Student.h
// Foundation框架(9)
//
// Created by XinYou on 15-1-21.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import
@interface Student : NSObject
@property (nonatomic, retain) NSString *name;
+ (id)studentWithName:(NSString *)name;
@end
//
// Student.m
// Foundation框架(9)
//
// Created by XinYou on 15-1-21.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "Student.h"
@implementation Student
+ (id)studentWithName:(NSString *)name{
Student *stu = [[[Student alloc] init] autorelease];
stu.name = name;
return stu;
}
- (void)dealloc{
NSLog(@"%@被销毁了", _name);
[_name release];
[super dealloc];
}
@end
Foundation框架(9)——NSDictionary的初始化、用法、遍历、内存管理
最新推荐文章于 2020-08-23 16:55:00 发布