设为首页 加入收藏

TOP

Object-C-代码块Block回顾(二)
2015-02-02 13:17:12 来源: 作者: 【 】 浏览:163
Tags:Object-C- 代码 Block 回顾
emsProcessed"];
? ? ? ? NSLog(@"Number of items processed: %i", [theNum intValue]);
? ? }];
}
 枚举数组的时候的调用:
?
?
NSString *area = @"Europe";
NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];
NSMutableArray *areaArray = [NSMutableArray arrayWithCapacity:1];
NSIndexSet *areaIndexes = [timeZoneNames indexesOfObjectsWithOptions:NSEnumerationConcurrent
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? passingTest:^(id obj, NSUInteger idx, BOOL *stop) {
? ? NSString ?*tmpStr = (NSString *)obj;
? ? return [tmpStr hasPrefix:area];
}];
??
NSArray *tmpArray = [timeZoneNames objectsAtIndexes:areaIndexes];
[tmpArray enumerateObjectsWithOptions:NSEnumerationConcurrent|NSEnumerationReverse
? ? ? ? ? ? ? ? ? ? ? ? ? ?usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[areaArray addObject:[obj substringFromIndex:[area length]+1]];
}];
NSLog(@"Cities in %@ time zone:%@", area, areaArray);
 也可以写一个Block在截取字符串,做一些自己需要的事情:
?
?
NSString *musician = @"Beatles";
NSString *musicDates = [NSString stringWithContentsOfFile:
? ? @"/usr/share/calendar/calendar.music"
? ? encoding:NSASCIIStringEncoding error:NULL];
[musicDates enumerateSubstringsInRange:NSMakeRange(0, [musicDates length]-1)
? ? options:NSStringEnumerationByLines
? ? usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
? ? ? ? ? ?NSRange found = [substring rangeOfString:musician];
? ? ? ? ? ?if (found.location != NSNotFound) {
? ? ? ? ? ? ? ? NSLog(@"%@", substring);
? ? ? ? ? ?}
? ? ? }];
  视图动画和转换中的实践:
?
?
[UIView animateWithDuration:0.2 animations:^{
? ? ? ? view.alpha = 0.0;
? ? } completion:^(BOOL finished){
? ? ? ? [view removeFromSuperview];
? ? }];
  两个视图之间的动画:
?
?
[UIView transitionWithView:containerView duration:0.2
? ? ? ? ? ? ? ? ? ?options:UIViewAnimationOptionTransitionFlipFromLeft
? ? ? ? ? ? ? ? animations:^{
? ? ? ? ? ? ? ? ? ? [fromView removeFromSuperview];
? ? ? ? ? ? ? ? ? ? [containerView addSubview:toView]
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? completion:NULL];
 数组排序的实践:
?
?
NSArray *stringsArray = [NSArray arrayWithObjects:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?@"string 1",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?@"String 21",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?@"string 12",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?@"String 11",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?@"String 02", nil];
static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch |
? ? ? ? NSWidthInsensitiveSearch | NSForcedOrderingSearch;
NSLocale *currentLocale = [NSLocale currentLocale];
NSComparator finderSort = ^(id string1, id string2) {
? ? NSRange string1Range = NSMakeRange(0, [string1 length]);
? ? return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale];
};
NSLog(@"finderSort: %@", [stringsArray sortedArrayUsingComparator:finderSort]);
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言学习之Linux下TCP服务器与客.. 下一篇C中setjmp和longjmp用法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: