斯威夫特抱怨"无关的争论标签"

Chr*_*lio 11 xcode ios swift

试图开始一些Swift工作.我在用

var imageData = UIImageJPEGRepresentation(image, compressionQuality:1.0)
Run Code Online (Sandbox Code Playgroud)

但是我在调​​用中得到了一个警告"无关的参数标签'compressionQuality'.我认为在Swift中,二级参数要么被标记要么被允许标记,但这不会让我使用它 - 如果我失败了由于这是一个系统函数,我不能使用#来要求它.但是我希望能够为尽可能多的参数命名以使代码对我自己更具可读性,我喜欢ObjC方法名称,像他们有时一样冗长.

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

Mid*_* MP 9

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

在Swift中,UIImageJPEGRepresentation方法声明为:

func UIImageJPEGRepresentation(_ image: UIImage!,
                             _ compressionQuality: CGFloat) -> NSData! 
Run Code Online (Sandbox Code Playgroud)

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

var imageData = UIImageJPEGRepresentation(image, compressionQuality:1.0)
Run Code Online (Sandbox Code Playgroud)

改为:

var imageData = UIImageJPEGRepresentation(image,1.0)
Run Code Online (Sandbox Code Playgroud)

更新Swift 4.2:

在swift 4.2中,上述方法不再可用.而不是你需要使用:

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

// For PNG
let imageData = image.pngData()
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅API文档:图像和PDF