objective-c-blocks

iOS block with return value

≯℡__Kan透↙ 提交于 2019-12-20 23:27:00
问题 I am using a block from Facebook SDK. It returns a dictionary. I want that dictionary as a return value of a method. I am trying to wrap my head around the whole block concept but need a nudge in the right direction. The block: (the argument for the block is a string userFBid) -(NSDictionary*) getMutualFBFriendsWithFBid:(NSString*)fbID { [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"/%@/mutualfriends/%@", [[PFUser currentUser] objectForKey:kFbID],userFBid] parameters

ARC, self and blocks

旧巷老猫 提交于 2019-12-20 12:39:27
问题 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

Mulithreading: executing method calls only after finished executing other method

六眼飞鱼酱① 提交于 2019-12-20 10:56:06
问题 I am trying to process method asynchronously, as per requirements, once the first method has completed, only then the second method should start executing. The Problem is first method itself has code that runs on background thread. I tried dispatch_semaphore_wait, but that didnt work either. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, queue, ^{ [self firstMethod]; NSLog(@

iOS NSMethodSignature (or encoding) of NSBlock

两盒软妹~` 提交于 2019-12-20 10:54:14
问题 I need a way to check the number of arguments and argument types of a given block at runtime (I need this for some object mapping library I'm currently writing, I'm mapping String formatted values to selectors, want the same for blocks). I tried code from below examples, but for some reason it's not working for me and returnin nil for the string description. Do you know a way to evaluate block signatures at runtime(preferably afe for iPhone app store submissions)? This is the code I use:

Should my Block-based API have just completion or both success and failure handlers?

▼魔方 西西 提交于 2019-12-20 09:58:07
问题 When designing a block-based API in ObjC, which approach is better, one completion block or two, one each for success and failure? Let's say we have a method retrieving something asynchronously to a block, with one completion block it would be: - (void) retrieveSomethingCompletion:(void (^)(id retrievedObject, NSError *error))completionBlock; And with success/failure blocks (AFNetworking style): - (void) retrieveSomethingSuccess:(void(^)(id retrievedObject))successBlock failure:(void(^)

FIFO serial queue using GCD

岁酱吖の 提交于 2019-12-20 09:51:05
问题 I am trying to create a (network) synchronized array for the company I work for. While the networking part works fine, I have dwelled into an issue. My wish was to create a new queue using dispatch_create_queue , to which I would add two blocks that are not to run on the main thread, but in a serial manner, meaning that first the first block has to run, then the second, and never in parallel. I've read the apple documentation, but it is confusing to say the least. When I create my queue using

Correct management of addObserverForName:object:queue:usingBlock:

前提是你 提交于 2019-12-20 09:12:29
问题 I'm still new to blocks in objective-c and wondering if I have this psuedo code correct. I'm not sure if it's enough to just remove the observer or if i have to call removeObserver:name:object: -(void) scan { Scanner *scanner = [[Scanner alloc] init]; id scanComplete = [[NSNotificationCenter defaultCenter] addObserverForName:@"ScanComplete" object:scanner queue:nil usingBlock:^(NSNotification *notification){ /* do something */ [[NSNotificationCenter defaultCenter] removeObserver:scanComplete]

Xcode auto-complete for blocks-within-a-block (and the blocks they're in…)

这一生的挚爱 提交于 2019-12-20 07:17:27
问题 How do you set up a block property that takes as an argument another block property so that auto-complete supplies all of the required parameters for both blocks? To explain further, I'll demonstrate how auto-complete works with just one block property. In AppDelegate.h , create a convenient way to reference the AppDelegate class for all classes that require access to the block property: #define AppServices ((AppDelegate *)[[UIApplication sharedApplication] delegate]) Then, define the block:

Obj-C: __block variable not retaining data

时间秒杀一切 提交于 2019-12-20 05:17:13
问题 I think I might have an async problem going on here, which bites cause I thought I had solved it. Anyway, I am making a bunch of web service calls like so: //get the client data __block NSArray* arrClientPAs; [dataManager getJSONData:strWebService withBlock:^(id results, NSError* error) { if (error) { UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Getting Client Data Error!" message:error.description delegate:nil cancelButtonTitle:NSLocalizedString(@"Okay", nil) otherButtonTitles

What is (double (^)(int))foofoo

你说的曾经没有我的故事 提交于 2019-12-19 17:48:33
问题 There is an example on cdecl that goes (double (^)(int))foofoo means cast foofoo into block (int) returning double . What does it mean to cast foofoo into a "block" of int ? What does the symbol ^ exactly mean in this context. Usually it is bitwise XOR. 回答1: It's a GCC extension made by Apple, and implemented also in Clang. Blocks are small unnamed functions and that syntax is the type of a block. See Block Language Spec. 来源: https://stackoverflow.com/questions/3551999/what-is-double