user1659987
user1659987

Reputation: 85

iOS : Why am I getting 'Unused variable' in Xcode?

I'm coding to display a php echo

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setTimeoutInterval:180.0]; 
[request setURL:[NSURL URLWithString:@"http://localhost:8888/MAMP/signup/getkey.php"]]; 
[request setHTTPMethod:@"POST"];

NSString *key = [[NSString alloc] initWithData:[NSURLConnection    sendSynchronousRequest:request returningResponse:nil error:nil] encoding:NSUTF8StringEncoding];

When I compile, I have this warning message : Unused variable key

Where is the problem?

Upvotes: 0

Views: 5920

Answers (3)

David.Chu.ca
David.Chu.ca

Reputation: 38634

If you don't need to use the key, simply comment it out by //

// NSString *key = [[NSString alloc] initWithData:[NSURLConnection    sendSynchronousRequest:request returningResponse:nil error:nil] encoding:NSUTF8StringEncoding]; 

Upvotes: 0

Joe
Joe

Reputation: 2987

It is because you don't do anything with key. You simply alloc and init it, but never really use it.

You could do something like NSLog(@"%@",key); and the error will go away.

The other option is to set Unused Variables to warnings. Go to your project target, Build settings, then find the below image and change Unused Variables to Yes. This will change it from and error to a warning.

enter image description here

Upvotes: 5

alanmanderson
alanmanderson

Reputation: 8200

On the last line you are assigning key to a value but you never use it. You can configure it to not throw an error, but rather a warning if you do not use a variable. Otherwise just comment out the assignment of key.

Upvotes: 0

Related Questions