objective-c-blocks

How to call an objective-c function which accepts Dictionary of blocks as argument from Swift?

烈酒焚心 提交于 2019-11-30 19:00:19
问题 I have a function in my objective c file (lets say class MyBlockExecutor): + (void) runBlockFromDictionary: (NSDictionary*) blocksDict andKey: (NSString*) key { if ( [blocksDict objectForKey: key] != nil ) { ((MyBlock)[blocksDict objectForKey: key])(); } } Now, I want to call this function from Swift. Here is my swift call: MyBlockExecutor.runBlock(from: [ "key1":{ ()->Void in print("block for key1 called") } ], andKey: "key1") This crashes my app. I am getting EXC_BAD_ACCESS error on this

NSTimer Category + Blocks implementation to replace selector

泄露秘密 提交于 2019-11-30 18:07:46
问题 I am quite new to blocks and objective-c, and i am trying to write my first category using both. My idea is to create a category on NSTimer that will receive a block as a parameter and this block will be used in the selector call. Right now I have this. // NSTimer+Additions.h #import <Foundation/Foundation.h> typedef void (^VoidBlock)(); @interface NSTimer (NSTimer_Additions) + (NSTimer *)scheduleTimerWithTimeInterval:(NSTimeInterval)theSeconds repeats:(BOOL)repeats actions:(VoidBlock)actions

Keep object alive until a background task finishes

跟風遠走 提交于 2019-11-30 18:01:09
问题 I am trying to implement a method that executes a task in the background and then calls a block on the main thread: + (void)migrateStoreWithCompletionHandler:(MigrationControllerCompletion)completion { MigrationController *controller = [[MigrationController alloc] initWithCompletionBlock:completion]; [controller migrateStore]; } This is the -initWithCompletionBlock: method: - (id)initWithCompletionBlock:(MigrationControllerCompletion)completion { self = [super init]; if (self) { _completion =

How to remove elements in NSMutableArray or NSMutableDictionary during enumeration?

筅森魡賤 提交于 2019-11-30 17:50:12
I am using block based enumeration similar to the following code: [[[rows objectForKey:self.company.coaTypeCode] objectForKey:statementType] enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id coaItem, NSUInteger idx, BOOL *stop) { // block code here }] I would like to remove some of the objects during the enumeration process depending on the their object values. How could I do this? I know that manipulating an mutable array or dictionary (NSMutableArray or NSMutableDictionary) during enumeration is usually not possible. What would be the best way to implement this? Thank you!

Reference to self inside block

我怕爱的太早我们不能终老 提交于 2019-11-30 15:51:40
Right now I have a portion of code like this: __strong MyRequest *this = self; MyHTTPRequestOperation *operation = [[MyHTTPRequestOperation alloc]initWithRequest:urlRequest]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *request, id responseObject) { [this requestFinished:request]; } failure:^(AFHTTPRequestOperation *request, NSError *error) { [this requestFailed:request withError:error]; }]; I am mainly doing this because some other classes are inheriting from the class where this code is located and implementing their own requestFinished and requestFailed. If I change

Invoke block iOS

血红的双手。 提交于 2019-11-30 15:49:02
问题 I try to invoke some block, but I run into a EXC_BAD_ACCESS. -(void) methodA { self.block = ^ { [self methodB]; }; } -(void) webViewDidFinishLoad:(UIWebView *)webView { [block invoke]; // error here (block is not valid id type). } -(void)methodB { //do something } Any thoughts on why this is happening? 回答1: You should use copy attribute when you are declaring block property. Like: @property (nonatomic, copy) id block; 回答2: if you want to invoke the block you can simply do this block();

Understand one edge case of block memory management in objc

孤街醉人 提交于 2019-11-30 15:48:35
the code below will crash because of EXC_BAD_ACCESS typedef void(^myBlock)(void); - (void)viewDidLoad { [super viewDidLoad]; NSArray *tmp = [self getBlockArray]; myBlock block = tmp[0]; block(); } - (id)getBlockArray { int val = 10; //crash version return [[NSArray alloc] initWithObjects: ^{NSLog(@"blk0:%d", val);}, ^{NSLog(@"blk1:%d", val);}, nil]; //won't crash version // return @[^{NSLog(@"block0: %d", val);}, ^{NSLog(@"block1: %d", val);}]; } the code runs in iOS 9 with ARC enabled. And I was trying to figure out the reason that lead to crash. by po tmp in lldb I found (lldb) po tmp <_

Is this a sane Objective-C Block Implementation?

女生的网名这么多〃 提交于 2019-11-30 15:30:40
I wanted a variation of NSRegularExpression's – stringByReplacingMatchesInString:options:range:withTemplate: method that takes a block instead of a template. The return value of the block would be used as the replacement value. This is more flexible than a template, as you can imagine. Sort of like using the /e modifier in Perl regular expressions. So I wrote a category to add the method. This is what I came up with: @implementation NSRegularExpression (Block) - (NSString *)stringByReplacingMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range usingBlock:

Use of Blocks crashes app in iPhone Simulator 4.3/XCode 4.2 and 4.0.2

本秂侑毒 提交于 2019-11-30 14:01:47
Anybody else having trouble with the 4.3 iPhone Simulator in XCode 4.2(lion) or 4.0.2? I have code that has long been working, tested, and in production that uses blocks to specify completion actions. For example, I use UIView animate to fade out some text on top of the label as follows: [UIView animateWithDuration: 0.0 delay: 0.0 options: (UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionNone) animations: ^{ videoTextLabel1.alpha = 0.0; videoTextLabel2.alpha = 0.0; videoTextLabel3.alpha = 0.0; } completion: ^(BOOL completed) { [self fadeInNextMeditationLine: 0]; }]; I

Check for availability of blocks at runtime on iOS

岁酱吖の 提交于 2019-11-30 13:43:47
I need to test for the availability of blocks at runtime, so I can handle backwards compatibility with iOS 3. Any tips? edit: So far I'm doing if (!NSClassFromString(@"NSBlockOperation")) {...} Seems to be working... martineno You will also need to make sure to weak link the libSystem.B.dylib , set your base SDK to 4.0 and deployment target to 3.1.3, as described here . A good overview on how to deal with iOS versioning issues can also be found in this this Cocoa with Love article: Tips & Tricks for conditional iOS3, iOS3.2 and iOS4 code 来源: https://stackoverflow.com/questions/4231965/check