NSDictionary

 

//通过唯一的key找到对应的value(键 值)

//不可变

//通过索引找到内容

//也是只能放OC对象,不能放基本数据类型和空值

#pragma mark create dictionary

void dictCreate(){

//most commom used +

NSdictionary *dict=[NSDictionary dictionaryWithObject:@”v” forKey:@”key”];

//+method can’t change

dict=[NSDictionary dictoryWithObjectsAndKeys:

@”v1”,@”k1”,

@”v2”,@”k2”,

@”v3”,@”k3”,

@”v4”,@”k4”,

,nil];

NSArray *objects=[NSArray arrayWithObjects:@”v1”,@”v2”,@”v3”,nil];

NSArray *keys=[NSArray arrayWithObjects:@”k1”,@”k2”,@”k3”,nil];

dict=[NSDictionary dictoryWithObjects:objects forKeys:keys];

NSLog(@“%@”,dict);     //array->()  dict->{}

}

#pragma mark dict use

void dictUse(){

NSDictionary  *dict=[NSDictionary dictoryWithObjectsAndKeys:

@”v1”,@”k1”,

@”v2”,@”k2”,

@”v3”,@”k3”,

@”v4”,@”k4”,

,nil];

NSDictionary NSLog(@”count=%zi”,[dict count]);     //dict.count  how many couples

//通过key找value

NSLog(@”k2-> %@”,[dict objectForKey:@”k2”]);//只能取值不能改值

//can write a dictionary to a file

NSString *path =@”/User/apple/Desktop/dict.xml”;

[dict writeToFile:path atomically:YES];

//从文件中读取字典对象(按照格式才能)

NSDictionary dict2=[NSDictionary dictonaryWithContentOfFile:path];

}

#pragma mark use2

void dictUse2(){

dict=[NSDictionary dictoryWithObjectsAndKeys:

@”v1”,@”k1”,

@”v2”,@”k2”,

@”v3”,@”k3”,

@”v1”,@”k4”,

,nil];

NSLog(@”dict all keys is”,[dict allKeys]);//他是无序的,需要的话NSArray去排序

//key是唯一 value不一定  不能一对多 可以多对一

//当一对多时返回所有的value

//all values

NSArray *array1=[dict allKeysForObject:@”v1”];

NSArray *objects=[dict allValues];

//根据多个key取出多个value

id objects=[dict objectForKeys:[NSArray arrayWithObjects:@”k1”,@”k2”,@”k4”,nil] notFoundMarker:@“not-found”]; //v1 v2

//notFoundMarker 如果找不到对应的值的时候就用marker来替代

}

#pragma mark traverse dictionary

void dictTraverse(){

dict=[NSDictionary dictoryWithObjectsAndKeys:

@”v1”,@”k1”,

@”v2”,@”k2”,

@”v3”,@”k3”,

@”v1”,@”k4”,

,nil];

//1

for(id  key in dict)//因为key是唯一{

id value=[dict objectForKey:key];

NSLog(@”%@=%@”,key,value);

}

}

#pragma mark  traverse dic with  enumerator

void dictTraverseDict(){

//2迭代器 key

dict=[NSDictionary dictoryWithObjectsAndKeys:

@”v1”,@”k1”,

@”v2”,@”k2”,

@”v3”,@”k3”,

@”v1”,@”k4”,

,nil];

NSEnumerator *enumer=[dict keyEnumerator];

id key=nil;

while(key=[enumer nextObject]){

id value=[dict objectForKey:key];

NSLog(@”%@=%@”,key,value);

}

//对象迭代器 value

NSEnumerator *enumer=[dict objectEnumerator];

}

#pragma mark  traverse dic with block

void dictTraverseDict(){

dict=[NSDictionary dictoryWithObjectsAndKeys:

@”v1”,@”k1”,

@”v2”,@”k2”,

@”v3”,@”k3”,

@”v1”,@”k4”,

,nil];

[dict enumerateKeysAndObjectsUsngBlock:^(id key,id obj,BOOL stop){

NSLog(@”%@=%@”,key,obj);

}];

}

#pragma mark sort key


#pragma mark memory management

Student.h

@property (nonatomic,retain)NSString * name;

+(id)studentWithName:(NSString *)name;

Student.m

+(id)studentWithName:(NSString *)name{

Student *stu=[[Student alloc]init];

stu.name=name;

return [stu autorelease];

}

-(void)dealloc{

NSLog(@”%@ is destroied”,self.name);//_name

[-_name release];

[super dealloc];

}

main.m

#import “Student.h”

//不管是key 还是value  都会对对象进行retain操作

void dicMemory(){

Student *stu1=[Student studentWithName:@”stu1”];

Student *stu2=[Student studentWithName:@”stu2”];

Student *stu3=[Student studentWithName:@”stu3”];

NSDictionary *dict=[NSDictionary dictoryWithObjectsAndKeys:

stu1,@”k1”,

stu2,@”k2”,

stu3,@”k3”,

,nil];

//when the dictionary is destroied key and value all released once

//但是字典不需我们销毁,注意是否是自动释放 

}

原文地址:https://www.cnblogs.com/yesihoang/p/4549423.html