码迷,mamicode.com
首页 > 移动开发 > 详细

IOS之字典总结

时间:2015-02-03 17:25:58      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:apple   dictionary   

对列表中的对象进行分组,分组方式使用键值对
NSDictionary  NSMutableDictionary类创建带有键的对象列表。
如果需要向字典中添加或删除对象,就要使用NSMutableDictionary
1字典的创建alloc构造函数或者dictionaryWithObjects:forKeys
使用NSDictionary创建的字典一旦创建就无法改变,使用NSMutableDictionary创建的字典可以改变。
NSArray *listOfObjects=[NSArray arrayWithObjects:@"Hello world",@"Bonjour tout le monde",@"Hola Mundo",nil];
NSArray *listOfKeys=[NSArray arrayWithObjects:@"english",@"french",@"spanish",nil];
NSDictionary *dictionary2=[NSDictionary dictionaryWithObjects:listOfObjects forKeys:listOfKeys];
  构造函数                    键                    值
#import <Foundation/Foundation.h>
int main(int argc,const char *argv[]){
@autoreleasepoo;{
NSDictionart *dictionary1=[[NSDictionary alloc]init];
NSArray *listOfObjects=[NSArray arrayWithObjects:@"Hello World",@"Bonjour tout le monde",@"Hola mundo",nil];
NSArray *listOfKeys=[NSArray arayWithObjects:@"english",@"french",@"spanish",nil];
NSDictionary *dictionary2=[NSDictionary dictionaryWithObjects:listOfObject forKeys:listOfKeys];
   NSLog(@"dictionary2=%@",dictionary2);
}
return 0;
}
//打印出来的消息如下
%@对应的值如下:
english="Hello World";
french ="Bonjour tout le monde";
spanish="Hola Mundo";


//问题:想要获得指向字典中的对象的引用以便访问它们的属性或是向对象发送消息。
使用objectForKey:方法可以获得你提供的键相对应的引用:
NSString *helloWorld=[dictionary objectForKey:@"english"];






/**
*example
*/
#import <Foundation/Foundation.h>
int main(int argc,const *argv[]){
autoreleasepool{
NSArray *listOfObjects=[NSArray arrayWithObjects:@"Hello World",@"Bonjour tout le monde",@"Hola Moundo",nil];
NSArray *listOfKeys=[NSArray arrayWithObjects:@"english",@"french",@"spanish",nil];
NSDictionary *dictionary=[NSDictionary dictionaryWithObjects:listOfObjects forKeys:listOfKeys];
NSString *helloworld=[dictionary objectForKey:@"english"];
NSLog(@"%@",helloworld);
}
return 0;
}


/**
*获取字典中元素的数量NSDictionary类中的count属性可以得到
*/
#import <Foundation/Foundation.h>
int main(int argc,const char *argv[]){
@autoreleasepool{
NSArray *listOfObjects=[NSArray arrayWithObjects:@"Hello World",@"Bonjour tout le monde",@"Hola Mundo",nil];
NSDictionary *listOfKeys=[NSArray arrayWithObjects:@"english",@"french",@"spanish",nil];
NSDictionary *dictionary=[NSDictionary dictionaryWithObjects::listOfObjects forKeys:listOfKeys];
NSUInteger count=dictionary.count;
NSLog(@"The dictionary contains %lu items",count);
}
return 0;
}


/**
*遍历字典向字典中的每个对象发送相同的消息或是访问同样的属性
*allValues将字典转换为数组
*1使用for-each遍历数组
*2使用enumerateKeysAndObjectsUsingBlock:处理字典中的每个对象
*/
for(NSString *s in [dictionary allValues]){
NSLog(@"value:%@",s);
}
//allValues会将键值做为数组返回
/**
*还可以使用块通过emumerateKeysAndObjectsUsingBlock
*/
[dictionary emuerateKeysAndObjectsUsingBlock:^(id key,id obj,BOOL *stop){
NSLog(@"key=%@",key,obj);
}];


