//
// main.m
// Foundation框架(10)
//
// Created by XinYou on 15-1-21.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import
#import "Student.h"
#pragma mark 可变字典的使用
void mutableDictUse(){
// 创建一个空的字典
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
Student *stu1 = [Student studentWithName:@"zhangsan"];
Student *stu2= [Student studentWithName:@"lisi"];
// 添加元素
// stu1的计数器会+1
[dict setObject:stu1 forKey:@"k1"];
NSLog(@"stu1的计数器:%zi", [stu1 retainCount]);
// 添加其他字典other到当前字典dict中
NSDictionary *other = [NSDictionary dictionaryWithObject:@"v2" forKey:@"k2"];
[dict addEntriesFromDictionary:other];
// 删除所有的键值对
// [dict removeAllObjects];
// 删除k1对应的元素stu1,stu1会做一次release操作
[dict removeObjectForKey:@"k1"];
NSLog(@"stu1的计数器:%zi", [stu1 retainCount]);
// 删除多个key对应的value
// [dict removeObjectsForKeys:[NSArray arrayWithObject:@"k1"]];
// 字典被销毁时,内部的所有key和value计数器都会-1,也就是说stu1会release一次
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
mutableDictUse();
}
return 0;
}
Foundation框架(10)——NSMutableDictionary的创建、添加、删除、内存管理
最新推荐文章于 2024-05-13 21:30:50 发布