Andy Lebowitz Andy Lebowitz - 7 months ago 16
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

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:

NSUserDefaults.standardUserDefaults().objectForKey("key")

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!

Comments