objective-c-blocks

NSOperationQueue: Trouble understanding the order [duplicate]

自古美人都是妖i 提交于 2019-12-06 02:32:22
This question already has answers here : NSOperationQueue serial FIFO queue (3 answers) Closed 5 years ago . I'm having trouble understanding the way NSOperationQueue works. Say I have: NSOperationQueue *queue = [[NSOperationQueue alloc] init]; queue.maxConcurrentOperationCount=1; [queue addOperationWithBlock:^{ [someObject someSelector]; }]; [queue addOperationWithBlock:^{ [someObject anotherSelector]; }]; The second block is being called even before the first block finishes - the opposite of what I want. I tried using – performSelectorOnMainThread:withObject:waitUntilDone: instead, but the

Internal implementation of UIView's block-based animation methods

大城市里の小女人 提交于 2019-12-06 02:09:28
问题 Ever since their introduction in iOS 4, I have been wondering about the internal implementation of the UIView 's block-based animation methods. In particular I would like to understand what mystical features of Objective C are used there to capture all the relevant layer state changes before and after execution of the animation block. Observing the black-box implementation, I gather that it needs to capture the before-state of all layer properties modified in the animation block, to create

Strange ordering of Kiwi iOS context blocks

∥☆過路亽.° 提交于 2019-12-06 01:21:17
I have a Kiwi spec file that looks something like this: #import "Kiwi.h" #import "MyCollection.h" SPEC_BEGIN(CollectionSpec) describe(@"Collection starting with no objects", ^{ MyCollection *collection = [MyCollection new]; context(@"then adding 1 object", ^{ MyObject *object = [MyObject new]; [collection addObject:object]; it(@"has 1 object", ^{ [collection shouldNotBeNil]; [collection.objects shouldNotBeNil]; [[theValue(collection.objects.count) should] equal:theValue(1)]; //failing test }); context(@"then removing 1 object", ^{ [collection removeObject:object]; it(@"has 0 objects", ^{ [

Is it possible to define a block as a member of a class?

六月ゝ 毕业季﹏ 提交于 2019-12-05 23:22:59
问题 I'm trying to implement a very simple strategy class in Objective-C that allows for strategies to be defined inline instead of being defined through inheritance. Currently my code looks like this: @interface SSTaskStrategy : NSObject { (NSArray *)(^strategy)(void); } @end I thought this would work, but I'm getting the error Expected specifier-qualifier-list before '(' token Any ideas how to make this work? 回答1: You should drop the parentheses around NSArray * in your ivar definition:

In which thread are iOS completion handler blocks called?

旧时模样 提交于 2019-12-05 21:00:17
问题 For example, in GKScore 's reportScoreWithCompletionHandler (documentation), suppose you call [score reportScoreWithCompletionHandler:^(NSError *error) { // do some stuff that may be thread-unsafe }]; In which thread will the completion handler be called: the main thread, the same thread as reportScoreWithCompletionHandler was called, or a different thread (presumably the thread that the actual score reporting is done)? In other words, does the work done in the completion handler need to be

What is a simple rule to avoid objective-c block retain cycle memory leaks?

廉价感情. 提交于 2019-12-05 18:51:04
I've had it with memory leaks arising from block retain cycles. I would just like a simple rule that I can apply to my code to make sure I avoid them. On the other hand, I don't want to update half of my code base base to __weak pointers without it being necessary. Here is what I have up to now: There can be no memory leaks when you use the following: dispatch_async(queue, ^{...}); // GCD call. [Foo bar:^{...}]; // Class "+" methods with completion block. However, these cases will cause block retain cycle memory leaks for sure: self.myPropertyBlock = ^{ self; }; _myInstanceVariableBlock = ^{

“Converting” a function pointer to a block in objective-C

强颜欢笑 提交于 2019-12-05 17:36:56
问题 I'm doing some Interop from Mono C# to Obj-C and ran into this problem. The C# code needs to pass a callback - which it does with a function pointer. I can get the function pointer from the Obj-C side and call it and everything works. But I now need to give that function pointer as a callback to third party API which works with blocks as a callback. I want the third party to call the C# function - so in a way i'm trying to either convert the function pointer to a block so the third party can

iOS - Unit Testing Asynchoronous code

佐手、 提交于 2019-12-05 16:29:33
The part of a method that I am trying to test is as follows: - (void)configureTableFooterView { dispatch_async(dispatch_get_main_queue(), ^{ self.tableView.tableFooterView = nil; if ([self.parser.resultSet isLastPage]) { return; } }); } I have written the unit test as follows: - (void)testTableFooterViewConfigurationAfterLastPageLoaded { id mockTableView = OCMClassMock([GMGFlatTableView class]); OCMExpect([mockTableView setTableFooterView:[OCMArg isNil]]); id resultSet = OCMClassMock([GMGResultSetInfo class]); OCMStub([resultSet isLastPage]).andReturn(YES); OCMStub([self.mockParser resultSet])

How can I perform the handler of a UIAlertAction?

房东的猫 提交于 2019-12-05 15:13:09
I'm trying to write a helper class to allow our app to support both UIAlertAction and UIAlertView . However, when writing the alertView:clickedButtonAtIndex: method for the UIAlertViewDelegate , I came across this issue: I see no way to execute the code in the handler block of a UIAlertAction . I'm trying to do this by keeping an array of UIAlertAction s in a property called handlers @property (nonatomic, strong) NSArray *handlers; and then implement a delegate like such: - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { UIAlertAction *action = self

Executing Code Block In Place Of @selector

你说的曾经没有我的故事 提交于 2019-12-05 12:42:07
I'm creating a barButton which when pressed should set the editing mode of a UITableView to yes. Here's my code: self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @"Edit" style: self.navigationController.navigationItem.leftBarButtonItem.style target: self action: ]; What I don't understand is what I need to put as an argument for the action part so that I can execute a code block there. I could easily enough put @selector(someMethod) but I'm only executing one or two lines and creating another method is pretty pointless. Thanks for any help! Further to pgb's