objective-c-blocks

Which libraries do you need to link against for a clang program using blocks

不想你离开。 提交于 2019-12-03 17:00:31
I've discovered (below) that I need to use -fblocks when compiling code which uses blocks. What library do I need to link against to let the linker resolve _NSConcreteStackBlock? (On Ubuntu 9.10 AMD64.) chris@chris-desktop:~$ clang ctest.c ctest.c:3:25: error: blocks support disabled - compile with -fblocks or pick a deployment target that supports them void call_a_block(void (^blockptr)(int)) { ^ ctest.c:11:19: error: blocks support disabled - compile with -fblocks or pick a deployment target that supports them call_a_block( ^(int y) { ^ 2 diagnostics generated. chris@chris-desktop:~$ clang

Error trying to assigning __block ALAsset from inside assetForURL:resultBlock:

放肆的年华 提交于 2019-12-03 16:56:28
I am trying to create a method that will return me a ALAsset for a given asset url. (I need upload the asset later and want to do it outside the result block with the result.) + (ALAsset*) assetForPhoto:(Photo*)photo { ALAssetsLibrary* library = [[[ALAssetsLibrary alloc] init] autorelease]; __block ALAsset* assetToReturn = nil; NSURL* url = [NSURL URLWithString:photo.assetUrl]; NSLog(@"assetForPhoto: %@[", url); [library assetForURL:url resultBlock:^(ALAsset *asset) { NSLog(@"asset: %@", asset); assetToReturn = asset; NSLog(@"asset: %@ %d", assetToReturn, [assetToReturn retainCount]); }

Can't make weak reference to closure in Swift

怎甘沉沦 提交于 2019-12-03 16:25:17
问题 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); _

substituting for __weak when not using ARC

淺唱寂寞╮ 提交于 2019-12-03 16:15:06
I have this line of code: __weak NSBlockOperation *weakOperation = operation; which is triggering this compiler error: __weak attribute cannot be specified on automatic variable. Reason for this is I don't have ARC enabled (not ready to make the switch just yet). So from another StackOverFlow question, I was recommended to use: __unsafe_unretained NSBlockOperation *weakOperation = operation; Which makes the error go away, but for the context I'm using it, it's not working (see this question if interested: How to cancel NSOperationQueue ). So my question is, what I can substitute the __weak

How to know if several blocks have completed execution before taking action?

∥☆過路亽.° 提交于 2019-12-03 15:47:38
I'm using animateWithDuration:animations:completion: to move several elements of my User Interface (about 4 elements) before removeFromSuperview: is called. My question is, how can I know that all those animations have completed before calling removeFromSuperview: ? Ok, to answer my own question. I ended up doing something like this: // Create dispatch queue & group dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_group_t group = dispatch_group_create(); // Two group enters dispatch_group_enter(group); dispatch_group_enter(group); // (notice the

Passing blocks to a AFNetworking method?

半腔热情 提交于 2019-12-03 15:34:19
-(void )getDataFromServer: (NSMutableDictionary *)dict { NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/doSomething",MainURL ]]; [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:nil parameters:dict]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { _myArray = JSON; [

Nested blocks and references to self

岁酱吖の 提交于 2019-12-03 14:25:28
I have a block wherein I use self so I declare a weak reference to self: __weak MyClass *weakSelf = self; Now my questions: I get an error where I define weakSelf and I don't understand what this should mean.: weak attribute can not be specified on an automatic variable Inside my block I pass weakSelf to another block and I am not sure if I now have to do the same thing again like so: __weak MyClass *weakWeakSelf = weakSelf; And then pass weakWeakSelf to that block? This is most likely occurring as you are targeting down to iOS 4. You should change it to be __unsafe_unretained MyClass

OCMock and block testing, executing

╄→尐↘猪︶ㄣ 提交于 2019-12-03 13:18:58
问题 Here's the method under test: - (void)loginWithUser:(NSString *)userName andPass:(NSString *)pass { NSDictionary *userPassD = @{@"user":userName, @"pass":pass}; [_loginCntrl loginWithUserPass:userPassD withSuccess:^(NSString *authToken){ // save authToken to credential store } failure:^(NSString *errorMessage) { // alert user pass was wrong }]; } what I want to test is that in that success block the other dependency/OCMockObject _credStore is called with the appropriate methods. So currently

blocks and ARC - copy or crash with release build (caused by optimization level)

巧了我就是萌 提交于 2019-12-03 12:41:31
I'm using Xcode 4.3.3 and developing for iOS 5.0+. In development of an ARC iOS application, I've started using blocks as a callback mechanism for asynchronous operations. The app works fine in the simulator and on the device. Then I ran it the profiler for the first time, and it started crashing on me nearly right away - in particular, an EXC_BAD_ACCESS when trying to invoke the first callback block. After a little investigation, it was clear the difference in behavior was because the profiler runs in "Release mode" by default - in particular, with Optimization Level set to "Fastest, Smallest

NSURLConnection with blocks

元气小坏坏 提交于 2019-12-03 12:37:13
I'm using [NSURLConnection connectionWithRequest:req delegate:self]; and then I use -(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace; -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge; -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error; -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; -(void