如何获取NSDictionary的获取第一个子元素元素

& &昨天的时候遇到一个问题,就是收到一个NSDictionary类型的数据,里面有个值是int类型。用强制类型转换(在数据前边加上(int)或者(integer))取不出值来,后来还是通过先转成再把字符串转化成int才取到的。具体代码如下:int value=[[NSString stringWithFormat:@&%@&, dict[@&count&]] intValue];
版权声明:本文为博主原创,未经博主允许不得转载。NSArray/NSDictionary: case insensitive element lookup? [NSArray / NSDictionary:不区分大小写的元素查找?] - 问题-字节技术
NSArray/NSDictionary: case insensitive element lookup?
NSArray / NSDictionary:不区分大小写的元素查找?
问题 (Question)
Is there a way to use indexOfObject: of NSArray or objectForKey: of NSDictionary without case sensitive?
So if I have an array:
@[@"Apple", @"Banana", @"Peach"]
[array indexOfObject:@"apple"]
can give me 0?
有没有一种办法来使用indexOfObject:的NSArray或objectForKey:的NSDictionary不区分大小写的?所以如果我有一个数组:@[@"Apple", @"Banana", @"Peach"]
然后[array indexOfObject:@"apple"]
可以给我0?谢谢
最佳答案 (Best Answer)
This is possible with a lot if extra work and its a bad idea. Case sensitivity is a human idea. When keys in dictionaries are strings (normally they should be) the difference between Apple and apple is a different unichar and a different hash value.
Attempting to do this the hard way would degrade performance .
That said, you might have a valid reason to do it, in which case, you could get the array of all keys, do a containsObject with caseInsensitiveCompare then use the object at index matching. But you might still find nothing.
If your keys are coming from user input or JSON or something, you might do well to sanitize them or otherwise normalize them first.
这可能是很多,如果额外的工作和一个糟糕的主意。的情况下的灵敏度是人类的想法。当钥匙在字典的字符串(通常他们应该)苹果和苹果之间的差异是不同的unichar和不同的哈希值。试图做这艰苦的道路会降低性能。就是说,你可能要做一个正当的理由,在这种情况下,你可以得到所有键的阵列,做一个containsobject与caseinsensitivecompare然后使用对象在折射率匹配。但你可能还没发现什么。如果你的钥匙是来自用户输入或JSON或什么的,你不妨对他们或他们的第一个规范。
本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:今天看啥 热点:
IOS SDK详解之NSDictionary
原创Blog,转载请注明出处
blog.csdn.net/hello_hwc
前言:本文将要讲述的内容有
1.NSDictionary 以及 NSMutableDictionary 概述
2.常用属性方法举例(不常用的本文不会涉及)
一 NSDictionary/NSMutableDictionary概述
NSDictionary提供了一种key-value的数据存储方式。总的来说,任何对象都可以作为key,只要其遵循NSCopying协议。其中,key不能相同(由isEqual来判断)。key和value都不能为nil,如果要表达一个空的值,用NSNull。NSDictionary中的值不可变。
NSMutableDictionary是NSDictionary的子类,是可变的字典。
二 NSDictionary常用的属性方法举例
2.1 创建和初始化
创建兼初始化
(instancetype)dictionaryWithContentsOfFile:(NSString *)path
(instancetype)
(instancetype)dictionaryWithDictionary:(NSDictionary *)
(instancetype)dictionaryWithObjects:(NSArray *)objects
forKeys:(NSArray *)keys
(instancetype)dictionaryWithObjectsAndKeys:(id)firstObject
-(NSDictionary *)
-(NSDictionary *)initWithContentsOfFile:(NSString *)
-(NSDictionary *)initWithDictionary:(NSDictionary *);
-(NSDictionary *)initWithObjects:(NSArray *)objects
forKeys:(NSArray *)
-(NSDictionary *)initWithObjectsAndKeys:(id)firstO
就个人而言,我比较习惯后一种。当然,快捷创建的方式不要忘记了
NSDictionary * emptyDic = [NSDictionary dictionary];
NSDictionary * firstDic = @{@"key":@"value",
@"first":@"1"};
NSDictionary * secondDic = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2",nil];
返回key-value对的个数
NSDictionary * dic = @{@"key1":@"1",
@"key2":@"2"};
NSLog(@"%d",dic.count);
2.3 isEqualToDictionary比较两个dictionary内容是否一样。
NSDictionary * dic1 = @{@"key1":@"1",
@"key2":@"2"};
NSDictionary * dic2 = @{@"key2":@"2",
@"key1":@"1"};
if ([dic1 isEqualToDictionary:dic2]) {
NSLog(@"Equal contents");
2.4 objectForKey: 和valueForKey 由属性获得内容
NSDictionary * dic1 = @{@"key1":@"1",
@"key2":@"2"};
NSLog(@"%@",[dic1 objectForKey:@"key1"]);
NSLog(@"%@",[dic1 valueForKey:@"key2"]);
2.5 allKeys 和 allValues 获得所有的key/value
NSDictionary * dic1 = @{@"key1":@"1",
@"key2":@"2"};
NSArray * keys = [dic1 allKeys];
NSArray * values = [dic1 allValues];
2.6 enumerateKeysAndObjectsUsingBlock 用Block的方式遍历
这里的stop决定了是否停止遍历。
NSDictionary * dic1 = @{@"key1":@"1",
@"key2":@"2"};
[dic1 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"%@=>%@",[key description],[obj description]);
keysSortedByValueUsingSelector:
keysSortedByValueUsingComparator :
keysSortedByValueWithOptions: usingComparator:
返回Keys的数组,顺序按照value排序顺序。
NSDictionary * numsDic = @{@(2):@"second",
@(1):@"first",
@(3):@"thrid"};
NSDictionary * strDic = @{@"id_1":@"first",
@"id_3":@"thrid",
@"id_2":@"second"};
NSArray * numsSortedKeys = [numsDic keysSortedByValueUsingSelector:@selector(compare:)];
NSArray * strSortedKyes = [strDic keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString * str1 = obj1;
NSString * str2 = obj2;
return [str1 compare:str2];
NSLog(@"%@",numsSortedKeys.description);
NSLog(@"%@",strSortedKyes.description);
22:04:12.070 DictonaryExample[] (
22:04:12.070 DictonaryExample[] (
keysOfEntriesPassingTest:
返回keys的集合,这些keys符合参数block的约束
NSDictionary * numsDic = @{@(2):@"second",
@(1):@"first",
@(3):@"thrid"};
NSSet * filteredKyes = [numsDic keysOfEntriesPassingTest:^BOOL(id key, id obj, BOOL *stop) {
BOOL result = NO;
NSNumber * numKey =
if (numKey.integerValue > 2) {
NSLog(@"%@",filteredKyes.description);
22:09:50.800 DictonaryExample[] {(
2.9写到文件
writeToFile:atomically
writeToURL:atomically
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] ;
NSString * fileName = @"file";
NSString * filePath = [path stringByAppendingPathComponent:fileName];
NSDictionary * dic = @{@"key":@"value"};
BOOL result = [dic writeToFile:filePath atomically:YES];
if (result) {
NSLog(@"Success");
2.10Description-常常用来debug输出dictionary的内容。
在之前已经举例好多了,这里不再赘述
三 NSMutableDictionary的额外方法
3.1 添加元素
- (void)setObject:(id)anObject
forKey:(id)aKey
- (void)setValue:(id)value
forKey:(NSString *)key
- (void)setDictionary:(NSDictionary *)otherDictionary
注意,使用KVC的时候,key一定要是NSString。第三个函数是删除之前的元素,然后把otherDictionary元素放到当前dic中。
NSMutableDictionary * dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"object",@"key", nil];
NSLog(@"%@",dic.description);
[dic setDictionary:@{@"otherKey":@"otherValue"}];
NSLog(@"%@",dic.description);
22:31:21.417 DictonaryExample[] {
22:31:21.418 DictonaryExample[] {
otherKey = otherV
3.2删除元素
- (void)removeObjectForKey:(id)aKey
- (void)removeAllObjects
- (void)removeObjectsForKeys:(NSArray *)keyArray
比较容易忽视的是第三个,删除一组key。
NSDictionary * dic = @{@(1):@"first",
@(2):@"second",
@(3):@"thrid"};
NSMutableDictionary * mutableDic = [[NSMutableDictionary alloc] initWithDictionary:dic];
[mutableDic removeObjectsForKeys:@[@(1),@(2)]];
NSLog(@"%@",mutableDic.description);
22:34:13.112 DictonaryExample[] {
BTY:年前计划在更新一篇KVC 和 KVO的详细讲解。然后继续更新GCD系列的第五篇。如果精力够用的话,更再更新一篇Swift相关的。
相关搜索:
相关阅读:
相关频道:
IOS教程最近更新下次自动登录
现在的位置:
& 综合 & 正文
NSDictionary读取boolean值数据
NSDictionary* json = [html JSONValue];
bool bol = [[json objectForKey:@"state"] boolValue];
switch (bol) {
case 0: //false
case 1: //true
NSString字符串相加 相连接
1. string = [NSString initWithFormat:@"%@,%@", string1, string2 ];
2. string = [string1 stringByAppendingString:string2];
3 . string = [string stringByAppendingFormat:@"%@,%@",string1, string2];
这三种方法都会将string1和string2 合并,但是第二个方法更有效率。
&&&&推荐文章:
【上篇】【下篇】OC字典NSDictionary - 简书
下载简书移动应用
写了61105字,被137人关注,获得了159个喜欢
OC字典NSDictionary
三种集合类
NSSArray 用于对象有序集合(NSObject对象)NSSet 用于对象的无序集合NSDictionary 永固键值映射以上三种集合类是不可变的(一旦初始化, 就不能改变)
以下是对应的三种可变集合类(者三种可变集合类是对应上面三种集合类的子类)NSMutableArrayNSMutableSetNSMutableDictionary
1. 字典的定义
字典用于保存具有映射关系(Key - value对)数据的集合,对于 "name : 张三" 来讲, key就是"name" , key对应的value值就是"张三", 一个key-value对认为是一个条(Entry), 字典是存储key-value对的容器!
字典的特点
与数组不同, 字典靠key存取元素;
key值不能重复; value必须是对象;
键值对在字典中是无序存储的
NSDictionary 不可变字典
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
@"male", @"sex",
@"20", @"age",
@"Tom", @"name",
@"run",@"hobby", nil ];`
获取所有的key值和所有的Value值[dic allKeys];[dic allValues];
根据key值获取Value值[dic valueForkey:@"name"];NSLog(@"%@", [dic valueForKey:@"name"])
遍历字典for-in 循环
快速遍历, 根据存在的对象类型输出
for (NSString *key in dic) {
NSLog(@"%@", key) // key为NSString可I型, 所以能遍历一边!
NSMutableDictionary
初始化字典NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"male", @"sex",
@"20", @"age",
@"tom", @"name",
@"run", @"hobby"];
向字典中添加字典: // NSDictionary *dict4=[NSDictionary
dictionaryWithObject:@"v6"forKey:@"k6"];
[mutableDic addEntriesFromDictionary:dict4];表示 把dict4字典中的对象和key值添加到了mutableDic中
向字典中添加value 和 key[mutableDic setValue:@"object" forKey:@"key"];
创建空的字典 , 并设置新的一个字典NSMutableDictionary *mutableDict2 = [NSMutableDictionary dictionary];`[mutableDict2 setDictionary:mutableDic];
删除指定key的value值[mutableDict2 removeObjectForKey:@"k4"];
删除移除key集合的value值NSArray *arrayKeys=[NSArray arrayWithObjects:@"k1",@"k2",@"k3",nil];
[mutableDict2 removeObjectsForKeys:arrayKeys];
删除字典中的所有value值[mutableDict2 removeAllObjects]; // 删除所有value值
不可变集合:Foundation框架中, 提供了NSSet类, 它是一组单值对象的集合, 且NSSet实例中元素是无序的, 用一个对象只能保存一个, 并且它也分为可变和不可变
创建集合NSSet *set1 = [[NSSet alloc] initWithObjects:@"one", @"two", nil];
通过数组构建集合NSArray *array = [NSArray aarayWithObjects:@"one", @"two", nil];NSSet *set = [NSSet setWithArray: array1];
通过已有集合构建集合NSSet *set3 = [NSSet setWithSet : set2];
集合对象数量NSInteger *count = [set3 count]; NSLog(@"%ld", count);
返回集合中任意元素NSString *str = [set3 anyObject];
// 注意返回的集合中的任意值, 随机出现
给集合中添加新元素NSSet *set1 = [NSSet setWithObject: @"one", nil];NSSet *appSet = [set1 setByAddingObject:@"two"]; NSLog(@"%@", appSet);
初始化NSMutableSet *mutableSet1 =[NSMutableSet Set];NSMutableSet *mutableSet2 = [NSMutableSet setWithObjects:@"1", @"2", nil];
从集合中去除相同的元素[mutableSet1 minusSet: mutableSet2];从mutableSet2去除mutableSet3中相同的!
得到两个集合的公共元素[mutableSet1 intersectSet:mutableSet2];mSet1 只留下与mSet2相同的元素
合并两个集合的元素[mutableSet unionSet: mutableSet3];
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
被以下专题收入,发现更多相似内容:
Object-C, 苹果编程之道!
· 174人关注
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
选择支付方式:

我要回帖

更多关于 css获取第一个元素 的文章

 

随机推荐