objective-c-blocks

NSOperation with completition block

♀尐吖头ヾ 提交于 2019-12-11 08:35:11
问题 I have some image processing that take much time and resources, so i use NSOperation + NSOperatioQueue + delegate for callBack. and all work. now i want to use blocks because its much elegant and simple to use in a tableView for example. what i need to do is just like AFJSONRequestOperation for example: NSURL *url = [NSURL URLWithString:@"url"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFJSONRequestOperation *operation = [AFJSONRequestOperation

Retain cycles when using addObserverForName:object:queue:usingBlock:

Deadly 提交于 2019-12-11 08:30:45
问题 I am new to programming with block. I have following code (not using arc) in my Listener class: - (void)someBlock:((void)^(NSDictionary *)myDictionary)myBlock { __block Listener *weakSelf = self; weakSelf = [[NSNotificationCenter defaultCenter] addObserverForName:@"MyNotification" object:nil queue:nil usingBlock:^(NSNotification *note) { //--- Here have the retain cycles myBlock(note.userInfo); [[NSNotificationCenter defaultCenter] removeObserver:weakSelf name:@"MyNotification"]; }]; } and in

Please explain the usefulness of blocks in Objective-C

只谈情不闲聊 提交于 2019-12-11 07:24:26
问题 I've recently been trying to understand the importance of blocks in programming (Objective-C in particular). They're obviously used quite a lot in iOS/Cocoa APIs, so I'm trying to understand them. Mostly, I still don't understand why you would use a block versus just creating a separate helper function. For example, if I create a block that implements some sorting function, wouldn't it be easier to create that function as a method of a helper class so all objects in the code could use it more

method returns before completionhandler

纵饮孤独 提交于 2019-12-11 06:58:42
问题 I am developing a networkUtil for my project, I need a method that gets a url and returns the JSON received from that url using NSURLSessionDataTask to get a JSON from server. the method is the following: + (NSDictionary*) getJsonDataFromURL:(NSString *)urlString{ __block NSDictionary* jsonResponse; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:urlString] completionHandler:^(NSData *data, NSURLResponse

What qualifier should I use to declare a block as an ivar?

冷暖自知 提交于 2019-12-11 06:23:30
问题 Example: typedef void(^responseBlock)(NSDictionary*, NSError *); @interface MyClass : NSObject { [??] responseBlock responseHandler; } What qualifier should I put in the [??] brackets? I've read that blocks as properties should be setup with the copy qualifier...but in this case I don't need the block exposed as a property. I simply want it to remain an ivar but how can I specify copy? And also, without specifying anything what is the default qualifier used? Is it __strong as in the case with

obj-c weak self in a block: why the 2nd one doesn't need a weak self inside in two similar cases

懵懂的女人 提交于 2019-12-11 04:58:41
问题 I finally found my memory bug is caused by referring self strongly in a block. But I don't know why in a similar case, the weak is not needed: I have a CameraCaptureManager class doing image capture tasks, and a CameraViewController has a strong property of this manager. The manager has weak delegate property pointing back to the controller. This is where I must use weakSelf in the manager, otherwise -(void)dealloc won't be called: // in CameraCaptureManager __weak CameraCaptureManager

ObjectiveC Blocks and Variables

孤街醉人 提交于 2019-12-11 03:58:46
问题 I have the following method that makes a webservice call from my iOS app (using Restkit)... BOOL valid = NO; RKObjectManager *objectManager = [RKObjectManager sharedManager]; NSString *servicePath = [WebServiceHelper pathForServiceOperation:[NSString stringWithFormat:@"/security/isSessionValid/%@", username]]; [objectManager getObjectsAtPath:servicePath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { BooleanServiceResponse *resp = [mappingResult

Phillips HUE SDK in Swift

醉酒当歌 提交于 2019-12-11 03:15:25
问题 I am very new to Swift! I do have some experience with Objective-C (although it has been around two years since i've really written anything in it). I am trying to use the Phillips HUE SDK with Swift, and am having some trouble! I am trying to re-write the following Objective-C code into Swift: // Start search for bridges [self.bridgeSearch startSearchWithCompletionHandler:^(NSDictionary *bridgesFound) { //Search complete [self showBridgesFound:bridgesFound]; } I have had no luck looking

What happens to a block at compile time, and can I create one at runtime?

烈酒焚心 提交于 2019-12-11 02:36:12
问题 This is a two-part question about blocks (^{}) in Objective-C . I have searched for some answers, but nothing has showed up in Google or SO. This question stems from a desire to create an custom XML Layout Engine for iOS, with support for blocks - meaning that I want to parse NSStrings and create a block during runtime. 1) Is this even possible? If so, how can it be done? Not being able to find much on NSString to Block , I thought that the reason may be how a block is handled by the compiler

Is there anything like an Objective-C block in Java?

老子叫甜甜 提交于 2019-12-11 02:29:35
问题 I'm just learning Java, and I've developed apps in Objective-C before. I like the concept of "blocks", because they allow code to be ran after something happens. For example, to execute a block after a certain amount of time in a SpriteKit app that calls a method helloWorld from scene myScene : [myScene runAction:[SKAction sequence:@[[SKAction waitForDuration:5], [SKAction runBlock:^{ [myScene helloWorld]; }]]]]; Is there anything like a block in Java? If so, how would I use it? What's the