iOS:为什么我在 Xcode 中得到“未使用的变量”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12358406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:27:00  来源:igfitidea点击:

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

iphonexcodevariableswarnings

提问by user1659987

I'm coding to display a phpecho

我正在编码以显示phpecho

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

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

Where is the problem?

问题出在哪儿?

回答by Joe

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

这是因为你没有对key. 您只需分配和初始化它,但从未真正使用它。

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

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

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

另一个选项是设置Unused Variables为警告。转到您的项目目标,构建设置,然后找到下图并更改Unused VariablesYes. 这会将它从和错误更改为警告。

enter image description here

在此处输入图片说明

回答by ajon

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.

在最后一行,您将键分配给一个值,但您从未使用过它。您可以将其配置为不抛出错误,而是在不使用变量时发出警告。否则只需注释掉键的分配。

回答by David.Chu.ca

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];