Ios 保存到coredata的多个Swift值
我正在制作一个包含一些常量的游戏:Ios 保存到coredata的多个Swift值,ios,swift,core-data,Ios,Swift,Core Data,我正在制作一个包含一些常量的游戏: struct moneyConstants { static var tapValue = NSInteger() static var tapMultiplier = NSInteger() static var moneyPerSecond = NSInteger() static var money = NSInteger() } 我正在使用CoreData在应用程序关闭后保存这些值。我想知道您是否能够同时保存多个值。假
struct moneyConstants {
static var tapValue = NSInteger()
static var tapMultiplier = NSInteger()
static var moneyPerSecond = NSInteger()
static var money = NSInteger()
}
我正在使用CoreData在应用程序关闭后保存这些值。我想知道您是否能够同时保存多个值。假设我保存某些内容的代码是:
让appDelegate=UIApplication.sharedApplication()。将委托为!AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("Constants", inManagedObjectContext:managedContext)
let moneyS = NSManagedObject(entity: entity!, insertIntoManagedObjectContext:managedContext)
moneyS.setValue(moneyConstants.money, forKey: "moneySave")
do {
try managedContext.save()
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
如果我想保存第二个值,如:
let moneyPerSecondS = NSManagedObject(entity:entity!, insertIntoManagedObjectContext:managedContext)
moneyPerSecondS.setValue(moneyConstants.moneyPerSecond, forKey: "money")
我可以将这些行放在do{
部分之前吗?或者我必须将其放在do{
部分之后,然后编写另一个do{
部分吗?当然可以(如果在模型中声明了属性):
所有不抛出的东西都不需要放在do范围内。我想这是不可能的。但不能保证你总是可以这样做,但是。(moneyS.setValue(moneyConstants.moneyPerSecond,forKey:“money”))不相关,为什么要使用
NSInteger()
而不是本机Int
例如juststatic var tapValue=0
?结构和类名应该以大写字母开头。@vadian我使用了NSInteger()因为如果我不这样做,那么我就无法将值保存到coredata中。或者我可以吗?我没有太多使用coredata。然后使用Int32
:static var tapValue:Int32=0
let moneyS = NSManagedObject(entity: entity!, insertIntoManagedObjectContext:managedContext)
moneyS.setValue(moneyConstants.money, forKey: "moneySave")
moneyS.setValue(moneyConstants.moneyPerSecond, forKey: "money")