objective-c dictionary(字典)
2016-01-04 14:45
609 查看
dictionary是由键-对象组成的数据集合。正如在词典中查找单词的定义一样,可通过对象的键从objective-c词典中获取所需的值。
词典中的键必须是单值的,尽管它们通常是字符串,但还可以是任何对象类型。和键关联的值可以是任何对象类型,但它们不能为nil。
下面是一个使用词典的类:
view plaincopy
to clipboardprint?
#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已存丰,则替换该值
词典中的键必须是单值的,尽管它们通常是字符串,但还可以是任何对象类型。和键关联的值可以是任何对象类型,但它们不能为nil。
下面是一个使用词典的类:
view plaincopy
to clipboardprint?
#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已存丰,则替换该值
相关文章推荐
- object-c ubuntu下开发环境搭建
- Objective-C Runtime 运行时之一到六
- Objective-C Runtime
- objective-c code style
- js判断object类型
- String.valueof(null) 与 String.valueof((Object)null)
- 探讨WaitForMultipleObjects如何突破64个句柄的限制
- Objective-C 与Swift - UIView中的- (void)drawRect:(CGRect)rect
- iOS UIView动画详解(Objective-C)
- Objective-C 去掉NSString 前后中空格
- 分享你最喜欢的技巧和提示(Xcode,Objective-C,Swift,C...等等)
- 黑马程序员——常用API详解之Object、Scanner、String
- objective-c 苹果官方NSLayoutConstraint 页面布局
- OBjective-c 第三方布局 Masonry
- GitHub Top 100 简介(Objective-C 项目 )
- Entity Framework 出现 "此 ObjectContext 实例已释放,不可再用于需要连接的操作" 的错误
- 每日一vim(18)Text-Object
- 2006-7有价值的Kean博客——Calling ObjectARX functions from a .NET Application(PInvoke)
- Objective-C开发者,一天学会swift
- 图像目标检测(Object Detection)原理与实现(三)