23

The following code is returning an expression unused warning on the assignment operation in the block. The code isn't very practical, but there is a lot more code in the excluded section and that code has to run on a particular queue.

__block NSNumber *pageId=nil;
dispatch_sync(_myDispatchQueue, ^{
    int val;
    //... code generates an int and puts it in val
    pageId = [NSNumber numberWithInt:val];
}];
//pageId used below

How do I get rid of this error?

Mark Lilback
  • 1,154
  • 9
  • 21

2 Answers2

47
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-value"
 pageId = [NSNumber numberWithInt:val];
#pragma clang diagnostic pop
Matt Hudson
  • 7,329
  • 5
  • 49
  • 66
3

My Experimental Findings

Note I got this from Intrubidus, but I wanted additional information so after experimenting I recorded my findings here for the next guy.

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-value"
pageId = [NSNumber numberWithInt:val];
#pragma clang diagnostic pop

Only applies to the area between the ignore and the pop. "-Wunused-value" does not suppress unused variables.



This is how you would suppress unused variables:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
int i = 0;
#pragma clang diagnostic pop



Also, without the push and pop, as shown:

#pragma clang diagnostic ignored "-Wunused-value"
pageId = [NSNumber numberWithInt:val];

The type of warning was ignored anywhere in that file after the #pragma. This seems to only apply to the file in question.

Hope you found this useful,
    - Chase

csga5000
  • 4,062
  • 4
  • 39
  • 52