Swift - Binary operator '>=' cannot be applied to operands of type 'String' and 'Int' - swift

Not really understanding why this isn't working. I'm pretty new to the Swift world.
The error I'm getting is Binary operator '>=' cannot be applied to operands of type 'String' and 'Int'
Could anyone help me understand why I'm getting this error? Do I need to convert the String to a Double or is there something else I'm totally missing? Again I'm new to Swift.

Do I need to convert the String to a Double?
Yes, that's basically it.
You must declare first a variable to accumulate all the inputs:
var inputs = [Double]()
Observe that I'm declaring an array of Double because that's what we are interested in.
Then, each time you ask the input, convert the obtained String to Double and store it in your array:
print("Please enter a temperature\t", terminator: "")
var message : String = readLine()!
let value : Double = Double(message)!
inputs.append(value)
Finally, check all the accumulated values in inputs (you got this part right):
for value in inputs {
// value is already a Double
if value >= 80 {
message = "hot!"
}
// etc.
}
I suggest researching how to convert to Double with error checking (i.e. how to detect "100 hot!" and ignore it because can't be converted).
Also, consider using a loop to read the values.

Related

Binary operator '-' cannot be applied to two 'Double?' operands

var result = (dimensione-base)/2
dimensione & base are two "Double?" number, how can i solve this?
i want to calculate this, but he always gives me this error:"Binary operator - cannot be applied to two "double?" operands.
Double? is an optional type. That means a variable of this type can be nil. So before making an operation with them, you need to make sure these variables actually hold a value :
if let dimensione = dimensione,
let base = base
{
var result = (dimensione-base)/2
// Use result here
}else{
// Do something here when dimensione or base is nil.
}
You could also prefer to assign them a default value when they are nil (but in this case it seems less secure) :
var result = ((dimensione ?? 0)-(base ?? 0))/2
Here, if nil, 0 will be used instead.

How to convert from UITextField to Decimal

I have a UITextField with a decimal pad. I try to do some math with the value in it but I can't figure it out.
To get a string as an int, I do this:
let number:Int? = Int(self.number.text!)
...which works.
But I don't understand why I can't do the following for decimal:
let decimalNumber:Decimal? = Int(self.decimalNumber.text!)
I get an error message:
Cannot invoke initializer for type 'Decimal' with an argument of list of type '(String)'
With my javascript knowledge, I find it hard to deal with values coming from a field on iOS :)

Cannot assign value of type 'String' to type 'NSTimeInterval' (aka 'Double')

I'm trying to set my sliders value to the sounds currentValue but then I get this error:
Cannot assign value of type 'String' to type 'NSTimeInterval' (aka 'Double')
I am kind of new to Xcode and errors so I don't really know how to solve this problem but I tried to delete the string part but that did not work.
#IBAction func time(sender: UISlider) {
var timeValue:String = String(Int(sender.value)) sound!.currentTime = timeValue
}
timeValue is a String and currentTime most likely is a NSTimeInterval. You can't assign string to NSTimeInterval. Use Double(sender.value) instead. Also, I would suggest that you do not combine so many operations on one line, especially if you are new to programming or Swift. If you get an error, you don't easily see if the problem was with conversion to String, to Int, if your sound variable was nil or what else is going on.

Swift: Trying to print an empty string – The process has been returned to the state before expression evaluation

I have a model with a column entry, which is a String. Sometimes entry has some text in it, sometimes it doesn't and it's an empty string.
When I try to print the string in console, I'm getting The process has been returned to the state before expression evaluation. (running e object.entry)
Not sure why it isn't just printing ""
Trying e object.entry! gives me error: operand of postfix '!' should have optional type; type is 'String'
Any ideas on how to fix this?
Empty String "" doesn't mean nil, and entry is not an optional type.
I think you are supposed to declare entry as "" when initialize the object
class object {
var entry = ""
//do anything you want next
}

How to detect illegal input in Swift string when converting to Double

Converting a string to a double in Swift is done as follows:
var string = "123.45"
string.bridgeToObjectiveC().doubleValue
If string is not a legal double value (ex. "a23e") the call to doubleValue will return 0.0. Since it is not returning nil how am I supposed to discriminate between the legal user input of 0.0 and an illegal input?
This is not a problem that is specific to Swift, With NSString in Objective-C, the doubleValue method returns 0.0 for invalid input (which is pretty horrible!). You are going to have to check the format of the string manually.
See the various options available to you here:
Check that a input to UITextField is numeric only
Here is my solution. Try to parse it and if it's not nil return the double value, otherwise handle it how you want.
var string = "2.1.2.1.2"
if let number = NSNumberFormatter().numberFromString(string) {
return number.doubleValue
} else {
...
}
Here is the solution I would use:
var string = NSString(string:"string")
var double = string.doubleValue
if double == 0.0 && string == "0.0"
{
//double is valid
}
else
{
//double is invalid
}
Firstly, I couldn't reassign the double value of the string to the original string variable, so I assigned it to a new one. If you can do this, assign the original string variable to a new variable before you convert it to a double, then use the new variable in place of string in the second if statement, and replace double with string the first statement.
The way this works is it converts the string to a double in a different variable, then it checks to see if the double equals 0.0 and if the original string equals 0.0 as a string. If they are both equal, the double is valid, as the user originally input 0.0, if not, the double is invalid. Put the code you want to use if the double is invalid in the else clause, such as an alert informing the user their input was invalid.