xcode 斯威夫特抱怨“无关参数标签”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27085490/
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 06:14:56  来源:igfitidea点击:

Swift complains "extraneous argument label"

iosxcodeswift

提问by Chris Paveglio

Trying to start some Swift work. I am using

尝试开始一些 Swift 工作。我在用

var imageData = UIImageJPEGRepresentation(image, compressionQuality:1.0)

but I get a warning "extraneous argument label 'compressionQuality' in call. I thought that in Swift the secondary parameters were either required or 'allowed' to be labelled, but this won't let me use it at all- fails building if I leave it. Since this is a system function, I can't use the # to require it. But I WOULD like to be able to name as many parameters as possible to make code more readable for myself, I like the ObjC method names, as verbose as they sometimes are.

但我收到警告“调用中的无关参数标签‘compressionQuality’。我认为在 Swift 中,辅助参数要么是必需的,要么是‘允许’被标记的,但这根本不会让我使用它-如果我构建失败离开它。由于这是一个系统函数,我不能使用#来要求它。但我希望能够命名尽可能多的参数以使代码对自己更具可读性,我喜欢ObjC方法名称,就像它们有时一样冗长。

Is there a way to set a compiler flag to allow extra argument labels?

有没有办法设置编译器标志以允许额外的参数标签?

采纳答案by Midhun MP

You can't do like that, because that function doesn't declare any external parameter name. Internal parameter names can only be used within the function that declares them.

您不能这样做,因为该函数没有声明任何外部参数名称。内部参数名称只能在声明它们的函数中使用。

In Swift UIImageJPEGRepresentationmethod is declared as:

在 Swift UIImageJPEGRepresentation方法中声明为:

func UIImageJPEGRepresentation(_ image: UIImage!,
                             _ compressionQuality: CGFloat) -> NSData! 

Check both parameters, both have internal name only so you can't use:

检查两个参数,两者都只有内部名称,因此您不能使用:

var imageData = UIImageJPEGRepresentation(image, compressionQuality:1.0)

Change that to:

将其更改为:

var imageData = UIImageJPEGRepresentation(image,1.0)


Update Swift 4.2:

更新 Swift 4.2:

In swift 4.2, the above mentioned methods are no longer available. Instead of that you need to use:

在 swift 4.2 中,上述方法不再可用。取而代之的是,您需要使用:

// For JPEG
let imageData = image.jpegData(compressionQuality: 1.0)

// For PNG
let imageData = image.pngData()

Refer the API Document for more: Images & PDF

有关更多信息,请参阅 API 文档:Images & PDF

回答by gfpacheco

I had a similar problem, but Xcode was complaining about it in one of my funcions.

我有一个类似的问题,但 Xcode 在我的一个功能中抱怨它。

Turned out to be an extra }in my code, making the subsequent function declarations to be outside my class.

结果是}我的代码中的一个额外内容,使后续的函数声明在我的类之外。

The error message was weird as hell, so I hope it hepls somebody else.

错误消息很奇怪,所以我希望它可以帮助其他人。