objective-c-blocks

Under ARC, are Blocks automatically copied when assigned to an ivar via the property?

假如想象 提交于 2019-12-18 13:17:32
问题 Assume typedef void (^MyResponseHandler) (NSError *error); @property (strong, nonatomic) MyResponseHandler ivarResponseHandler; synthesize ivarResponseHandler = _ivarResponseHandler; - (void)myMethod:(MyResponseHandler)responseHandler { self.ivarResponseHandler = responseHandler; ... } Is the assignment to the ivar through the @property correct? I know that in manual memory management you would have needed self.ivarResponseHandler = [responseHandler copy]; to make sure the block was copied

EXC_BAD_ACCESS when using recursive block

Deadly 提交于 2019-12-18 12:34:14
问题 I'm trying to create recursion using blocks. It works for a while, but eventually it crashes and gives me a bad access exception. This is my code: BOOL (^Block)(Square *square, NSMutableArray *processedSquares) = ^(Square *square, NSMutableArray *processedSquares) { [processedSquares addObject:square]; if (square.nuked) { return YES; // Found a nuked square, immediately return } for (Square *adjacentSquare in square.adjacentSquares) { if ([processedSquares containsObject:adjacentSquare]) {

Is Block_copy recursive?

戏子无情 提交于 2019-12-18 12:24:20
问题 I have some code that essentially boils down to this: -(void)doSomethingWithBlock:(BlockTypedef)block { [Foo doSomethingElseWithBlock:^() { block(); }]; } Foo doSomethingElseWithBlock: calls Block_copy and Block_release on the block that it receives. Is this also necessary at the outer scope, or will the inner Block_copy handle this? 回答1: I quote the Blocks Programming Topics guide on Apple's developer documentation site: When you copy a block, any references to other blocks from within that

Passing blocks in Objective-C

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 10:55:14
问题 When writing a method that accepts a block as an argument, do I need to do anything special such as copying the block to the heap before executing it? For example, if I had the following method: - (void)testWithBlock:(void (^)(NSString *))block { NSString *testString = @"Test"; block(testString); } Should I do anything with block before calling it, or when entering the method? Or is the above the correct way of using the passed-in block? Also, is the following way of calling the method

How to write Objective-C Blocks inline?

倖福魔咒の 提交于 2019-12-18 10:46:46
问题 I am trying to implement a binary search using objective-c blocks. I am using the function indexOfObject:inSortedRange:options:usingComparator: . Here is an example. // A pile of data. NSUInteger amount = 900000; // A number to search for. NSNumber* number = [NSNumber numberWithInt:724242]; // Create some array. NSMutableArray* array = [NSMutableArray arrayWithCapacity:amount]; for (NSUInteger i = 0; i < amount; ++i) {; [array addObject:[NSNumber numberWithUnsignedInteger:i]]; }

How does a completion handler work on iOS?

僤鯓⒐⒋嵵緔 提交于 2019-12-18 10:41:35
问题 Im trying to understand completion handlers & blocks. I believe you can use blocks for many deep programming things without completion handlers, but I think i understand that completion handlers are based on blocks. (So basically completion handlers need blocks but not the other way around). So I saw this code on the internet about the old twitter framework: [twitterFeed performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { if (!error) { self

Objective-C pass block as parameter

↘锁芯ラ 提交于 2019-12-18 09:59:48
问题 How can I pass a Block to a Function / Method ? I tried - (void)someFunc:(__Block)someBlock with no avail. ie. What is the type for a Block ? 回答1: The type of a block varies depending on its arguments and its return type. In the general case, block types are declared the same way function pointer types are, but replacing the * with a ^ . One way to pass a block to a method is as follows: - (void)iterateWidgets:(void (^)(id, int))iteratorBlock; But as you can see, that's messy. You can instead

Swift closure in array becomes nil in Objective-c

北城以北 提交于 2019-12-18 09:04:37
问题 I created an objective-c method which will invoke a method via NSInvocation: typedef void (^ScriptingEmptyBlock)(); typedef void (^ScriptingErrorBlock)(NSError *error); - (void)scripting_execute:(NSString *)operation withParams:(nullable NSArray *)args { SEL selector = [self scripting_selectorForOperation:operation]; Class class = [self class]; NSMethodSignature *signature = [class instanceMethodSignatureForSelector:selector]; NSInvocation *invocation = [NSInvocation

Why do NSBlocks have to be copied for storage in containers?

◇◆丶佛笑我妖孽 提交于 2019-12-18 03:48:30
问题 - (void) addABlock { void (^aBlock)(void) = ^() { [someObject doSomething]; }; [self.myMutableArray addObject: aBlock]; // Oops.. [self.myMutableArray addObject: [aBlock copy]]; // works fine } In the above simplified example I am seeing undefined behavior if the block copy is not performed. This case is specifically listed in apple's ARC transition guide. The part that I do not understand is why I have to manually call copy. The block is created on the stack so a block_copy needs to be

How to get data from blocks using NSURLSession?

邮差的信 提交于 2019-12-18 01:03:38
问题 I have problem with this block. I trying to get the data inside the block of NSURLSession . here's my code -(NSDictionary *) RetrieveData{ NSURLSession * session = [NSURLSession sharedSession]; NSURL * url = [NSURL URLWithString: self.getURL]; dataList =[[NSDictionary alloc] init]; NSURLSessionDataTask * dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { self.json = [NSJSONSerialization JSONObjectWithData:data options:0 error