Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 保存到coredata的多个Swift值_Ios_Swift_Core Data - Fatal编程技术网

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
例如just
static 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")