NSDictionary
NSDictionary *dictionary1 = [[
NSDictionary alloc] initWithObjectsAndKeys:@
"one",@
"1",@
"two",@
"2",
nil];
NSLog(@
"%@",dictionary1);
NSDictionary *dictionary3 = @{@
"1":@
"one",@
"2":@
"two"};
NSLog(@
"%@",dictionary3);
NSLog(@
"%ld", dictionary1
.count);
NSLog(@
"obj = %@",[dictionary1 objectForKey:@
"2"]);
NSLog(@
"obj1 = %@",dictionary1[@
"2"]);
NSArray *keys = dictionary1
.allKeys;
NSArray *values = dictionary1
.allValues;
NSLog(@
"%@",values);
字典的遍历
/
/因为字典是以key-
value形式存在,所以不可以直接遍历出一对,只能keys 或
value
for (
int i =
0; i <dictionary1.count ; i++) {
id key = keys[i];
NSLog(
@"%@",dictionary1[key]);
}
for (id
object in dictionary1) {
NSLog(
@"%@",dictionary1[
object]);
}
for (id
object in keys) {
NSLog(
@"%@",dictionary1[
object]);
}
NSMutableDictionary
NSMutableDictionary *mutableDic = [
NSMutableDictionary dictionaryWithObjectsAndKeys:@
"one",@
"1",@
"two",@
"2",
nil];
[mutableDic setObject:@
"three" forKey:@
"3"];
[mutableDic removeObjectForKey:@
"2"];
[mutableDic objectForKey:@
"3"];
NSArray *dataArr = @[@
"a",@[@
"ada",@
"anie"],
@{@
"book":@[@
"math",@
"english",@
"chinese"]
}
];
NSLog(@
"chinese:%@",[[[dataArr objectAtIndex:
2] objectForKey:@
"book"] objectAtIndex:
2]);
NSLog(@
"%@",dataArr[
1][
1]);
转载请注明原文地址: https://ju.6miu.com/read-1311444.html