objective-c-blocks

How to test async method in block using OCMock

人盡茶涼 提交于 2019-12-11 01:45:26
问题 I can't seem to figure out how to test this method: - (void)writer:(id)writer didFailWithError:(NSError *)error; { [self.alertView dismissWithClickedButtonIndex:0 animated:YES]; void (^alertViewBlock)(int) = ^(int buttonIndex) { if (buttonIndex == 1) { [self performSelectorOnMainThread:@selector(savePostponeReasonsAsynchronously) withObject:nil waitUntilDone:NO]; } else { NSLog(@"dismissed"); self.savePostponeReasonsQueue = nil; } }; [self showPostponeReasonFailedAlert:alertViewBlock]; }

passing a block in @selector()

a 夏天 提交于 2019-12-11 01:42:36
问题 How do I pass a block, and what would it look like, in the method incrementCount:completion to get the property self.count returned after its increment in the CounterClass? I'm not sure if the way I defined the block parameter (void(^)(void))callback; in the method is correct i.e. should it also have a return value? ViewController [NSTimer scheduledTimerWithTimeInterval:3.0 target:self.counterClass selector:@selector(incrementCount:completion:) userInfo:nil repeats:YES]; CounterClass -(void

How do display a UIAlertView from a block on iOS?

梦想的初衷 提交于 2019-12-11 01:29:05
问题 What is the best way of displaying a UIAlertView from a block? I have the following action in my code : - (IBAction)connectWithTwitterClicked:(id)sender { ACAccountStore * account = [[ACAccountStore alloc]init]; ACAccountType * accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; [account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) { if (granted == YES){ NSLog(@"Twitter account has been granted");

Static variable inside block

江枫思渺然 提交于 2019-12-11 00:53:48
问题 What happens when I declare a static variable inside a block like this? dispatch_async(dispatch_get_main_queue(), ^{ static NSInteger myNumber; // do stuff with myNumber }); What happens the second time this block is triggered? How can myNumber be still there if the block will deallocate after running? Is doing this OK? I mean will this practice cause any problem, like the block leaking because it is unable to be released? 回答1: The block specification does not explicitly mention how static

Is there any way to serialize / unserialize an Objective-C block?

妖精的绣舞 提交于 2019-12-10 22:18:38
问题 I'm writing an app where support for "promotions" is required, and these promotions could be arbitrarily complex and many different pieces of data might be relevant in their calculation. Therefore, whilst in the early stages of development, I don't want to invent a whole specification schema for these things, I'd rather just write each one in Objective-C and then somehow serialize the compiled code into the (CoreData) database for later recall and execution. Is this in any way possible? I was

Strange “zombie” in forwardInvocation: + getArgument:atIndex methods

自闭症网瘾萝莉.ら 提交于 2019-12-10 22:09:59
问题 Here is part from my code: - (void)viewDidLoad { [super viewDidLoad]; CGRect frame = [[UIScreen mainScreen] bounds]; _webView = [[UIWebView alloc] initWithFrame:frame]; [_webView setHidden:NO]; [self.view addSubview:_webView]; _vk = [[DPVkontakteCommunicator alloc] initWithWebView:_webView]; DPVkontakteUserAccount *user; NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"accessToken"]; NSInteger userId = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"

Why should we copy blocks rather than retain?

别来无恙 提交于 2019-12-10 17:36:07
问题 I recently met a problem using blocks with Facebook's app switching. I needed to call a block after the Facebook login. First my block was destroyed when the app switched back ('cause it was on the stack), so I decided to retain it. But that didn't work, and I messed with that problem :/. I found a solution on that blog and also here. My question is simply : why copy works and retain does not ? 回答1: Because when you create a block there is nothing to retain, since it doesn't exist in the heap

Why does UIAlertController create a retain cycle with self?

耗尽温柔 提交于 2019-12-10 15:36:57
问题 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"alert" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"action" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self doSomething]; }]; [alert addAction:action]; [self presentViewController:alert animated:YES completion:nil]; I understand the cycle . self retains the UIAlertController , UIAlertController retains the

Return a dispatch_async fetched variable [duplicate]

孤人 提交于 2019-12-10 13:45:59
问题 This question already has answers here : Return value for function inside a block (3 answers) Closed 6 years ago . Basically: a method needs to return a NSDictionary that is fetched within a dispatch_async. This is what I have tried: - (NSDictionary *)fetchNSDictionary { dispatch_queue_t Queue = dispatch_queue_create("Dictionary fetcher", NULL); dispatch_async(Queue, ^{ NSDictionary *dict = ... dispatch_async(dispatch_get_main_queue(),^{ return dict; )}; )}; } Result: Incompatible block

Objective-C Callbacks/Block Pattern

走远了吗. 提交于 2019-12-10 11:38:06
问题 What I am trying to do is load a list of people (JSON format) from a remote server, save the file onto disk, and then parse the result and return an NSArray * back to the caller. I have created a EmployeeDirectoryManager that has the following: - (NSArray *)loadDirectory:(BOOL)refreshFromServer; - (void)loadDirectoryFromFile; - (void)loadDirectoryFromServer; I would like to use a block on the loadDirectory method so the caller can be informed when the loadDirectoryFromServer , which is using