0

I am very new to Swift 2.0 and am pretty stuck here... I have created a random number generator based off a user set range for low and high limits. This function worked great with my test function when I could set the variables for low and high to Uint32 Once I tried to store the low/high range in NSUserDefaults as an integer. Now when I try to load the values set by the user for the low and high to use in my function below it gives me the error that it needs to be a UInt32 value.

The problem is.... I cannot seem to figure out how to save them in NSUser as that type. Or unwrap it as that type coming out of the UserDefaults.

My specific error message is... ERROR IS Binary Operator + cannot be applied to operands of type UInt32 and In

Thanks so much ahead of time for your help and time I truly appreciate it! You guys are awesome here!

Let me know if there is anything else you need from me :)

 //CREATE SAVE SETTINGS FOR LOW AND HIGH RANGE
 NSUserDefaults.standardUserDefaults().integerForKey("CatLowRange")                       
 NSUserDefaults.standardUserDefaults().integerForKey("CatHighRange")

//Variables to load in values from NSUserDefaults on load
var catLowRange             = 0     //Load In Low Range From NSUser
var catHighRange            = 40    //Load In High Range From NSUser

//LOAD IN VALUES TO VARIABLES FROM NSUSERDEFAULTS

catLowRange = NSUserDefaults.standardUserDefaults().integerForKey("CatLowRange")

catHighRange = NSUserDefaults.standardUserDefaults().integerForKey("CatHighRange")

**//My Function To Generate RandNumber...** 
*ERROR IS Binary Operator + cannot be applied to operands of type UInt32 and Int*
randomNumber = arc4random_uniform (catHighRange - catLowRange) + catLowRange

2 Answers2

1

For anyone wondering... Thanks to Adam we have an answer... Here is the code broken down how I used his suggestion to make this thing work :)

//WORKING METHOD!?
    //vars for use
    let catLowRange         : UInt32    = 0
    let catHighRange        : UInt32    = 40

    var randNumGenerated    : UInt32    = 0

    //SAVEPOINTS (STEP 1 - save the UInt32s as int for saving)
    NSUserDefaults.standardUserDefaults().setInteger(Int(catLowRange), forKey: "CatLowRange")
    NSUserDefaults.standardUserDefaults().setInteger(Int(catHighRange), forKey: "CatHighRange")

    //LOAD (STEP 2 - Take Int save as a UInt32 now)
    let catLowRangeLoad = UInt32(NSUserDefaults.standardUserDefaults().integerForKey("CatLowRange"))
    let catHighRangeLoad = UInt32(NSUserDefaults.standardUserDefaults().integerForKey("CatHighRange"))

    //FUNCTION TEST
    randNumGenerated = arc4random_uniform (catHighRangeLoad - catLowRangeLoad) + catLowRangeLoad

    //CHECK
    print ("catLowRange = \(catLowRange)")
    print ("catLowRangeLoad = \(catLowRangeLoad)")
    print ("catHighRange = \(catHighRange)")
    print ("catHighRangeLoad = \(catHighRangeLoad)")
    print ("randNumGen = \(randNumGenerated)")
0

Use this code (updated for swift 3):

    let number : UInt32 = 8

    UserDefaults.standard.set(NSNumber(value: number), forKey: "key")

    let number2 = (UserDefaults.standard.value(forKey: "key") as? NSNumber)?.uint32Value
Mousavian
  • 1,435
  • 1
  • 15
  • 23
Adam
  • 26,549
  • 8
  • 62
  • 79
  • Seems like this is a great idea! I will try to implement this method into my app as soon as I get a chance today. Thanks so much for the fast response and I will update you as soon as I give this a go! Pretty excited! – CakeGamesStudios Sep 15 '15 at 15:41
  • This worked like a charm! Fully working and amazing. Thanks for the insight. I REALLY Appreciate it. – CakeGamesStudios Sep 15 '15 at 20:12
  • this will not work on 32 bit devices when number is beyond 2^31 – Mousavian Sep 15 '15 at 22:08
  • @Mousavian the number is `UInt32` in the first place. It will never be larger than that. – Adam Sep 16 '15 at 04:13
  • Int range in **32bit devices** is -2^31 to 2^31-1 while UInt32 is 0 to 2^32. theoretically UInt32 can have larger values than Int – Mousavian Sep 16 '15 at 08:27
  • @Mousavian converting numerically between Int32 and UInt32 in either direction can result in numerical underflow/overflow, but bitwise the conversion always fits, ie 32 bits to 32 bits. – andrewz Feb 24 '17 at 15:09
  • @andrewz I updated answer to what it should be, You should use NSNumber in these scenarios as there is no implicit conversion. For swift 2 also you should use NSNumber directly – Mousavian Feb 25 '17 at 10:18