/**
*遍历
*/
#import <Foundation/Foudation.h>
int main(int argc,const char *argv[]){
@autoreleasepool{
 NSArray *listOfObjects=[NSArray arrayWithObjects:@"Hello World",@"Bonjour tout le monde",@"Hola Mundo",nil];
 NSArray *listOfKeys=[NSArray arrayWithObjects:@"english",@"french",@"spanish",nil];
 NSDictionary *dictionary=[NSDictionary dictionaryWithObjects:listOfObjects forKeys:listOfKeys[;
 for(NSString *s in [dictionary allKeys]){
 NSLog(@"key:%@",s);
 }
 [dictionary enumerateKeysAndObjectsUsingBlock:^(id key,id obj,BOOL *stop){
 NSLog(@"key = %@ and obj = %@",key,obj);
 }];
}
return 0;
}


/*
*操纵字典在字典中添加、删除和插入对象。方式使用NSMutableDictionary
*/
//对象的创建:
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
//添加键值对
[dictionary setObject:@"Hello World" forKey:@"english"];
[dictionary setObject:@"Bonjour tout le mode" forKey:@"french"];
[dictionary setObject:@"Hola Mundo forKey:@"spanish"];








[dictionary removeObjectForKey@"french"];








/**
*example
*/
#import <Foundation/Foundation.h>
int main(int argc,const char *argv[]){
@autoreleasepool{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
[dictionary setObject:@"Hello World" forKey:@"english"];
[dictionary setObject:@"Bonjour tout le monde" forKey:@"french"];
[dictionary setObject:@"Hola Mundo" forKey:@"spanish"];
NSLog(@"OBJECT ADDED TO DICTIONARY:%@",dictionary);
[dictionary removeObjectForKey:@"french"];
NSLog(@"OBJECTED REMOVED FROM DICTIONARY%@",dictionary);
[dictionary removeAllObjects];
NSLog(@"ALL OBJECTS REMOVED FROM DICTIONARY:%@",dictionary);
}
return 0;
}


//第一次添加之后结果为:
OBJECT ADDED TO DICTIONARY:{
english="Hello World";
french="Bonjour tout le monde";
spanish="Hola Mundo";
}
//第一次移除了一个元素
OBJECT REMOVED FROM DICTIONARY:{
english ="Hello World";
spanish="Hola Mundo";
}
//最后所有的元素被移除
ALL OBJECTS REMOVED FROM DICTIONARY:{
}




//将字典中的对象保存到文件系统中以供日后或其他程序使用。
//NSDictionary中有 writeToFile可以将字典写到文件里
NSArray *listOfObjects=[NSArray arrayWithObjects:@"Hello World",@"Bonjour tout le monde",@"Hola Mundo",nil];
NSArray *listOfKeys=[NSArray arrayWithObjects:@"english",@"french",@"spanish",nil];
NSDictionary *dictionary=[NSDictionary dictionaryWithObjects:listOfObjects forKeys:listOfKeys];


NSString filePathName=@"/Users/Shared/dictionary.txt";
[dictionary writeToFile:filePathName atomically:YES];








/**
*example
*/


#import <Foundation/Foundation.h>
int main(int argc,const char *argv[]){
@autoreleasepool{
NSArray *listOfObject=[NSArray arrayWithObjects:@"Hello World",@"Bonjour tout le monde",@"Hola Mundo",nil];
NSArray *listOfKeys = [NSArray arrayWithObjects:@"english",@"french",@"spanish",nil];
NSDictionary *dictionary=[NSDictionary dictionaryWithObjects:listOfObjects forKeys:listOfKeys];
NSString filePathName=@"/Users/Shared/dictionary.txt";
[dictionary writeToFile:filePathName atomically:YES];
}
return 0;
}
/**
*文件中的内容
*/
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDS/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
 <key>english</key>
 <string>Hello World</string>
 <key>french</key>
 <string>Bonjour tout le monde</string>
 <key>spanish</key>
 <string>Hola Mundo</string>
 </dict>
 </plist>


/**
*读文件initWithContentsOfFile
*/
NSString *filePathName=@"/Users/Shared/dictionary.txt";
NSDictionary *dictionary=[[NSDictionary alloc]initWithContentsOfFile:filePathName];


/**
*example
*/
#import <Foundation/Foundation.h>
int main(int argc,const char *argv[]){
@autoreleasepool{
NSString *filePathName=@"/Users/Shared/dictionary.txt";
NSDictionary *dictionary=[NSDictionary alloc]initWithContentsOfFile:filePathName];
NSLog(@"dictionary:%@",dictionary);
}
 return 0;
}























































IOS之字典总结

标签:apple   dictionary   

原文地址:http://blog.csdn.net/songjunyan/article/details/43450203

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!