IOS之字典总结
来源:互联网 发布:java异步调用方法 编辑:程序博客网 时间:2024/06/06 13:49
对列表中的对象进行分组,分组方式使用键值对
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;
}
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;
}
0 0
- IOS之字典总结
- IOS开发之字典
- ios之遍历字典
- OC总结之字典
- IOS之字典转模型
- iOS 工作之余小总结---(四)-(4)可变与不可变字典
- IOS--OC-- 字典 数组 集 总结
- iOS开发之 字典和可变字典初解
- 6.ios之字典与模型
- iOS开发之数组、字典、集合
- iOS开发之数组、字典、集合
- iOS数据处理之字典转模型
- iOS开发之字典转模型
- iOS——之数组 字典 枚举
- iOS学习笔记之字典排序
- 【IOS 开发学习总结-OC-31】★★OC之foundation 框架——字典(NSDictionary 与 NSMutableDictionary)
- ios开发之小总结「类与对象的判断方法和遍历字典的方法」
- ios开发之小总结「类与对象的判断方法和遍历字典的方法」
- Spring动态多数据源实例Demo
- JDK里的设计模式
- 中兴GWH-11 ZXV10 H108B的AD路由器获取超级用户密码
- JAVA正则表达式 Pattern和Matcher
- Spring Bean中的lazy-init
- IOS之字典总结
- 左划菜单SlidingMenu
- GitHub中"watch" "star" "fork"
- Mysql优化和小技巧等
- Java网络通信之HttpClient
- 关于Cannot construct org.apache.maven.plugin.war.util.WebappStructure maven打包问题解决
- iPhone屏幕适配,历史及现状
- Cocos2d-x3.2与OpenGL渲染总结(一)Cocos2d-x3.2的渲染流程
- 【ShareSDK】ShareSDK for Android 2.5.8已经发布