Home Convert [Float] to variables arguments
Reply: 1

Convert [Float] to variables arguments

Fabrice Froehly
1#
Fabrice Froehly Published in 2017-12-06 14:47:17Z

I'm trying to use NSString(format: , arg...) to update a UILabel by incrementing one or more values in this label.

To do that, I have a function where I am receiving the args as Float... (which is what I want) and the only solution I found to store them is by using an array [Float] (which is not what I want). Then, I'm modifying each value of my array to display it.

To modify the text of my UILabel, I am using label.text = NSString(format: "%f and %f", myArrayOfFloat) and it does not work. NSString takes the array as a single value, not as an array.

If anyone has a solution, I'm all ears.

Martin R
2#
Martin R Reply to 2017-12-06 15:12:07Z

There is a

NSString(format: String, arguments argList: CVaListPointer)

initializer which takes the substitution values as a CVaListPointer (which is the Swift equivalent of va_list in C). Example:

let myArrayOfFloat: [Float] = [1.2, 3.4]
let text = withVaList(myArrayOfFloat) {
    NSString(format: "%f and %f", arguments: $0)
}
print(text) // 1.200000 and 3.400000

However, it is simpler to create a String, using the

String(format: String, arguments: [CVarArg])

initializer, which takes the substitution values as a single array parameter. Example:

let myArrayOfFloat: [Float] = [1.2, 3.4]
let text = String(format: "%f and %f", arguments: myArrayOfFloat)
print(text) // 1.200000 and 3.400000
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.324638 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO