objective-c-blocks

Objective-C blocks and variables

柔情痞子 提交于 2019-12-05 04:24:01
I've started using Objective-C blocks today. I wrote the following code: NSArray *array = @[@25, @"abc", @7.2]; void (^print)(NSUInteger index) = ^(NSUInteger index) { NSLog(@"%@", array[index]); }; for (int n = 0; n < 3; n++) print(n); Which works properly. I needed to change the array variable after its declaration, though, so I tried using the following code: NSArray *array; void (^print)(NSUInteger index) = ^(NSUInteger index) { NSLog(@"%@", array[index]); }; array = @[@25, @"abc", @7.2]; for (int n = 0; n < 3; n++) print(n); However, that doesn't work. The console just prints (null) three

Strong reference in the block, it will be retained?

纵饮孤独 提交于 2019-12-05 02:12:57
问题 I discover this snippet code from company's document: __weak __typeof(self)weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ __strong __typeof(weakSelf)strongSelf = weakSelf; // Do stuff }); It's will be retained ? 回答1: There are two reasons to capture weak references within a block. avoid retain cycles create no-op situations. The former has been discussed ad-nauseum. The second is more interesting. Example The block in question is a completion handler for an image download. When

Passing blocks to a AFNetworking method?

主宰稳场 提交于 2019-12-05 01:24:36
问题 -(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

Multithreading and Grand Central Dispatch on iOS [closed]

孤者浪人 提交于 2019-12-05 00:31:57
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . now i am trying to understand the concept of gcd. using grand central dispatch how to implement multithreading in my application.i have the idea about the gcd concept but i cant implement the concept to my

Swift closure crashes when called as Objective-C block

大憨熊 提交于 2019-12-05 00:26:43
In my project, I have both Objective-C and Swift code. I have some objects that have properties containing blocks to clean up some UITableView configuration. Using it works in Objective-C, but crashes when using Swift. I have reduced the problem to be as minimal as possible, while still being reproducible. // in Objective-C @interface MyClass : NSObject @property (copy, nonatomic) NSString* (^block)(); - (NSString *)callTheBlock; @end @implementation MyClass - (NSString *)callTheBlock { if (self.block) { return self.block(); } else { return @"There is no spoon"; } } @end // In Swift // I

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

这一生的挚爱 提交于 2019-12-04 23:56:16
问题 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: ? 回答1: 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

pass block as parameter in my case

情到浓时终转凉″ 提交于 2019-12-04 22:06:27
I have a function which takes a block as parameter: typedef void (^ MyBlock)(int); -(void)doTask:(MyBlock)theBlock{ ... } I need to run above function on another thread, I want to use - performSelector:onThread:withObject:waitUntilDone: , my current code: NSThread *workerThread = [[NSThread alloc] init]; [workerThread start]; [self performSelector:@selector(doTask:) onThread:workerThread withObject:??? waitUntilDone:NO]; BUT, How can I pass MyBlock parameter with this approach? (Please don't suggest GCD, I am wondering how can I do with my current code, is it possible?) This answer assumes you

How to name a block of code and call it in a different method?

六月ゝ 毕业季﹏ 提交于 2019-12-04 21:39:10
问题 I use Grand Central Dispatch methods to do some executions of my app in a queue. I decide the frames for buttons in a calculation on that queue. I want my app to re-draw its scren and calculate new frames after rotation. Here is some pseudo code explanation from what i do: CGFloat a=123, b=24; dispatch_async(drawingQue, ^{ //needed loops to get the total button count-how many ones will be drawn et.. for(int x=0;x<someCount<x++){ for(int y=0;y<anotherCount;y++){ //needed frame&name ect

Grand Central Dispatch and unit testing

只愿长相守 提交于 2019-12-04 19:28:47
I have written a simple test case that follows Apple's documentation and I am not seeing the results that I'm expecting. Here's the code: - (void)testExample2 { NSLog(@"1"); dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"3"); dispatch_semaphore_signal(semaphore); }); NSLog(@"2"); dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); NSLog(@"4"); dispatch_release(semaphore); } I would expect to read: 1, 2, 3, 4 but instead my console just shows me 1, 3. I've been able to work around the issue using DISPATCH_TIME_NOW in a

How to use NSComparator?

北战南征 提交于 2019-12-04 18:55:16
问题 I would like to know if the below question is possible using NSComparator or not? I have two arrays; both hold data models. I have a property named rank in the data model. Now I want to compare both arrays and want to know if one of them holds higher ranked data models. If Yes I would like to get NSComparisonResult = NSOrderedAscending . By the way I'm using another approach here: is "total of each data Model's rank in array and if the total is greater than second array's data Model's total