常用api:
dictionaryWithCapacity: 用来初始化
setObject: 设置
removeObjectForKey 删除
[count] 返回字典的数量
[allKeys]获取所有键的集合
[allValues】获取所有值得集合
sample:
NSMutableDictionary *testDic=[NSMutableDictionary dictionaryWithCapacity:12];
[testDic setObject:@"1" forKey:@"hel"];
[testDic setObject:@"2da" forKey:@"what"];
[testDic setObject:@"hello" forKey:@"hello"];
NSLog(@"the val is %@",[testDic objectForKey:@"what"]);
NSLog(@"the length is %ld",[testDic count]);
[testDic removeObjectForKey:@"hello"];
NSLog(@"the length is %lu",testDic.count);
NSArray *keyArray=[testDic allKeys];
NSArray *valArray=[testDic allValues];
NSLog(@"the all keys are %@",keyArray);
NSLog(@"the all values are %@",valArray);
}
</span>
返回结果是:
2013-10-09 17:20:19.533 element[401:303] the val is 2da
2013-10-09 17:20:19.535 element[401:303] the length is 3
2013-10-09 17:20:19.536 element[401:303] the length is 2
2013-10-09 17:20:19.537 element[401:303] the all keys are (
what,
hel
)
2013-10-09 17:20:19.537 element[401:303] the all values are (
2da,
1
)
不经常用的方法为
- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
- (void)removeAllObjects;
- (void)removeObjectsForKeys:(NSArray *)keyArray;
- (void)setDictionary:(NSDictionary *)otherDictionary;
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)keyNS_AVAILABLE(10_8,6_0);
sample如下:
NSMutableDictionary *emptyDic=[NSMutableDictionary dictionary];
[emptyDic setValue:@"2" forKey:@"2"];
NSLog(@"empty DIC is %@",emptyDic);
NSMutableDictionary *testDic=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"hello",@"1",@"andy",@"2",@"yang",@"3",nil];
NSDictionary *dic4=[NSDictionary dictionaryWithObject:@"yang" forKey:@"4"];
[testDic addEntriesFromDictionary:dic4];
NSLog(@"the mutalble dictionary is %@",testDic);
[emptyDic setDictionary:testDic];
NSLog(@"empty DIC is %@",emptyDic);
NSArray *deleteArr=[NSArray arrayWithObjects:@"1",@"2",@"yang",nil];
[testDic removeObjectsForKeys:deleteArr];
NSLog(@"the mutalble dictionary is %@",testDic);
[testDic removeAllObjects];
NSLog(@"the mutalble dictionary is %@",testDic);
结果:
2013-10-09 17:39:37.832 element[463:303] empty DIC is {
2 = 2;
}
2013-10-09 17:39:37.834 element[463:303] the mutalble dictionary is {
1 = hello;
2 = andy;
3 = yang;
4 = yang;
}
2013-10-09 17:39:37.834 element[463:303] empty DIC is {
1 = hello;
2 = andy;
3 = yang;
4 = yang;
}
2013-10-09 17:39:37.835 element[463:303] the mutalble dictionary is {
3 = yang;
4 = yang;
}
2013-10-09 17:39:37.835 element[463:303] the mutalble dictionary is {
}
遍历字典的方法:
for(id key in dic)
{
id obj=[dic objectForKey:key];
NSLog(@"%@",obj);
}
一般的枚举
NSMutableDictionary *testDic=[NSMutableDictionarydictionaryWithObjectsAndKeys:@"hello",@"1",@"andy",@"2",@"yang",@"3",nil];
for(int index=0;index<[testDiccount];index++)
{
NSString *obj=[testDic objectForKey:[[testDic allKeys] objectAtIndex:index]];
NSLog(@"the val is %@",obj);
}
- (void)deleteCharactersInRange:(NSRange)range; 删除字符串
NSMutableString *str1=[NSMutableString stringWithFormat:
@"hello what is your name?"];
NSRange range;
range=[str1 rangeOfString:@"what"];
NSLog(@"start is %lu,length is %lu",range.location,range.length);
[str1 deleteCharactersInRange:range];
NSLog(@"str is %@",str1);
4、数组转化为字符串
NSString *str=[firstArray componentsJoinedByString:@".."];
NSLog(@"the number is %@",str);
5、判断是否包含字符串
NSArray *firstArray=[[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil];
NSLog(@"has value %d",[firstArray containsObject:@"two"]);
NSLog(@"has value %ld",[firstArray indexOfObject:@"two"]);
NSLog(@"the last object is %@",[firstArray lastObject]);
8、遍历
NSMutableArray *mutableArr=[NSMutableArray arrayWithCapacity:4];
[mutableArr addObject:@"hello"];
[mutableArr addObject:@"hello"];
[mutableArr addObject:@"hello"];
for(int index=0;index<[mutableArr count];index++)
{
NSLog(@"the val is %@",[mutableArr objectAtIndex:index]);
}
for(NSString *str in mutableArr)
{
NSLog(@"%@",str);
}
for (id str in mutableArr) {
NSLog(@"%@",str);
}
第五、NSNumber的使用
基本数据类型无法用于字典、集合和数组,为此,我们需要将基本数据类型封装成数字对象,在OC中提供了NSNumber解决此问题
1、NSNumber封装的代码:
int age=12;
NSNumber *num1=[NSNumber numberWithInt:age];
NSNumber *num2=[NSNumber numberWithFloat:10.8];
NSLog(@"the val is %@",num1);
NSLog(@"the val is %@",num2);
NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:num1,@"age",num2,@"info", nil];
// insert code here...
NSLog(@"%@",dic);
NSLog(@"Hello, World!");
结果为:
2013-10-08 17:50:58.777 element[1731:303] the val is 12
2013-10-08 17:50:58.779 element[1731:303] the val is 10.8
2013-10-08 17:50:58.779 element[1731:303] {
age = 12;
info = "10.8";
}
2013-10-08 17:50:58.780 element[1731:303] Hello, World!
2、NSNumber解析的代码:
int age=12;
NSNumber *num1=[NSNumber numberWithInt:age];
NSNumber *num2=[NSNumber numberWithFloat:10.8];
NSLog(@"the num is %d",[num1 intValue]);
NSLog(@"the num is %d",[num2 intValue]);
}