objective-c-blocks

Can using __weak attribute to pass parameter to blocks lead to memory leaks?

杀马特。学长 韩版系。学妹 提交于 2019-12-03 06:25:31
问题 In my iOS ARC-enabled code, I need to pass "self" and other objects to a block. More specifically, I need to interact with self and an ASIHTTPRequest object inside the ASIHTTPRequest 's completionBlock . _operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(parseServerReply) object:nil]; _request = [ASIHTTPRequest requestWithURL:@"server.address"]; // ... [_request setCompletionBlock:^{ [self setResponseString:_request.responseString]; [[MyAppDelegate getQueue]

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

浪尽此生 提交于 2019-12-03 05:48:51
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 @pwc is correct in that you can't create a category for a class that you can't see. However... WHAT I AM ABOUT TO TELL YOU SHOULD BE USED STRICTLY AS AN

Can't make weak reference to closure in Swift

会有一股神秘感。 提交于 2019-12-03 05:44:32
Update: I tried writing it without making it weak, and there doesn't seem to be a leak. So maybe the question is no longer necessary. In Objective-C ARC, when you want to have a closure be able to use itself inside of the closure, the block cannot capture a strong reference to itself, or it will be a retain cycle, so instead you can make the closure capture a weak reference to itself, like so: // This is a simplified example, but there are real uses of recursive closures int (^fib)(int); __block __weak int (^weak_fib)(int); weak_fib = fib = ^(int n) { if (n < 2) return n; else return weak_fib

How does typedef-ing a block works

情到浓时终转凉″ 提交于 2019-12-03 04:04:37
问题 In C/Obj-C, we do a typedef like this typedef int MYINT; which is clear. Doing typedef for a block - typedef void (^MyBlock) (int a); Now, we can use MyBlock . Shouldn't it be like - typedef void (^MyBlock) (int a) MyBlock; similar to #define ? How the syntax works? 回答1: See Declaring a Block Reference in "Blocks Programming Topics": Block variables hold references to blocks. You declare them using syntax similar to that you use to declare a pointer to a function, except that you use ^

What will happen if I have nested dispatch_async calls?

一笑奈何 提交于 2019-12-03 03:47:10
问题 It may be a dumb question but I need to ask and clear this up for myself. To submit a block onto a queue for execution, use the functions dispatch_sync and dispatch_async . They both take a queue and a block as parameters. dispatch_async returns immediately, running the block asynchronously, while dispatch_sync blocks execution until the provided block returns. Here are some situations: Situation 1 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);

Executing Blocks From NSArray?

纵饮孤独 提交于 2019-12-03 03:31:47
问题 I was just thinking, as you can treat Blocks like objects if I create two of them and then add them to an NSArray is there a way to execute them from the array? int (^Block_001)(void) = ^{ return 101; }; int (^Block_002)(void) = ^{ return 202; }; NSArray *array = [NSArray arrayWithObjects:Block_001, Block_002, nil]; EDIT: Update for clarity Per @davedelong's excellent answer int (^Block_001)(void) = [^{ return 101; } copy]; int (^Block_002)(void) = [^{ return 202; } copy]; NSArray *array =

ARC, self and blocks

前提是你 提交于 2019-12-03 03:11:50
I thought I understood the usage of self in a block that is copied is a no no . But in an attempt to clean my code i enabled a bunch of warnings in Xcode, one called "Sending messages to weak pointers" so now in all my blocks, every time I use my created weakself reference __weak typeof(self) weakself = self; I get this warning: Weak receiver may be unpredictably set to nil a trivial example: __weak typeof(self) weakself = self; [aClass doSomethingInABlock:^{ [weakself doSomething]; //warning. }]; I have seen answers which define a version of self within the block like so: __weak typeof(self)

Meaning of symbol ^ in Objective C [duplicate]

末鹿安然 提交于 2019-12-03 02:56:18
Possible Duplicate: Caret in objective C What does this ^ syntax mean in Objective-C? I am tired by searching the meaning of symbol ^ in Objective C. I have seen it in lot of projects especially in back ground running tasks. I will put a link http://developer.apple.com/library/ios/#samplecode/StitchedStreamPlayer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010092 and in MyStreamingMovieViewController.m you can find the following inside - (IBAction)endScrubbing:(id)sender method . timeObserver = [[player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(tolerance, NSEC_PER_SEC)

ObjectiveC and JavaScriptCore: Will using this method of calling CallBacks cause memory issues?

微笑、不失礼 提交于 2019-12-03 00:33:15
DISCLAIMER: This is a long post, but could prove very valuable for those grappling with using the new ObjectiveC JavascriptCore framework and doing asynchronous coding between ObjC and JS. Hi there, I'm super new to Objective C and am integrating a javascript communication library into my iOS app. Anyway, I've been trying my hand at using the new ObjectiveC JavaScriptCore Framework introduced in iOS7. It's pretty awesome for the most part, though quite poorly documented so far. It's really strange mixing language conventions, but also kind of liberating in some ways. I should add that I am of

dispatch_sync vs. dispatch_async on main queue

前提是你 提交于 2019-12-03 00:29:29
问题 Bear with me, this is going to take some explaining. I have a function that looks like the one below. Context: "aProject" is a Core Data entity named LPProject with an array named 'memberFiles' that contains instances of another Core Data entity called LPFile. Each LPFile represents a file on disk and what we want to do is open each of those files and parse its text, looking for @import statements that point to OTHER files. If we find @import statements, we want to locate the file they point