Andy Lebowitz Andy Lebowitz - 2 years ago 96
Swift Question

Error with a Double Printing (Swift)

Everytime I print percent1, it says 0.0. any thoughts?

print("right1: \(categ1)")
print("total1: \(any1)")
percent1 = 100 * Double(categ1 / any1)
percent2 = 100 * Double(categ2 / any2)
percent3 = 100 * Double(categ3 / any3)
percent4 = 100 * Double(categ4 / any4)

By the way, when it comes out as zero, categ1 was 111 and any1 printed as 136. (But percent1 comes out as zero with anything).

Answer Source

There is a fantastical thing named NSUserDefaults. It is a dictionary that can be accessed at an time within the app. To set an object:

NSUserDefaults.standardUserDefaults().setObject(YourObject, forKey: "key")

Then to access it somewhere else:


Keep in mind that you would have to cast the object in the second snippet, because the returned object is of type AnyObject.

Hope this helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download