if(image != nil){
mediaSize = String(image!.size.width) + "|" + String(image!.size.height)
}
The result should be like "400|300"
I get the error: No exact matches in call to initializer
image
is UIImage
and I’m uploading it to the server before I try to convert the size to string and it’s valid
Other questions about this aren’t helping
2
Answers
The problem is that both
height
andwidth
areCGFloat
, which cannot be directly converted to aString
usingString.init
.You can either convert them to
Double
and then initialise aString
from theDouble
Or simply use String interpolation, which does support
CGFloat
.However, be aware that you’re converting floating point numbers to a
String
, so you might end up with a non user friendly value with tons of decimal places. If you know that your values are integers, convert them to integers usingInt(image.size.width)
or format them to a fixed number of decimal places using eitherString(format:_:)
or aNumberFormatter
.You can also use
String(format:...)
:The
%0.0f
means convert float to string, with NO decimals.So your result would be
"400|300"