簡體   English   中英

CoreData在使用xCode 6的iOS 8 Swift中的我的模擬器上運行但在我的iPhone上不可用

[英]CoreData works on my simulator in but not on my iPhone in iOS 8 Swift using xCode 6

回到帶有斷點的代碼時,我可以將錯誤范圍縮小到一行,但是以后可能還會出現其他問題。 這在我的模擬器上工作正常,但是當我嘗試使用手機時,會在該行上引發錯誤。

我通過說“ <<<<<這是我的錯誤”來指定錯誤的行

所以這是我的代碼

import UIKit
import CoreData

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var startUpLabel: UILabel!
@IBOutlet weak var nameSubmitOutlet: UIButton!
@IBOutlet weak var characterName: UITextField!
@IBOutlet weak var enterGameOutlet: UIButton!
@IBOutlet weak var continueChangeLabelStartUpOutlet: UIButton!


var startUpScene = StartUpScene()
var index = 0
var name: String? = nil

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    startUpLabel.text = "[Heavy breathing]"

    self.characterName.delegate = self

    enterGameOutlet.hidden = true
    characterName.hidden = true
    nameSubmitOutlet.hidden = true

}

*code in here blah*

@IBAction func continueChangeLabelStartup() {


    if index < startUpScene.startUpLabelArray.count {

        startUpLabel.text = startUpScene.startUpLabelArray[index++]
        if index == 3 {
            characterName.hidden = false
            nameSubmitOutlet.hidden = false
            continueChangeLabelStartUpOutlet.hidden = true
        }

        if index == startUpScene.startUpLabelArray.count {//later on enter game will have its own method to hide everything except itself
            startUpLabel.hidden = true
            continueChangeLabelStartUpOutlet.hidden = true
            enterGameOutlet.hidden = false
        }
    }
}

@IBAction func nameSubmitButton() {
    name = characterName.text



    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate) //creates and instance of the appdelegate
    if let context: NSManagedObjectContext = appDel.managedObjectContext {//<<<<< THIS IS MY ERROR (i tried using an "if let" statement to unwrap the code that returned as an optional. Previously it didn't return as an optional so maybe that why I'm getting the error.The "bang"(!) was too risky
        var newUser = NSEntityDescription.insertNewObjectForEntityForName("UserInfo", inManagedObjectContext: context) as NSManagedObject // creates a "newUser" var and enters the data i want to save in the "userName" key under the UserInfo table i created in the .xcdatamold earlier

        newUser.setValue(name!, forKey: "userName") // sets the name in the .xcdatamold as if it were a dictionary

        context.save(nil)//saves the data
    }// creates an instance of the NSManagedObjectContext 



    characterName.hidden = true
    nameSubmitOutlet.hidden = true
    continueChangeLabelStartUpOutlet.hidden = false

    characterName.resignFirstResponder()

    startUpLabel.text = "Ah that's right. I'm \(name!)"
}
*Other code*

}

我將數據加載到此代碼中。 在首次使用該應用程序進行介紹之后,這就是游戲默認的位置。 所以我需要在這里重新加載。 同樣,它在我的模擬器上也可以正常工作,但是我無法在手機中得到足夠的代碼。

import UIKit
import CoreData

class GameMenuViewController: UIViewController {

@IBOutlet weak var loadGameOutlet: UIButton!
var name = ""

*code*

@IBAction func loadGameButton() {

    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate) //creates and instance of the appdelegate
    var context: NSManagedObjectContext = appDel.managedObjectContext! // creates an instance of the NSManagedObjectContext

    var request = NSFetchRequest(entityName: "UserInfo")//requests the userName entity which is the name
    request.returnsObjectsAsFaults = false //dont get the object back until i cast it as an object and use it

    var results: NSArray = context.executeFetchRequest(request, error: nil)!

    if results.count > 0 {

        var res = results[0] as NSManagedObject
        name = res.valueForKey("userName") as String
        println(name) *my name prints in the console here when i use the simulator*
    } else {
        println("Error")
    }
}

}

這是我的代碼錯誤

2014-12-20 23:19:54.773 Forgotten[1316:313403] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///var/mobile/Containers/Data/Application/7DCE915A-6A49-4B6D-9AF5-51D7CE5BEDDB/Documents/Forgotten.sqlite options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x15d71df0 {metadata={
NSPersistenceFrameworkVersion = 519;
NSStoreModelVersionHashes =     {
};
NSStoreModelVersionHashesVersion = 3;
NSStoreModelVersionIdentifiers =     (
    ""
);
NSStoreType = SQLite;
NSStoreUUID = "B9C87F2A-E940-4569-B98C-396127B9339A";
"_NSAutoVacuumLevel" = 2;

}, reason=The model used to open the store is incompatible with the one used to create the store} with userInfo dictionary {
metadata =     {
    NSPersistenceFrameworkVersion = 519;
    NSStoreModelVersionHashes =         {
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =         (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "B9C87F2A-E940-4569-B98C-396127B9339A";
    "_NSAutoVacuumLevel" = 2;

};
reason = "The model used to open the store is incompatible with the one used to create the store";
}
2014-12-20 23:19:54.788 Forgotten[1316:313403] Unresolved error Optional(Error Domain=YOUR_ERROR_DOMAIN Code=9999 "Failed to initialize the application's saved data" UserInfo=0x15ef5990 {NSLocalizedDescription=Failed to initialize the application's saved data, NSUnderlyingError=0x15d71e10 "The operation couldn’t be completed. (Cocoa error 134100.)", NSLocalizedFailureReason=There was an error creating or loading the application's saved data.}), Optional([NSLocalizedDescription: Failed to initialize the application's saved data, NSUnderlyingError: Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x15d71df0 {metadata={
NSPersistenceFrameworkVersion = 519;
NSStoreModelVersionHashes =     {
};
NSStoreModelVersionHashesVersion = 3;
NSStoreModelVersionIdentifiers =     (
    ""
);
NSStoreType = SQLite;
NSStoreUUID = "B9C87F2A-E940-4569-B98C-396127B9339A";
"_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store}, NSLocalizedFailureReason: There was an error creating or loading the application's saved data.])
(lldb) 

我不太擅長調試,但這是我所能做的。 這是我的結果

self    Forgotten.AppDelegate   0x15da1440  0x15da1440
coordinator NSPersistentStoreCoordinator?   nil None
error   NSError?    domain: nil - code: 9999    0x15ed82c0
failureReason   String  "There was an error creating or loading the application's saved data."  
url NSURL   "file:///var/mobile/Containers/Data/Application/7DCE915A-6A49-4B6D-9AF5-51D7CE5BEDDB/Documents/Forgotten.sqlite"    0x15ece010
dict    NSMutableDictionary 3 key/value pairs   0x15e89ac0

錯誤“用於打開存儲的模型與用於創建存儲的模型不兼容”錯誤表明用於存儲數據的模型已更改,並且您尚未編寫適當的代碼來處理更改。 如果您在測試應用程序時在手機上遇到此錯誤,只需刪除該應用程序並重新安裝即可。 如果您在模擬器中遇到此問題,則可以重置模擬器的內容和設置。 如果這些方法由於您的數據很重要而不合適,請參閱“ 核心數據模型版本控制和數據遷移”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM