CoreData 适用于我的模拟器,但不适用于使用 xCode 6 的 iOS 8 Swift 中的 iPhone
Posted
技术标签:
【中文标题】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 【发布时间】:2014-12-21 05:15:58 【问题描述】:当用断点返回我的代码时,我能够将我的错误专门缩小到一行,但以后可能会出现其他问题。这在我的模拟器上工作得很好,但是当我尝试使用我的手机时,它会在那条线上抛出一个错误。
我通过说“
这是我的代码
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
【问题讨论】:
它会抛出什么错误? 在您的设备上重新安装托管数据模型后,您是否对其进行了更新? @Bluehound 托管数据模型?请详细说明,因为我不知道那是什么。 您设置核心数据实体的 .xcdatamodeld 文件 核心数据出错可能导致该错误的常见情况是更新数据模型(解决方案删除手机上的应用) 【参考方案1】:“用于打开商店的模型与用于创建商店的模型不兼容”错误表示用于存储数据的模型已更改,并且您尚未编写适当的代码来处理更改。如果您在测试应用程序时在手机上遇到此错误,只需删除该应用程序并重新安装即可。如果您在模拟器中遇到这种情况,您可以重置模拟器的内容和设置。如果因为您的数据很重要而这些方法不合适,请参阅Core Data Model Versioning and Data Migration。
【讨论】:
以上是关于CoreData 适用于我的模拟器,但不适用于使用 xCode 6 的 iOS 8 Swift 中的 iPhone的主要内容,如果未能解决你的问题,请参考以下文章
Cloudkit 身份验证仅适用于我的 alt 帐户,不适用于 Dev 或 Tester 帐户