Best practice - Variants

Dears,

This is my very first post, and I'm still learning Swift with Xcode, so...


I'm planning to create an app, with a lot of varriants, more than 100...

All of the variants are editable, and the user can access it anytime.


What would be the best practice to work with variants?

Should I create a Cacoa Class just for the variants?


or, would you declare your variants at the top of each func?


How would you deal with it?


Thank you

Accepted Reply

String is a text, like "This is a text" or "some value: 123.45" or "123.45"


Float is a number, as 123.45


You can compute on number as

let freq = 123.45

let doubleFreq = 2 * freq


You cannot compute on Strings.


However, you can convert back and forth:


let freqString = "123.45"

let freq = Float(freqString) // That will be an optional, so usually do this

let freq = Float(freqString) ?? 0 // If freqString cannot be converted, will be zero, otherwise, the value in the string


the other direction

let freq = 123.45

let freqString = String(freq)

Replies

What do you mean by "variants" ?


Show some code example to make it clear.


Anyway, redeclaring at the top of each func does not seem the right way to go.

Example:


var radioFreqOne: String = "123.45"

var radioFreqTwo: String = "126.50"

var radioFreqThree String = "133.45"

var radioFreqFour: String = "136.50"


I'll have something aroung 200 var


And so on...

You should probably declare an array of String, with all values, outside of the class definition, so that it has global visibility.

And access where you need by:

radioFreq[i] // Take care, array starts at zero index


What do you mean:

All of the variants are editable, and the user can access it anytime


Do you mean that user can modify the values ?

then when the newValue is defined:

radioFreq[i] = newValue // (a String in that case)


But why store Strings and not Float values ?

That's very interesting...

What's the difference between stored Strings and Float values?


Thank you

String is a text, like "This is a text" or "some value: 123.45" or "123.45"


Float is a number, as 123.45


You can compute on number as

let freq = 123.45

let doubleFreq = 2 * freq


You cannot compute on Strings.


However, you can convert back and forth:


let freqString = "123.45"

let freq = Float(freqString) // That will be an optional, so usually do this

let freq = Float(freqString) ?? 0 // If freqString cannot be converted, will be zero, otherwise, the value in the string


the other direction

let freq = 123.45

let freqString = String(freq)

Ok, I got it...


That's a very interesting solution.

I'll try using an array of float values.


By the way... can I use .gif images as background of my UIButtons?

Thank you so much

I it advised usually to use png images.