NSMutableDictionary的用法,oc怎么构建一个对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NSMutableDictionary的用法,oc怎么构建一个对象相关的知识,希望对你有一定的参考价值。

参考技术A 在oc里,构建一个对象很简单,也许你会采用和java那样面向对象都见一个类,也许我们会有更简单的办法构建一个类
譬如,我构建了一个列表,列表里面存在用户对象,用户对象包含
name 张三 //姓名
id 123456 //编号
context 他是个男的 //信息
...等信息
我们可以这样来沟槽这个对象:

NSMutableDictionary * dicc = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
“张三”,@"name",
“123456”,@"id",
·“他是个男的”,@"context",
...
nil];
OK!,下面给大家共享一段代码

-(void)setchatListInfo:(NSString*)username userId:(NSString*)userid context:(NSString*)text viever:(id)viewer

if ([chatListInfo count]==0)
NSMutableArray * arr = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableDictionary *dicc=nil;
if (text!=nil)
[arr addObject:text];
dicc = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
username,@"name",
userid,@"id",
arr,@"context",
@"1",@"count",
viewer,@"view",
nil];
else
dicc = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
username,@"name",
userid,@"id",
arr,@"context",
@"0",@"count",
viewer,@"view",
nil];

[chatListInfo addObject:dicc];
[arr release];
[dicc release];
[chatList reloadData];
return;

int count = 10000;
for (NSDictionary *dic in chatListInfo)
if ([[dic objectForKey:@"id"] isEqualToString:userid])
NSMutableArray *arr = [dic objectForKey:@"context"];
if (![text isEqualToString:@""] && text!=nil)
[arr addObject:text];
[dic setValue:arr forKey:@"context"];
count = [[dic objectForKey:@"count"] intValue];
[dic setValue:[NSString stringWithFormat:@"%i",count+1] forKey:@"count"];

[chatList reloadData];
return;


if (count == 10000)
NSMutableArray * arr = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableDictionary *dicc=nil;
if (text!=nil)
[arr addObject:text];
dicc = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
username,@"name",
userid,@"id",
arr,@"context",
@"1",@"count",
viewer,@"view",
nil];
else
dicc = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
username,@"name",
userid,@"id",
arr,@"context",
@"0",@"count",
viewer,@"view",
nil];

[chatListInfo addObject:dicc];
[arr release];
[dicc release];

[chatList reloadData];本回答被提问者和网友采纳

以上是关于NSMutableDictionary的用法,oc怎么构建一个对象的主要内容,如果未能解决你的问题,请参考以下文章

转NSDictionary以及NSMutableDictionary的用法

OC基础--常用类的初步介绍与简单实用之集合类

IOS Intro - NSDictionary and NSMutableDictionary

OC + RAC Command 命令的用法

Swift3.0和OC交互的最新用法

Swift3.0和OC交互的最新用法