Tag: objective c blocks

对数组中的对象执行块操作,并在完成时完成

我有一个对象的数组,我想执行块操作。 我不确定最好的方法来做到这一点。 我正在做类似下面的代码,但我不认为这是最好的做法。 做这种手术的最好方法是什么? – (void)performBlockOnAllObjects:(NSArray*)objects completion:(void(^)(BOOL success))completionHandler { NSInteger counter = objects.count; for (MyObject *obj in objects) { [obj performTaskWithCompletion:^(NSError *error) { counter–; if (counter == 0) { completionHandler(YES); } }]; } }

目标C:class init和block?

是否有可能,例如,在View Controller的init方法中使用块作为完成处理程序,以便父视图控制器能够填充块中的细节,而无需创build自定义initWithNibName:andResourceBundle:andThis:andThat:为每个可能的属性? // … in the didSelectRowAtIndexPath method of the main view controller : SubViewController *subviewController = [[SubViewController alloc] initWithNibName:nil bundle:nil completionHandler:^(SubViewController * vc) { vc.property1 = NO; vc.property2 = [NSArray array]; vc.property3 = SomeEnumValue; vc.delegate = self; }]; [self.navigationController pushViewController:subviewController animated:YES]; [subviewController release]; 在SubViewController.m中: – (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil completionHandler:(void (^)(id newObj))block { self […]

在nsdictionary块?

