objective-c-blocks

“__block” variable results in nil value when go out of block

两盒软妹~` 提交于 2019-12-07 05:25:36
问题 I wanna use __block variable to get value in block. But when out of block, the __block variable seems to be nil. Why this would happen? NSString *fileName = [Tools MD5Encode:url]; __block NSString *filePath = nil; [fileList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSString *aFileName = obj; if ([aFileName isEqualToString:fileName]) { NSString *path = [VERSIONS_INFO_DATA_DIRECTORY stringByAppendingPathComponent:aFileName]; filePath = path; NSLog(@"filePath1 %@",

Objective-C: blocks in ARC

萝らか妹 提交于 2019-12-07 04:01:05
问题 I'm transitioning to ARC (Automatic Reference Counting) and this document was very helpful: Transitioning to ARC Release Notes. It says: How do blocks work in ARC? Blocks “just work” when you pass blocks up the stack in ARC mode, such as in a return. You don’t have to call Block Copy any more. You still need to use [^{} copy] when passing “down” the stack into arrayWithObjects: and other methods that do a retain. Can somebody elaborate on this a little bit or give some example? I understood

Are Objective-C blocks supported by compilers on Linux?

 ̄綄美尐妖づ 提交于 2019-12-07 03:14:51
问题 How do I compile the following code on linux? Using Ubuntu 10.10 (Maverick Meerkat). #include <stdio.h> #include <stdlib.h> int main() { void (^block)() = ^{ printf("Hello world"); }; block(); } I tried: gcc -x objective-c t.c And got: t.c: In function 'main': t.c:5: error: expected identifier or '(' before '^' token Any guidance on how to make this work is appreciated. Edited question based on feedback, thanks. 回答1: The official GCC does not include blocks support. For that, you either need

Can C style blocks cause memory leaks?

谁都会走 提交于 2019-12-07 03:05:54
问题 I'm working on a kiosk style slideshow app. I have a UIScrollView which shows the slides, and a factory class, which generates the slides. The "slides" themselves are UIViewController subclasses, which are loaded out from XIB files and customized by the factory class. In my main view controller, I set up the scroll view and start a timer. The timer calls a "reload" method every N seconds, which handles the reload and call to the factory class. The method that the factory class uses looks

Any way to have completionHandler style syntax using [facebook authorize] on iOS 5?

孤者浪人 提交于 2019-12-06 23:12:31
The code below is trying to lazy login to Facebook right before posting a photo, but has an asynchronous problem. In the logs, the after isSessionValid block will appear before the fbDidLogin and then a facebookErrDomain error 10000 will happen ("OAuthException", "active access token must be used", etc). MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; if (![appDelegate.facebook isSessionValid]) { [appDelegate.facebook authorize:[NSArray arrayWithObjects:@"publish_stream", @"user_photos", nil]]; } NSLog(@"after isSessionValid block"); NSData

Objective-C Callbacks/Block Pattern

浪子不回头ぞ 提交于 2019-12-06 17:42:25
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 an AFJSONRequestOperation which has a success block on it. I need a little direction on how to

If a strong property has a block that refers to self, is that a retain cycle?

风格不统一 提交于 2019-12-06 15:41:10
For instance: [self.contentWrapperView addGestureRecognizer: [UITapGestureRecognizer recognizerWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) { if (self.customEditing) { [self setEditingMode:NO Animated:YES]; } }]]; Where contentWrapperView is a strong property on self , and presuming contentWrapperView has a strong reference to the recognizer block. Is using self in the block going to lead to a retain cycle? This is the only part I don't quite understand. Yes it will lead to a retain cycle. The workaround incase you care is __weak id weakSelf =

How to access user input from UIAlertView completion block without delegation?

房东的猫 提交于 2019-12-06 13:31:59
问题 Using iOS6: I would like to retrieve the text entered by a user into a UITextField associated with the UIAlertView. I am aware that I could achieve the desired result with a delegate however I am curious about solving this issue with a callback function as I believe this may be an interesting pattern. I began by examining a common pattern for category extension of the UIAlertView class. Code below. Thanks in advance for any suggestions. import <UIKit/UIKit.h> @interface UIAlertView (Block) -

Reading CGImageRef in a background thread crashes the app

眉间皱痕 提交于 2019-12-06 11:37:37
i have a big jpeg image that i want to load in tiles asynchronously in my opengl engine. Everything works well if its done on the main thread but its slow. When i try to put the tile loading on an NSOperationBlock, it always crashes when trying to access the shared image data pointer that i previously loaded on the main thread. There must be something i don't get with background operation because i assume i can access memory sections that i created on the main thread. What i try to do is the following : @interface MyViewer { } @property (atomic, assign) CGImageRef imageRef; @property (atomic,

The execution of block is always delayed

做~自己de王妃 提交于 2019-12-06 11:31:51
问题 I'm pretty new to IOS. I want to get all pictures on a device in viewDidLoad . but the block is always executed after I called NSLog(@"%d", photos.count) in the code as follows. How to handle such a case? __block NSMutableArray* photos = [[[NSMutableArray alloc] init] retain]; ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; void (^assertsEnumerator)(ALAsset *, NSUInteger , BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) { if(result) { NSLog(@"Assert name : %@", [result