objective-c-blocks

Explain __weak and __strong usage reasons in SDWebImage code

て烟熏妆下的殇ゞ 提交于 2019-11-30 13:29:19
问题 I think I understand strong and weak keywords well, but I don't understand how it's used in the code below. This code is from SDWebImage by Olivier Poitrey available on github. I understand strong and weak keywords as is described here: Explanation of strong and weak storage in iOS5 The code below uses __weak and __strong keywords in a way that is curious to me. It is not a child-parent relationship or delegate pattern as I am used to seeing weak used. However, I'm sure that this is a pattern

Is there a SELF pointer for blocks?

六眼飞鱼酱① 提交于 2019-11-30 11:59:16
问题 I'd like to recursively call a block from within itself. In an obj-c object, we get to use "self", is there something like this to refer to a block instance from inside itself? 回答1: Fun story! Blocks actually are Objective-C objects. That said, there is no exposed API to get the self pointer of blocks. However, if you declare blocks before using them, you can use them recursively. In a non-garbage-collected environment, you would do something like this: __weak __block int (^block_self)(int);

How to cast blocks to and from void *

ε祈祈猫儿з 提交于 2019-11-30 11:35:25
So, I'm trying to pass a block as an NSAlert contextInfo parameter. [myAlert beginSheetModalForWindow: theWindow modalDelegate: myAlert didEndSelector: @selector(alertDidEnd:returnCode:contextInfo:) contextInfo: (void *) aBlock]; and get it back on the other end: void (^responseBlock)() = (__bridge_transfer void (^)()) contextInfo; Which works, to an extent. Before my call to beginSheetModalForWindow:... aBlock is at 0x00007fff610e1ec0 , and in the response ( alertDidEnd:... ), contextInfo is at 0x00007fff610e1ec0 . However, when I try to call the block: responseBlock(); I get the following

Getting data out of the NSURLResponse completion block

醉酒当歌 提交于 2019-11-30 11:09:40
问题 It looks like I didn't get the concept of blocks completely yet... In my code I have to get out the JSON data from the asychronous block to be returned to from the ' outer ' method. I googled and found that if defining a variable with __block , the v̶i̶s̶i̶b̶i̶l̶i̶t̶y̶ _mutability_ of that variable is extended to the block . But for some reason returned json object is nil.I wonder why? - (NSMutableDictionary *)executeRequestUrlString:(NSString *)urlString { __block NSMutableDictionary *json =

Objective-C crash on __destroy_helper_block_

我怕爱的太早我们不能终老 提交于 2019-11-30 10:57:16
问题 I have an iOS application that's crashing on calls like __destroy_helper_block_253 and __destroy_helper_block_278 and I'm not really sure what either "destroy_helper_block" is referencing or what the number after it is supposed to point to. Does anyone have any pointers for how to track down where exactly these crashes might be occuring? Here's an example traceback (note that the lines with __destroy_helper_block only references the file it's contained in and nothing else, when normally the

Assignment to ivar in a Block via weak pointer

≯℡__Kan透↙ 提交于 2019-11-30 09:52:06
I have a read-only property isFinished in my interface file: typedef void (^MyFinishedBlock)(BOOL success, NSError *e); @interface TMSyncBase : NSObject { BOOL isFinished_; } @property (nonatomic, readonly) BOOL isFinished; and I want to set it to YES in a block at some point later, without creating a retain cycle to self : - (void)doSomethingWithFinishedBlock:(MyFinishedBlock)theFinishedBlock { __weak MyClass *weakSelf = self; MyFinishedBlock finishedBlockWrapper = ^(BOOL success, NSError *e) { [weakSelf willChangeValueForKey:@"isFinished"]; weakSelf -> isFinished_ = YES; [weakSelf

Objective-C Blocks and variable scope

这一生的挚爱 提交于 2019-11-30 09:26:23
问题 I would like to set the value of an NSData object within my block. Can someone let me know what I have done wrong here? // Data __block NSData *data = nil; [ZSURLConnection performRequestWithUrl:wsdlURL xmlString:xml completionHandler:^(NSData *response, NSError *error) { // Handle the error if (error) { NSLog(@"Error: %@", [error localizedDescription]); } else { data = response; }//end }];//end block if (data) { NSString *d = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding

Passing block parameter that doesn't match signature

浪子不回头ぞ 提交于 2019-11-30 09:20:10
I'm working with a block-based API and stumbled across a scenario where I was passing in a block parameter that had a signature that didn't match the typedef'd parameter the method was expecting. To my surprise, the compiler didn't seem to care about this, and the app didn't crash. Is this expected behavior? Example: typedef void(^MyBlock)(); typedef void(^MyBlockWithParam)(id param); - (void)doWork { MyBlockWithParam block1 = ^(id param) { NSLog(@"block1: %@", param); }; MyBlock block2 = ^{ NSLog(@"block2"); }; [self loadData:block1]; [self loadData:block2]; } - (void)loadData:

iOS 5 Blocks ARC bridged cast

我们两清 提交于 2019-11-30 09:12:06
This Question references this Question: How to simplify callback logic with a Block? My header has these typedefs typedef void (^StuffDoneBlock)(NSDictionary * parsedData); typedef void (^StuffFailedBlock)(NSError * error); And in init stuffDoneCallback = Block_copy(done); StuffFailedCallback = Block_copy(error); In this paper its says that Block_copy is unnecessary. But then it needs a bridged cast. The compiler message is at follows: error: cast of block pointer type 'StuffDoneBlock' (aka 'void (^)(NSDictionary *__strong)') to C pointer type 'const void *' requires a bridged cast [4]

Encoding an Objective-c Block?

会有一股神秘感。 提交于 2019-11-30 08:17:18
Is it possible to encode an Objective-C block with an NSKeyedArchiver ? I don't think a Block object is NSCoding -compliant, therefore [coder encodeObject:block forKey:@"block"] does not work? Any ideas? bbum No, it isn't possible for a variety of reasons. The data contained within a block isn't represented in any way similar to, say, instance variables. There is no inventory of state and, thus, no way to enumerate the state for archival purposes. Instead, I would suggest you create a simple class to hold your data, instances of which carry the state used by the blocks during processing and