首页 > 其他分享 >OC之【NSMutableDictionary的使用】

OC之【NSMutableDictionary的使用】

时间:2022-12-09 15:02:59浏览次数:36  
标签:stu1 OC NSMutableDictionary k1 other dict Student 使用 字典

main.m文件
#import <Foundation/Foundation.h>
#import "Student.h"

可变字典的使用
void
//创建一个空的字典
NSMutableDictionary *dict = [NSMutableDictionarydictionary];
Student *stu1 = [StudentstudentWithName:@"stu1"];
Student *stu2= [StudentstudentWithName:@"stu2"];

// 添加元素
// stu1的计数器会+1
setObject:stu1 forKey:@"k1"];
NSLog(@"stu1:%zi", [stu1retainCount]);

// 添加其他字典other到当前字典dict中
NSDictionary *other = [NSDictionarydictionaryWithObject:@"v2"forKey:@"k2"];
[dict addEntriesFromDictionary:other];

//删除所有的键值对
// [dict removeAllObjects];

// 删除k1对应的元素stu1,stu1会做一次release操作
[dict removeObjectForKey:@"k1"];
NSLog(@"stu1:%zi", [stu1retainCount]);

// 删除多个key对应的value
// [dict removeObjectsForKeys:[NSArray arrayWithObject:@"k1"]];

// 字典被销毁时,内部的所有key和value计数器都会-1,也就是说stu1会release一次
}

int main(int argc,const char
{

@autoreleasepool {
dictUse();
}
return 0;
}

标签:stu1,OC,NSMutableDictionary,k1,other,dict,Student,使用,字典
From: https://blog.51cto.com/u_15907570/5925317

相关文章

  • OC之【NSValue的使用】
    #import<Foundation/Foundation.h>voidCGPointpoint=CGPointMake(10,10);//将结构体变量包装成一个对象NSValue*value=[NSValuevalueWithPoint:point];......
  • OC之【enum枚举】
    void//定义一种枚举类型enum//定义一个枚举变量senumSeasons=winter;}void//定义枚举类型的同时定义一个枚举变量senumSeason{spring,summer,......
  • OC之【NSDate使用】
    #import<Foundation/Foundation.h>日期创建void//date方法返回的就是当前时间(now)NSDate*date=[NSDatedate];//now:21:09:40//date:21:09:50......
  • OC之【内存管理】
    Student@synthesizeage=_age;//在xcode4.5以上环境下可以省略-(void)dealloc{@"%@被销毁了",self);super//一定要调用super的dealloc方法,而且最好放在最......
  • OC之【NSObject使用】
    main.m文件#import<Foundation/Foundation.h>#import"Student.h"#import"Person.h"常用方法voidStudent*stu=[[[Studentalloc]init]autorelease];//isKin......
  • OC之【@property的用法】
    1.这里的retain代表:在set方法中,release旧值,retain新值(nonatomic,retain)Book*book;(retain)Card*card;代表只生成get方法的声明默认是readwrite,同时生成get和set......
  • OC之【objective-c中结构体】
    #import<Foundation/Foundation.h>void//定义了Date这种结构体类型structintintint};//定义结构体变量structDated={2013,4,5};day=6;}voi......
  • Java使用Steam流对数组进行排序
    原文地址:Java使用Steam流对数组进行排序-Stars-One的杂货小窝简单记下笔记,不是啥难的东西sorted()方法里传了一个比较器的接口Filefile=newFile("D:\\temp\\db_ba......
  • vscode使用chatGPT
    vscode使用chatGPT一、下载chatPGT在拓展中找到chatGPT,我这里下载的是中文版二、使用1.使用快捷键ctrl+shift+p进行查找chatGPT2.点击请输入问题3.输入你的问题,......
  • C# OpenProtocol 开放以太网协议 读写数据 订阅数据
    主要使用的软件是HslCommunication关于这个软件的本身,详细可以参考下面的地址:github地址:https://github.com/dathlin/HslCommunication官网:http://www.hslcommunicatio......