Tag: objective c blocks

闪烁隐藏并使用块

我有方法: – (void)blinkView:(UIView *)view { view.layer.opacity = 0.0f; view.hidden = NO; [UIView beginAnimations:@"Blinking" context:nil]; [UIView setAnimationRepeatCount:1.0]; [UIView setAnimationDuration:0.6f]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; view.layer.opacity = 1.0f; [UIView commitAnimations]; } 我怎样才能写这个代码块,以及如何实现具有相反效果的方法(隐藏与闪烁uiview)?

使用块加载表中的图像 – dispatch_async返回值

我已经环顾了这个网页,并没有find具体的答案。 很简单,我有一个表格从Flickr加载图像信息。 我想在每个单元格的左侧显示图像的缩略图。 为了做到这一点而不阻塞主(UI)线程,我正在使用块: – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"top50places"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //getting the selected row image NSDictionary* currentImageDictionary=[self.topfifty objectAtIndex:indexPath.row];//topFifty is an array of image dictionaries //creating the download queue dispatch_queue_t downloadQueue=dispatch_queue_create("thumbnailImage", NULL); dispatch_async(downloadQueue, ^{ UIImage *downloadedThumbImage=[self getImage:currentImageDictionary] ; //Need to go back to the main […]

如果一个强大的财产有一个指向自我的块,那么这是一个保留周期吗?

例如: [self.contentWrapperView addGestureRecognizer: [UITapGestureRecognizer recognizerWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) { if (self.customEditing) { [self setEditingMode:NO Animated:YES]; } }]]; 其中contentWrapperView是一个强大的self属性,并假设contentWrapperView具有强大的识别器块引用。 在块中使用self将导致保留周期? 这是我不明白的唯一部分。

对多个位置进行地理编码 – 了解何时调用“所有”完成块

我正在使用CoreLocation的地理编码器来获取多个地图项的CLLocation坐标。 地理编码器在完成时为每个项目调用完成块。 当所有包含asynchronous地理编码调用的调用完成后,如何创build一个类似的块function? (我可以使用手动计数器,但必须有一个更优雅的解决scheme) 这是我迄今为止的地理编码function。 它通过一系列位置项目进行循环,并为每个项目启动一个新的地理编码过程。 -(void)geoCodeAllItems { for (EventItem* thisEvent in [[EventItemStore sharedStore] allItems]) { if (![thisEvent eventLocationCLLocation]){ //Only geocode if the item has no location data yet CLGeocoder *geocoder = [[CLGeocoder alloc]init]; [geocoder geocodeAddressString:[thisEvent eventLocationGeoQuery] completionHandler:^(NSArray *placemarks, NSError *error) { if (error){ NSLog(@"\t Geo Code – Error – Failed to geocode"; return; } […]

recursion块太早解除分配

我已经写了一个recursion块遵循这些准则: NSMutableArray *groups = [NSMutableArray arrayWithArray:@[@"group1", @"group2", @"group3", @"group4"]; __block CommunicationCompletionHandler completion = [^{ [groups removeObjectAtIndex:0]; if ([groups count] > 0) { // This will send some information to the network, and calls the completion handler when it receives a response [mySocket saveGroup:groups[0] completion:completion]; } } copy]; // Removing copy here doesn't work either [mySocket […]

在objective-c中使用块进行recursion

在执行涉及objective-c块的recursion时,我正在接收iOS应用程序中的EXC_BAD_ACCESS信号。 这是简化的代码: – (void)problematicMethod:(FriendInfo*)friendInfo onComplete:(void(^)(NSString*))onComplete1 { [self doSomethingWithFriend:friendInfo onComplete:^(Response* response) { switch (response.status) { case IS_OK: onComplete1(message); break; case ISNT_OK: // Recursively calls the method until a different response is received [self problematicMethod:friendInfo onComplete:onComplete1]; break; default: break; } }]; } 所以基本上, 问题的方法 ,在这个简化版本中,调用doSomethingWithFriend:onComplete:。 当该方法完成( onComplete ),如果一切正常,原来的onComplete1块被调用,这工作正常。 但是如果出现了问题, 有问题的方法需要再次调用(recursion部分),当第一次发生这种情况时,我立即得到EXC_BAD_ACCESS信号。 任何forms的帮助将不胜感激。

NSOperationQueue:麻烦理解命令

我无法理解NSOperationQueue工作方式。 说我有: NSOperationQueue *queue = [[NSOperationQueue alloc] init]; queue.maxConcurrentOperationCount=1; [queue addOperationWithBlock:^{ [someObject someSelector]; }]; [queue addOperationWithBlock:^{ [someObject anotherSelector]; }]; 第二个区块甚至在第一个区块完成之前被调用 – 与我想要的相反。 我尝试使用 – performSelectorOnMainThread:withObject:waitUntilDone:相反,但第二个块仍然是首先执行 – 大概是因为块线程没有在主线程上完成,所以它不会被waitUntilDone封锁。 我在我的someSelector块中添加了一个断点,并在第二个块内的断点之后到达。 我不太明白。 帮我!!

在objective-c中使用asynchronous代码避免嵌套块

我需要在我的Objective-C代码中发生很长的一系列事件。 假设我有6件东西 – thingA,thingB,thingC,thingD,thingE和thingF。 thingB和thingD返回一个BOOL。 如果thingB是NO,那么thingC不需要被调用。 如果thingD是NO,那么thingE不需要被调用。 – (void)doThings:(void(^)())completion { [self thingA: ^{ [self thingB: ^(BOOL success) { if (success) { [self thingC: ^{ [self thingD: ^(BOOL success) { if (thingD) { [self thingE: ^{ [self thingF]; completion(); }]; return; } [self thingF]; completion(); }]; }]; return; } [self thingD: ^(BOOL success) { if (success) […]

当被称为Objective-C块时,Swift结束崩溃

在我的项目中,我有Objective-C和Swift代码。 我有一些对象具有包含块来清除一些UITableViewconfiguration的属性。 使用它在Objective-C中工作,但使用Swift时会崩溃。 我已经将问题减less到尽可能最小,同时仍然是可重复的。 // in Objective-C @interface MyClass : NSObject @property (copy, nonatomic) NSString* (^block)(); – (NSString *)callTheBlock; @end @implementation MyClass – (NSString *)callTheBlock { if (self.block) { return self.block(); } else { return @"There is no spoon"; } } @end // In Swift // I actually have this in my AppDelegate to run […]

参考计数块中的自我

我试图让我的脑海如何对象生命周期和引用计数与代码块交互。 在下面的代码中,我只是做一个简单的animation,当UINavigationController的堆栈上的顶视图交换时闪烁。 棘手的部分是popup的视图控制器是定义这个代码的地方 。 [UIView animateWithDuration:0.2 animations:^{self.navigationController.view.alpha = 0.0;} completion:^(BOOL finished){ UINavigationController *navController = self.navigationController; [self.navigationController popViewControllerAnimated:NO]; [navController pushViewController:nextView animated:NO]; [nextView release]; [UIView animateWithDuration:0.2 animations:^{navController.view.alpha = 1.0;}]; }]; 我的问题是(忽略什么animation看起来像),这是从内存pipe理的angular度来做这件事的正确方法。 尤其是: (1)在pop + push循环中使用这种方法时,正确的是不再需要保留自己,就像其他类似的不使用块的例子一样? (2)是否调用animateWithDuration:…这些块保留定义视图控制器(自我),直到块执行?