Obj-C: (loop) 优雅的for...in loop
来源:互联网 发布:vb按钮事件断点 编辑:程序博客网 时间:2024/06/06 09:13
Obj-C: (loop) 优雅的for...in loop
早期的Objective-C, 遍历一个容器类(NSArray, NSSet, NSDictionary)的方式是这样的:
12345678NSSet
*items = [
NSSet
setWithObjects:@
"foo"
,@
"bar"
,
nil
];
NSEnumerator
*enumerator = [items objectEnumerator];
NSString
*item =
nil
;
while
(item = [enumerator nextObject])
{
NSLog
(@
"%@"
, item);
}
如果是一个有序容器, 比如NSArray, 还可以使用C-style for loop:
1234567NSArray
*items =[
NSArray
arrayWithObjects:@
"foo"
,@
"bar"
,
nil
];
for
(
NSUInteger
i = 0; i < items.count; i++)
{
NSString
*item =[items objectAtIndex:i];
NSLog
(@
"%@"
, item);
}
可如果是NSSet这种无序容器, 就只能使用前一种了
------------------------------------------ 2.0时代的分割线 ------------------------------------------
Objective-C 2.0引入了for...in loop, 极大简化了遍历操作,
用for...in loop遍历一个NSArray:
123456NSSet
*items = [
NSSet
setWithObjects:@
"foo"
,@
"bar"
,
nil
];
for
(
NSString
*item in items)
{
NSLog
(@
"%@"
, item);
}
甚至可以简化成:
1234for
(
NSString
*item in [
NSSet
setWithObjects:@
"foo"
,@
"bar"
,
nil
])
{
NSLog
(@
"%@"
, item);
}
当用for...in loop来遍历NSDictionary时, 每次返回的是一个key:
123456789NSDictionary
*numbers =[
NSDictionary
dictionaryWithObjectsAndKeys:
@
"zero"
,@
"0"
,
@
"one"
,@
"1"
,
nil
];
for
(
NSString
*key in numbers)
{
NSString
*value =[numbers objectForKey:key];
NSLog
(@
"key:%@ value:%@"
, key, value);
}
在遍历一个容器类的同时, 是不能删除, 增加其中的元素的,
123456789NSMutableSet
*items =[
NSMutableSet
setWithObjects:@
""
,@
"a"
,@
"aa"
,@
"aaa"
,
nil
];
for
(
NSString
*item in items)
{
if
(item.length <2)
{
[items removeObject:item];
// WRONG!
}
}
for...in loop引入了 Mutation Guard的机制, 运行时抛出异常:
Collection was mutated while being enumerated
但如果我们真的需要遍历时删除怎么办? 解决方案之一是遍历一个容器的拷贝:
123456789NSMutableSet
*items =[
NSMutableSet
setWithObjects:@
""
,@
"a"
,@
"aa"
,@
"aaa"
,
nil
];
for
(
NSString
*item in [[items
copy
] autorelease])
{
if
(item.length <2)
{
[items removeObject:item];
// OKAY
}
}
Obj-C: (loop) 优雅的for...in loop
早期的Objective-C, 遍历一个容器类(NSArray, NSSet, NSDictionary)的方式是这样的:
1
2
3
4
5
6
7
8
NSSet
*items = [
NSSet
setWithObjects:@
"foo"
,@
"bar"
,
nil
];
NSEnumerator
*enumerator = [items objectEnumerator];
NSString
*item =
nil
;
while
(item = [enumerator nextObject])
{
NSLog
(@
"%@"
, item);
}
如果是一个有序容器, 比如NSArray, 还可以使用C-style for loop:
1
2
3
4
5
6
7
NSArray
*items =[
NSArray
arrayWithObjects:@
"foo"
,@
"bar"
,
nil
];
for
(
NSUInteger
i = 0; i < items.count; i++)
{
NSString
*item =[items objectAtIndex:i];
NSLog
(@
"%@"
, item);
}
可如果是NSSet这种无序容器, 就只能使用前一种了
------------------------------------------ 2.0时代的分割线 ------------------------------------------
Objective-C 2.0引入了for...in loop, 极大简化了遍历操作,
用for...in loop遍历一个NSArray:
1
2
3
4
5
6
NSSet
*items = [
NSSet
setWithObjects:@
"foo"
,@
"bar"
,
nil
];
for
(
NSString
*item in items)
{
NSLog
(@
"%@"
, item);
}
甚至可以简化成:
1
2
3
4
for
(
NSString
*item in [
NSSet
setWithObjects:@
"foo"
,@
"bar"
,
nil
])
{
NSLog
(@
"%@"
, item);
}
当用for...in loop来遍历NSDictionary时, 每次返回的是一个key:
1
2
3
4
5
6
7
8
9
NSDictionary
*numbers =[
NSDictionary
dictionaryWithObjectsAndKeys:
@
"zero"
,@
"0"
,
@
"one"
,@
"1"
,
nil
];
for
(
NSString
*key in numbers)
{
NSString
*value =[numbers objectForKey:key];
NSLog
(@
"key:%@ value:%@"
, key, value);
}
在遍历一个容器类的同时, 是不能删除, 增加其中的元素的,
1
2
3
4
5
6
7
8
9
NSMutableSet
*items =[
NSMutableSet
setWithObjects:@
""
,@
"a"
,@
"aa"
,@
"aaa"
,
nil
];
for
(
NSString
*item in items)
{
if
(item.length <2)
{
[items removeObject:item];
// WRONG!
}
}
for...in loop引入了 Mutation Guard的机制, 运行时抛出异常:
Collection was mutated while being enumerated
但如果我们真的需要遍历时删除怎么办? 解决方案之一是遍历一个容器的拷贝:
1
2
3
4
5
6
7
8
9
NSMutableSet
*items =[
NSMutableSet
setWithObjects:@
""
,@
"a"
,@
"aa"
,@
"aaa"
,
nil
];
for
(
NSString
*item in [[items
copy
] autorelease])
{
if
(item.length <2)
{
[items removeObject:item];
// OKAY
}
}
- Obj-C: (loop) 优雅的for...in loop
- oracle优雅for 循环插入 in loop insert
- For loop in MakeFile
- oracle for in loop
- using for loop in ssh
- for in loop 循环语句
- for loop in python demo
- Js for in vs for loop
- C loop
- hasOwnProperty,Object.keys(),propertyIsEnumerable,for-in(loop)
- oracle for in loop 两例
- C++ STL erase in for loop
- loop
- loop
- Loop
- loop
- VBA 中 do-loop,do-while-loop,do-until-loop,for-each-next 的小例子
- @autoreleasepool in Loop 和 Loop in @autoreleasepool
- jpa @Column 属性 解析
- js限制gridview中的textbox的输入为数字
- 谈谈Oracle中的锁
- ROC曲线及AUC评价指标
- 跟我一起写 Makefile(十)
- Obj-C: (loop) 优雅的for...in loop
- Spring MVC 进阶1
- 嵌入式设备的时区设置-busybox版本
- 跟我一起写 Makefile(十四)
- Linux添加/删除用户和用户组(linux中,添加cvs用户,实质就是添加linux用户。)
- 销售CRM就销售“买卖市场的管理思想”
- Team Foundation Server 2010 安装、部署与配置(一):安装计划
- 解决win7下安装Mysql卡在Start service的问题
- 使用ioctl添加网关产生Network is unreachable的原因