objective-c-blocks

Chain UIView animations with time intervals

依然范特西╮ 提交于 2019-12-04 10:44:26
问题 I need to animate 3 UIViews (fade in/out). 1 animation duration is 0.6s (fade in/out cycle is 0.6+0.6s). But I need to launch animations in 0.2 seconds. 1st animation should be launched in 0.0 seconds. 2nd animation should be launched in 0.2 seconds. 3rd animation should be launched in 0.4 seconds. And all of them should be looped "indefinitely" (until some trigger). What I have at the moment: - (void)playAnimation { isAnimated = YES; [self animateView:firstView afterDelay:0.0]; [self

Is it possible to create a category of the “Block” object in Objective-C

你。 提交于 2019-12-04 10:43:07
问题 I would like to add functions by creating a category for Objective-C Blocks. __block int (^aBlock)(int) = ^int( int n ){ if( n <= 1 ) return n; return aBlock( n - 1 ) + aBlock( n - 2 ); }; Instead of just allowing the normal [aBlock copy] , [aBlock retain] , [aBlock release] , [aBlock autorelease] . I could do thing like: [aBlock mapTo:anArray]; Possible Category @interface UnknownBlockClass (map) - (NSArray *)mapTo:(NSArray *)array_; @end 回答1: @pwc is correct in that you can't create a

[myArray addObject:[[objcBlock copy] autorelease]] crashes on dealloc'ing the array

时光毁灭记忆、已成空白 提交于 2019-12-04 10:33:39
I wrote a class for declaratively describing a sequence of UIView animations. My method takes a vararg of animation blocks and puts them in an array. So in my loop I want to do this: [animations addObject:[[block copy] autorelease]]; I first copy the block so that it is moved to the heap, allowing it to be retain 'ed by the array. Then I autorelease it to relinquish ownership (because the array retains it). However this crashes when the animations array is dealloc'd. (My understanding is that the referenced blocks have already been dealloc'd.) Strange thing is, this works: [animations

crashing at performBatchUpdates of collection view [closed]

霸气de小男生 提交于 2019-12-04 10:18:25
i have a collection view which display two array of images with different section count's this will be toggled between two different view of one collectionview view - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { if (self.storyview) { return 4; } else { return 1; } } Where i perform Batch updates of collection view. it is getting crashed because i have two section count's [self.collectionView performBatchUpdates:^{ NSRange range = NSMakeRange(0, [self.collectionView numberOfSections]); NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];

Block automatic retaining, does it affect even to ivars in self?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 09:53:31
If I have class: @interface A : NSObject { BOOL b; id c; } @end and reference b and c in a block, is the block retain self automatically? Or just b and c ? About c , it may be retained itself, but how about b ? Bill's answer isn't quite correct: If you have an instance of A and create a block inside of that instance like so: ^{ b = YES; } Then self is retained (when the block is copied). b is not const -copied, because b is strongly referenced by self , and only self is const within the scope of the block. On the other hand, if you do: BOOL aBool = YES; ^{ aBool = NO; c = [[NSObject alloc]

Do I have to retain blocks in Objective-C for iOS?

可紊 提交于 2019-12-04 09:14:20
问题 I would like to make a method that takes in a block, saves it in a member, starts up an asynch task, and then calls the block when the asynchronous call makes its completion callback. Do I have to retain the block? Are blocks memory managed the same way as any other object? Can I synthesize a property to hold the block? 回答1: Blocks are similar to other objects for memory management, but not the same. When a block which accesses local variables is created, it is created on the stack. This

In Xcode how do I add a breakpoint inside a block?

守給你的承諾、 提交于 2019-12-04 08:48:32
I have a method which returns a block. I want to add a breakpoint inside the block. In Xcode adding a breakpoint on a line that's inside the block causes execution to pause when the method is returned and not when the block is executed. How do I add a breakpoint inside a block? I had the same difficulty, until I tried using Xcode 4's LLDB debugger (go to Product>Edit Scheme to turn it on). Perhaps you'll have better luck with it. From the documentation You can set breakpoints and single step into blocks. You can invoke a block from within a GDB session using invoke-block, as illustrated in

Best Technique for Replacing Delegate Methods with Blocks

为君一笑 提交于 2019-12-04 08:36:25
问题 I'm looking to create a category to replace delegate methods with callbacks blocks for a lot of the simple iOS APIs. Similar to the sendAsyc block on NSURLConnection. There are 2 techniques that are leak free and seem to work fine. What are the pros/cons about each? Is there a better way? Option 1. Use a category to implement the delegate's callback method on NSObject with the external callback block scoped. // Add category on NSObject to respond to the delegate @interface NSObject

Avoiding the “capturing self strongly in this block is likely to lead to a retain cycle” message

删除回忆录丶 提交于 2019-12-04 08:09:18
问题 every time I have to use a global var or property inside a block like this: self.save = ^(){ if (isItSaving == NO) { [self saveMyFile]; } }; I have to rewrite this like BOOL *iis = isItSaving; id myself = self; self.save = ^(){ if (iis == NO) { [myself saveMyFile]; } }; or Xcode will complain "capturing self strongly in this block is likely to lead to a retain cycle... It complains even about BOOL variables? Redeclaring everything before a block appears to be a lame solution. Is this the

How to make a function call in ios to wait, till the block inside that function is executed completely?

心不动则不痛 提交于 2019-12-04 07:56:00
Inside the following function, I've used a block. But when I call this function, it is returned even before the block is executed. I understood that Block inturn uses the threads and executes separately so that the function doesnt wait for it to return. But, Is there any other way I could make the function execution wait, or any other way to achieve the functionality of this block without using the block itself ? -(int)findCurrentZip { CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:[self findCurrentLatitude] longitude:[self findCurrentLongitude]]; int zipcode; self.myGeocoder