In swift a variable must always contain a valid value. For value types (int, float, strings, structs, etc.) it means the variable must be initialized. For reference types (instance of classes) they must be initialized to a valid instance of a class and cannot be nil.
In swift a variable cannot be left uninitialized. But there are cases when it is allowed for a variable to be non initialized or initialized with nil. This is why the concept of optionals has been introduced. An optional variable can contain a valid value for its data type, or nil. An optional variable is declared by postfixing the question mark to the type, for instance: var x = Int?.
Suggested reading: Optionals in the Swift Programming Language book.
As for your problem, here:
let tempFloat:Float! = tempDict.objectForKey("amount") as? Float
you read a value from a dictionary, which can be nil if no value has been set for the amount key. That's why there is a cast as? Float. That casts to an optional type, which can either contain a valid Float type, or nil.
In the left side of the assignment let tempFloat:Float! you are stating that the right side is not nil (by using the exclamation mark), and that you can use tempFloat without unwrapping it.
If the dictionary contains a valid float for the amount key, then that's not a problem. But if the dictionary doesn't contain a value, what happens is that a nil is attempted to be converted to a Float when you try to use the tempFloat variable - which causes the exception.
The workaround looks like this:
let tempFloat = tempDict.objectForKey("amount") as? Float
if let unwrappedFloat = tempFloat {
globalDaily = globalDaily + unwrappedFloat
}
this makes sure that you use the variable (and do the addition) only if tempFloat contains a valid float value.
Floatthe number type is inferred from the fact you use0.0.