iOS:为什么我在Xcode中获得"未使用的变量"?

use*_*987 0 iphone variables xcode warnings

我正在编码显示一个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];
Run Code Online (Sandbox Code Playgroud)

当我编译时,我有这个警告信息: Unused variable key

问题出在哪儿?

Joe*_*Joe 5

这是因为你没有做任何事情key.你只需要分配并初始化它,但从不真正使用它.

你可以做类似的事情NSLog(@"%@",key);,错误就会消失.

另一种选择是设置Unused Variables为警告.转到项目目标,构建设置,然后找到下面的图像并更改Unused VariablesYes.这会将其从错误更改为警告.

在此输入图像描述