objective-c-blocks

ARC: EXC_BAD_ACCESS when calling a method from inside a block, inside a delegate method

时间秒杀一切 提交于 2019-12-05 12:34:19
I created a block, inside a delegate method and I am using it to call a static method in another class. I am getting an EXC_BAD_ACCESS error even when I have NSZombies enabled. There are a few posts on here about similar problems - I think this one is the closest: ARC: Getting EXC_BAD_ACCESS from inside block used in delegate method But, I haven't found anything so far that has helped. Here is the code: @interface MyClass() @property (nonatomic, copy) CaseBlock c; @end .... //NSURLConnection delegate method - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSDictionary *d =

Avoiding nested blocks with asynchronous code in objective-c

a 夏天 提交于 2019-12-05 09:08:31
I have a long series of events that needs to happen in my Objective-C code. Lets say I have 6 things - thingA, thingB, thingC, thingD, thingE and thingF. thingB and thingD return a BOOL. If thingB is NO, then thingC doesn't need to be called. If thingD is NO, then thingE doesn't need to be called. - (void)doThings:(void(^)())completion { [self thingA: ^{ [self thingB: ^(BOOL success) { if (success) { [self thingC: ^{ [self thingD: ^(BOOL success) { if (thingD) { [self thingE: ^{ [self thingF]; completion(); }]; return; } [self thingF]; completion(); }]; }]; return; } [self thingD: ^(BOOL

Objective-C block syntax - can someone explain this?

喜夏-厌秋 提交于 2019-12-05 08:32:25
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. Can anyone break this down? This is the block: ^( void ) { // some more code.. return (NSInputStream *)

Are Objective-C blocks supported by compilers on Linux?

和自甴很熟 提交于 2019-12-05 08:06:37
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. The official GCC does not include blocks support. For that, you either need to use Apple's patches, or use clang, an LLVM-based compiler that has good Objective-C support (because

NSOperationQueue - Getting Completion Call Too Early

萝らか妹 提交于 2019-12-05 08:00:55
问题 I am using a NSOperationQueue to queue and call a number of Geocoding location lookups. I want to call a completion method when all asynchronicly running lookups have been finished. -(void)geocodeAllItems { NSOperationQueue *geoCodeQueue = [[NSOperationQueue alloc]init]; [geoCodeQueue setName:@"Geocode Queue"]; for (EventItem *item in [[EventItemStore sharedStore] allItems]) { if (item.eventLocationCLLocation){ NSLog(@"-Location Saved already. Skipping-"); continue; } [geoCodeQueue

Unable to understand the block's lexical scope

ぐ巨炮叔叔 提交于 2019-12-05 07:54:22
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 explain me why is it so? I assume you’ve been reading bbum’s post on blocks and know that your code

Can C style blocks cause memory leaks?

筅森魡賤 提交于 2019-12-05 07:31:22
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 something like this: - (SlideViewController *)slideFromManagedObject:(Slide *)managedObject{ NSInteger

Objective-C: blocks in ARC

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 07:03:41
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 that to mean usually I don't need to do [block copy] anymore even if the block is used outside the

Allocating/showing a UIAlertView in a Block statement

痴心易碎 提交于 2019-12-05 05:43:57
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 requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { if (granted == FALSE)

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

随声附和 提交于 2019-12-05 05:35:25
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 which will be supported in all versions including 4. (There are many other similar examples.) What do