objective-c-blocks

how to pass block as a macro's argument in objective-c?

二次信任 提交于 2019-12-01 17:09:21
In my code i have a lot of code like: if (block) block(....) So I want to define a macro, something like #define safetyCall(block, ...) if((block)) {block(##__VA_ARGS__)}; But i couldn't get it to work. Any idea? You don't need the ## and the ; needs moving: #define safetyCall(block, ...) if((block)) { block(__VA_ARGS__); } This can run into issues if your block is inline and contains code that has a series of comma separated strings, etc. Example: safetyCall(^void() { NSArray *foo = @[@"alice", "bob"]; }; The compiler will complain about "Expected ']' or '.'" and "Expected identifier or '('".

how to pass block as a macro's argument in objective-c?

白昼怎懂夜的黑 提交于 2019-12-01 16:22:24
问题 In my code i have a lot of code like: if (block) block(....) So I want to define a macro, something like #define safetyCall(block, ...) if((block)) {block(##__VA_ARGS__)}; But i couldn't get it to work. Any idea? 回答1: You don't need the ## and the ; needs moving: #define safetyCall(block, ...) if((block)) { block(__VA_ARGS__); } 回答2: This can run into issues if your block is inline and contains code that has a series of comma separated strings, etc. Example: safetyCall(^void() { NSArray *foo

why the type of `nil` is not `id` but `void *`

余生长醉 提交于 2019-12-01 16:21:09
In this code id (^block)(void) = ^(void) { return nil; }; I have this error Incompatible block pointer types initializing 'id (^__strong)(void)' with an expression of type 'void *(^)(void)' So I have to explicitly cast nil to id type id (^block)(void) = ^(void) { return (id)nil; }; to make compiler happy. Buy why nil is not id type? And for this code __typeof__(nil) dummy; dummy = [NSObject new]; Implicit conversion of Objective-C pointer type 'NSObject *' to C pointer type 'typeof (((void *)0))' (aka 'void *') requires a bridged cast which is saying nil is (void *)0 , but isn't just same as

How to set UIActivityViewController Gmail Share Subject different than body

谁说我不能喝 提交于 2019-12-01 16:01:27
I am using Gmail Share Extension from Google. I am providing implementation of: - (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType; - (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController; - (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType; For Mail client (from Apple) it goes into delegate method below but Gmail - (NSString *)activityViewController:(UIActivityViewController *)activityViewController

Create an optional block as a variable

依然范特西╮ 提交于 2019-12-01 15:43:07
I have a simple class where I declare a block as a variable: class MyObject : NSObject { var progressBlock:(progress:Double) -> ()? init() { } } As far as I understand, if defined this way, progressBlock does not have to be initialized in the init() initializer However, when I try to compile I get his error: Property 'self.progressBlock' not initialized at super.init So the question is, how do I create an optional progressBlock , so I don't get this error? The way you have written it, the compiler assumes progressBlock is a closure that returns an optional empty tuple instead of an optional

why the type of `nil` is not `id` but `void *`

断了今生、忘了曾经 提交于 2019-12-01 15:15:43
问题 In this code id (^block)(void) = ^(void) { return nil; }; I have this error Incompatible block pointer types initializing 'id (^__strong)(void)' with an expression of type 'void *(^)(void)' So I have to explicitly cast nil to id type id (^block)(void) = ^(void) { return (id)nil; }; to make compiler happy. Buy why nil is not id type? And for this code __typeof__(nil) dummy; dummy = [NSObject new]; Implicit conversion of Objective-C pointer type 'NSObject *' to C pointer type 'typeof (((void *

Why is this CLLocationCoordinate2D variable unassignable?

我的梦境 提交于 2019-12-01 14:24:59
I have a geocoder method and I want it to return the CLLocationCoordinate2D that it generates for me. - (CLLocationCoordinate2D)geocode{ CLGeocoder *geocoder = [[CLGeocoder alloc] init]; CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(0,0); [geocoder geocodeAddressDictionary:self.placeDictionary completionHandler:^(NSArray *placemarks, NSError *error) { if([placemarks count]) { CLPlacemark *placemark = [placemarks objectAtIndex:0]; CLLocation *location = placemark.location; coordinate = location.coordinate; } else { NSLog(@"error"); } }]; return coordinate; } The line coordinate

Method BOOL return from inside block

隐身守侯 提交于 2019-12-01 13:40:16
I'm trying to add Beeblex's new In App Purchase verification to my app, however i'm struggling passing a return value from within a block. Here's the code I have now, and as you can see I set a BOOL value, then within the verification block I set the BOOL and return it at the end. However the return at the end is called before the block finishes, so what I need is to return the BOOL from within the block? - (BOOL)verifyTransaction:(SKPaymentTransaction *)transaction { if (![BBXIAPTransaction canValidateTransactions]) { return YES; // There is no connectivity to reach the server } BOOL __block

Do I need to use a weak self pointer if a method called from a Block uses self?

坚强是说给别人听的谎言 提交于 2019-12-01 13:29:57
Using self. in blocks causes retain cycles, so I need to create a reference to weakSelf . I understand this BUT! If from my block I call a method which uses self ", does this too cause a retain cycle? For instance, if I reload a UITableView from a block and in some of my UITableView delegate methods I call self. , am I causing a retain cycle? Does that mean I have to pass around this weak reference everywhere? Seems hokey. I might be misreading your question, but your wording: If from my block I call a method which uses "self.", does this too cause a retain cycle? For instance if I reload a

Are blocks in Objective-C are really useful? What can their utility be? [closed]

倾然丶 夕夏残阳落幕 提交于 2019-12-01 12:00:20
I've just read about blocks, and I understood that they just encapsulate information as a normal method, but with their own strong referenced data. I'm wondering what a good use of blocks could be? Here's a use for blocks that applied itself to my project; replacing delegates and protocols (in certain situations). The Problem Say you need to asynchronously load data from a server. You might have a method that needs to PUT to a path (with data), and then eventually, when the task is completed, send the results to the method caller. Delegate and Protocol Solution Here's our method signature from