Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get rid of warning "Expression Result Unused"

I have the following few lines of code:

NSURL *url = [NSURL URLWithString:URL];
NSURLRequest* request = [NSURLRequest requestWithURL:url .... ];
NSURLConnection* connection = [NSURLConnection alloc];
[connection initWithRequest:request delegate:self];

In the last line I get "Expression Result Unused" warning. Now, according to all the articles online I have read, this is the correct way to call a method, and the syntax is as advised to download a URL async. How to rewrite this code to fix the warning?

like image 461
user230910 Avatar asked May 19 '15 08:05

user230910


2 Answers

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-value"
NSURL *url = [NSURL URLWithString:URL];
NSURLRequest* request = [NSURLRequest requestWithURL:url .... ];
NSURLConnection* connection = [NSURLConnection alloc];
[connection initWithRequest:request delegate:self];
#pragma clang diagnostic pop

For a list of all the Clang Warnings you can suppress take a look here

like image 68
Lefteris Avatar answered Oct 20 '22 00:10

Lefteris


The problem comes from the fact that method NSURLRequest initWithRequest… return an object that you don't store.

If you don't need it, you should write:

(void)[connection initWithRequest:request delegate:self];

On Xcode, you can use qualifier __unused to discard warning too:

__unused [connection initWithRequest:request delegate:self];

to inform the compiler that you deliberately want to ignore the returned value.

like image 40
Nicolas Buquet Avatar answered Oct 20 '22 01:10

Nicolas Buquet