objective-c dictionary(字典)
来源:互联网 发布:淘宝网店管理技巧 编辑:程序博客网 时间:2024/06/06 10:06
dictionary是由键-对象组成的数据集合。正如在词典中查找单词的定义一样,可通过对象的键从objective-c词典中获取所需的值。
词典中的键必须是单值的,尽管它们通常是字符串,但还可以是任何对象类型。和键关联的值可以是任何对象类型,但它们不能为nil。
下面是一个使用词典的类:
- #import <Foundation/NSDictionary.h>
- #import <Foundation/NSObject.h>
- #import <Foundation/NSString.h>
- #import <Foundation/NSAutoreleasePool.h>
- int main(int argc, const char *argv[])
- {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- //immutable dictionary
- NSDictionary *tires = [NSDictionary dictionaryWithObjectsAndKeys :
- @"front-left",@"t1" , @"front-right",@"t2" ,
- @"back-left",@"t3" , @"back-right",@"t4" ,nil];
- //display immutable dictionary
- NSLog(@"t1: %@",[tires objectForKey: @"t1"]);
- NSLog(@"t2: %@",[tires objectForKey: @"t2"]);
- NSLog(@"t3: %@",[tires objectForKey: @"t3"]);
- NSLog(@"t4: %@",[tires objectForKey: @"t4"]);
- //mutable dictionary
- NSMutableDictionary *glossary = [NSMutableDictionary dictionary];
- //Store three entries in the glossary
- //use setObject:forKey: method to set key/value
- [glossary setObject: @"A class defined so other classes can inherit from it"
- forKey: @"abstract class"];
- [glossary setObject: @"To implment all the methods defined in a protocol"
- forKey: @"adopt"];
- [glossary setObject: @"Storing an object for later use"
- forKey: @"archiving"];
- //Retrieve and display them
- //use objectForKey:key method to retrieve the value
- NSLog(@"abstract class: %@",[glossary objectForKey: @"abstract class"]);
- NSLog(@"adopt: %@",[glossary objectForKey: @"adopt"]);
- NSLog(@"archiving: %@",[glossary objectForKey: @"archiving"]);
- [pool drain];
- return 0;
- }
常用的NSDictionary方法:
+(id) dictionaryWithObjectsAndKeys: 使用键-对象{key1,obj1}、{key2,obj2}...创建词典
obj1,key1,obj2,key2,...,nil;
-(id) initWithObjectsAndKeys: 将新分配的词典初始化为键-对象对{key1,obj1}{key2,obj2}...创建词典
obj1,key1,obj2,key2...,nil;
-(unsigned int) count 返回词典中的记录数
-(NSEnumerator *) keyEnumerator 为词典中所有键返回一个NSEnumerator对象
-(NSArray *) keysSortedByVlaueUsingSelector: 返回词典中的键数组,它根据selector 指定的比较方法进行了排序
(SEL) selector
-(id) objectForKey:key 返回指定key的对象
常用的NSMutableDictionary方法:
+(id) dictionaryWithCapacity:size 使用一个初始指定的size创建可变词典
-(id) initWithCapacity:size 将新分配的词典初始化为指定的size
-(void) removeAllObjects 删除词典中所有的记录
-(void) removeObjectForKey:key 删除词曲中指定key对应的记录
-(void) setObject: obj forKey: key 向词典为key 键添加obj,如果key已存丰,则替换该值
- objective-c dictionary(字典)
- objective-c dictionary(字典)
- objective-c dictionary(字典)
- Object C Dictionary字典
- c#,字典,Dictionary
- C#Dictionary 字典的用法
- objective-c字典
- Objective-C 字典小结
- objective-c NSDictionary(字典)
- Objective-C 字典
- Objective-c-字典使用
- dictionary字典
- dictionary字典
- Dictionary 字典
- dictionary字典
- Objective-C 省市区 字典 封装
- Objective-C 省市区 字典 封装
- Objective-C字典使用详解
- [从头学数学] 第27节 总复习
- swift安装cocoapod遇到的一些坑
- python中常用的MySQL操作以及命令
- MVC 路由 及 遇到问题记录
- Shiro web 和spring
- objective-c dictionary(字典)
- 静态分派与动态分派
- 打开 Android Studio unable to access android sdk add-on list问题
- ArrayList.add(int index, E e)的数组越界
- Boost库的使用
- Java中的数字处理
- opengl基础知识 关于四个变换的顺序
- 关于Calendar导致的跨年时间问题
- 修改语句