Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift complains "extraneous argument label"

Tags:

xcode

ios

swift

Trying to start some Swift work. I am using

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.

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

like image 894
Chris Paveglio Avatar asked Nov 23 '14 04:11

Chris Paveglio


2 Answers

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

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.

like image 96
gfpacheco Avatar answered Sep 20 '22 14:09

gfpacheco


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 UIImageJPEGRepresentation method is declared as:

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:

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

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

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

Refer the API Document for more: Images & PDF

like image 31
Midhun MP Avatar answered Sep 18 '22 14:09

Midhun MP