所以我将阻止行为存储到nsmutabledictionary中,然后在WebSocket上回应响应时调用它们。 这将asynchronous请求转换为块语法。 这是精简的代码: – (void)sendMessage:(NSString*)message responseAction:(void (^)(id))responseAction { NSString *correlationID = (NSString*)[[message JSONValue] objectForKey:@"correlationId"]; [self.messageBlocks setObject:responseAction forKey:correlationID]; NSLog(@"Sending message: %@", correlationID); [webSocket send:message]; } – (void)webSocket:(SRWebSocket *)wsocket didReceiveMessage:(id)message; { NSString *correlationID = (NSString*)[[message JSONValue] objectForKey:@"correlationId"]; NSLog(@"Incoming message. CorrelationID: %@", correlationID); void (^action)(id) = nil; if (correlationID) { action = [messageBlocks objectForKey:correlationID]; if (action) action([message JSONValue]); […]

NSOperationQueue – 完成调用太早

我正在使用一个NSOperationQueue队列和调用一些地理编码位置查找。 当所有asynchronous运行的查找完成后,我想调用一个完成方法。 -(void)geocodeAllItems { NSOperationQueue *geoCodeQueue = [[NSOperationQueue alloc]init]; [geoCodeQueue setName:@"Geocode Queue"]; for (EventItem *item in [[EventItemStore sharedStore] allItems]) { if (item.eventLocationCLLocation){ NSLog(@"-Location Saved already. Skipping-"); continue; } [geoCodeQueue addOperationWithBlock:^{ NSLog(@"-Geocode Item-"); CLGeocoder* geocoder = [[CLGeocoder alloc] init]; [self geocodeItem:item withGeocoder:geocoder]; }]; } [geoCodeQueue addOperationWithBlock:^{ [[NSOperationQueue mainQueue]addOperationWithBlock:^{ NSLog(@"-End Of Queue Reached!-"); }]; }]; } – […]

在不使用ARC时替代__weak

我有这样的代码行: __weak NSBlockOperation *weakOperation = operation; 这是触发这个编译器错误: __weak attribute cannot be specified on automatic variable. 原因是我没有启用ARC(尚未准备好进行切换)。 所以从另一个StackOverFlow问题,我被推荐使用: __unsafe_unretained NSBlockOperation *weakOperation = operation; 这使得错误消失,但对于我使用它的上下文,它不工作(如果感兴趣,请参阅此问题: 如何取消NSOperationQueue )。 所以我的问题是,在这个例子中,我可以用__weak关键字替代__weak这个警告? 当我使用__weak ,所有的东西都能正确的工作,但是恐怕它不能支撑未来版本的iOS。

在采取行动之前如何知道几个街区是否已经完成执行?

我正在使用animateWithDuration:animations:completion:在removeFromSuperview:之前移动我的用户界面的几个元素(大约4个元素) removeFromSuperview:被调用。 我的问题是,在调用removeFromSuperview:之前,如何知道所有这些animation已经完成?

了解复杂的块语法

我是Objective C和iOS开发的初学者,但是已经有13年的.NET经验。 我很难精神地expression下面的声明,这个声明来自Objective C指南的编程 : void (^(^a)(void (^) (void))) (void) = … 它被用作为什么用typedef来定义块的例子,但是我想了解我在看什么,以便更好地理解块定义语法。 以下是我迄今为止的图表: 我遇到问题的地方在于,我是如何理解基本语法的: [return_val] (^[block_name]) ([block_args]) = … 如果是这样的话,那么我所拥有的是一个返回void且没有参数的块,但被命名为(^a) (void (^) void) 。 这意味着我的街区的名字,而不是一个直的string,本身就是一个街区。 很显然,我在这里错过了一些东西。 有人可以请说一说吗? 据网站介绍,这简化到: typedef void (^SimpleBlock) (void); SimpleBlock (^complexBlock) (SimpleBlock) = … 我只是想念如何。 编辑:第三个空白应该在括号中。 我解决了这个问题 在图像上是错误的,但我不想为此重做整个图像。 :)如果事实certificate是我的问题的来源,我会在这里修复它。

阻止recursion并打破保留周期

为了更好地说明这个问题,请考虑以下简化forms的块recursion: __block void (^next)(int) = ^(int index) { if (index == 3) { return; } int i = index; next(++i); }; next(0); XCode(支持ARC)警告说,“ 在这个块中强烈地捕获'下一个'可能会导致一个保留周期 。 同意。 问题1 :通过以下面的方式将块本身设置nil来保留周期是否成功: __block void (^next)(int) = ^(int index) { if (index == 3) { next = nil; // break the retain cycle return; } int i = index; next(++i); […]

iOS NSMethodSignature(或编码)的NSBlock

我需要一种方法来检查在运行时给定的块的参数和参数types的数量(我需要这个对于我正在写的一些对象映射库,我将string格式化的值映射到select器,要为块相同) 。 我尝试从下面的例子代码,但由于某种原因,它不适合我,并返回零string描述。 你知道在运行时评估块签名的方法吗(最好是iPhoneapp store的提交)? 这是我使用的代码: struct BlockDescriptor { unsigned long reserved; unsigned long size; void *rest[1]; }; struct Block { void *isa; int flags; int reserved; void (*invoke)(struct __block_literal_1 *); struct BlockDescriptor *descriptor; }; enum { BLOCK_HAS_COPY_DISPOSE = (1 << 25), BLOCK_HAS_CTOR = (1 << 26), // helpers have C++ code BLOCK_IS_GLOBAL = (1 […]

链UIViewanimation与时间间隔

我需要animation3 UIViews(淡入/淡出)。 1animation持续时间为0.6s(淡入/淡出周期为0.6 + 0.6s)。 但是我需要在0.2秒内启动animation。 第一个animation应该在0.0秒内启动。 第二个animation应该在0.2秒内启动。 应该在0.4秒内启动第三个animation。 所有这些都应该“无限期地”循环(直到某个触发器)。 我现在所拥有的: – (void)playAnimation { isAnimated = YES; [self animateView:firstView afterDelay:0.0]; [self animateView:secondView afterDelay:0.2]; [self animateView:thirdView afterDelay:0.4]; } – (void)stopAnimation { isAnimated = NO; } – (void)animateView:(UIView *)animatedView afterDelay:(float)delay { if(isAnimated) { [UIView animateWithDuration:0.6 delay:delay options:UIViewAnimationOptionTransitionNone animations:^ { animatedView.alpha = 1.0; } completion:^(BOOL finished) { [UIView […]