gpt4 book ai didi

swift - 将 UInt32 值保存到 NSUserDefaults (Swift 2.0) 错误

转载 作者:行者123 更新时间:2023-11-30 10:11:44 25 4
gpt4 key购买 nike

我对 Swift 2.0 很陌生,而且很困在这里......我根据用户设置的下限和上限范围创建了一个随机数生成器。当我可以将 low 和 high 的变量设置为 Uint32 时,此函数非常适合我的测试函数有一次我尝试将 NSUserDefaults 中的低/高范围存储为整数。现在,当我尝试加载用户设置的低值和高值以在下面的函数中使用时,它会给出错误,它需要是 UInt32 值。

问题是......我似乎无法弄清楚如何将它们保存在 NSUser 中作为该类型。或者将其解开为来自 UserDefaults 的类型。

我的具体错误消息是...错误 IS 二元运算符 + 不能应用于 UInt32 和 In 类型的操作数

非常感谢您的帮助和时间,我真的很感激!你们在这里太棒了!

如果您还需要我提供任何其他信息,请告诉我:)

 //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

最佳答案

对于任何想知道的人...感谢 Adam,我们有了答案...这是代码,详细说明了我如何使用他的建议来使这件事发挥作用:)

//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)")

关于swift - 将 UInt32 值保存到 NSUserDefaults (Swift 2.0) 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32589747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com