objective-c-blocks

ARC: Getting EXC_BAD_ACCESS from inside block used in delegate method

馋奶兔 提交于 2019-12-10 11:23:55
问题 I must be doing something wrong, but the Automatic Reference Counting docs don't give me a hint on what it might be. What I'm doing is calling a method with a block callback from inside a delegate method. Accessing that same delegate from inside the block results in a bad access . The problem is the object I'm passing - loginController which is sending the message to its delegate - is clearly not released, when I don't access it inside the block I can call the method multiple times without an

Objective-C block syntax - can someone explain this?

对着背影说爱祢 提交于 2019-12-10 05:00:45
问题 Can anyone explain how this block syntax works? AStreamBuilder stream_builder = [ [ ^( void ) { // some more code.. return (NSInputStream *)[ NSInputStream inputStreamWithFileAtPath: some_path ]; } copy ] autorelease ]; return stream_builder; What's the name of the block here? Why is the block being copied and then autoreleased? I'm sort of confused with what's going on here.. the block is said to return AStreamBuilder but inside the body of the block it returns an instance of NSInputStream.

Unable to understand the block's lexical scope

依然范特西╮ 提交于 2019-12-10 04:24:40
问题 To understand the lexical scope of block, I have write the following code typedef int (^ MyBlock)(void); MyBlock b[3]; for (int i=0; i<3; i++) { b[i]=^{return i;}; } for (int i=0; i<3; i++) { NSLog(@"%d",b[i]()); } NSLog(@"----------------------------"); int j=0; b[0]=^{return j;}; j++; b[1]=^{return j;}; j++; b[2]=^{return j;}; for (int i=0; i<3; i++) { NSLog(@"%d",b[i]()); } first time o/p is 2,2,2 second time o/p is 0,1,2 I am expecting 2,2,2 for both of block execution. Can anybody please

Should i use iOS 4 new features in my app? and why?

流过昼夜 提交于 2019-12-10 04:10:03
问题 I am updating one of my apps and I have a dilemma: In places I wanted to add iOS 4 unique features I had no choice but to implement them only for supported devices - no dilemma here. The dilemma is when I have 2 ways to achieve the same effect, one in the "old way" and one in a "new way". A good example is using blocks for animation, I can use this syntax: [UIView animateWithDuration:2 animations:^{ self.segmentedControl.alpha=0; }]; that will be supported in iOS 4.0 only. or use the old way

Allocating/showing a UIAlertView in a Block statement

随声附和 提交于 2019-12-10 03:38:35
问题 I'm pretty new to blocks in objective C. I've read the docs and I have a pretty basic understanding of them. Why won't this work? This is a framework callback for requesting Calendar access. It takes a block as an argument. All I want to do is allocate and show the UIAlertView in the block, but it will crash when it tries to show. I hope this isn't a silly question... all the intro examples on the net using blocks just show trivial examples with counters. //Request access [eventStore

Swift closure crashes when called as Objective-C block

纵然是瞬间 提交于 2019-12-10 02:04:29
问题 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

“Missing __block type specifier” compilation error after refactoring code but ok before refactor

感情迁移 提交于 2019-12-09 18:25:44
问题 I have some code that downloads an image and assigns it within a block. The code currently works however I want to refactor it into a separate method, however after the refactoring I get a compilation error. This is the original code which compiles and runs with the downloaded image being assigned successfully: - (void) someMethod { … MyObject* object = [[MyObject alloc] init]; [self.objects addObject: object]; NSString* graphRequest = [NSString stringWithFormat:@"%@%@%@", @"https://graph

Assigning objects to variable outside a block

老子叫甜甜 提交于 2019-12-09 16:08:10
问题 The following code crashes, since the contents of sentence go away when the final block exits. #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // simple block test - just iterate over some items and // add them to a string NSArray *items = [NSArray arrayWithObjects:@"why ", @"must ", @"this ",nil]; __block NSString *sentence = @""; [items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

Custom Modal Window with Block Completion Handler

瘦欲@ 提交于 2019-12-09 13:30:00
问题 I'm stuck! I am trying to create a custom modal dialog. I would like it to perform similarly to the NSSavePanel using a block as a completion handler. I've copied only the important snippets I think are needed. @implementation ModalWindowController - (void)makeKeyAndOrderFront:(id)sender modalToWindow:(NSWindow*)window sourceRect:(NSRect)rect completionHandler:(void (^)(NSInteger result))handler { _handler = [handler retain]; session = [NSApp beginModalSessionForWindow:[self window]]; [

how to add an extra argument to a block

穿精又带淫゛_ 提交于 2019-12-09 03:58:23
问题 Mailcore has a cool method that downloads an attachment and accepts a block as a parameter to return download progress: - (CTCoreAttachment *)fetchFullAttachmentWithProgress:(CTProgressBlock)block; where CTProgressBlock is defined as typedef void (^CTProgressBlock)(size_t curr, size_t max); so typically I would use it like so: //AttachmentDownloader.m int fileNum = x; // explained later CTCoreAttachment* fullAttachment = [attachment fetchFullAttachmentWithProgress:^(size_t curr, size_t max) {