Swift:IOS7设备上的核心数据 - >第二个实体上的entityForName为nil [英] Swift: Core Data on IOS7 device -> entityForName on second entity is nil

查看:402
本文介绍了Swift:IOS7设备上的核心数据 - >第二个实体上的entityForName为nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用Xcode 6 beta 2在一个iPhone项目上测试了swift。
该项目使用核心数据访问数据模型中的2个实体(用户/合同)。在IOS模拟器内部一切正常。


问题:当我构建IOS7的应用程序,并测试在我的iPhone5s(运行IOS 7.1.1)的应用程序只能保存数据到第一实体(在核心数据模型=用户中定义)。对于所有其他实体,NSEntityDescription.entityForName(...)是nil。在managedObjectModel(println(managedObjectModel))中包括所有实体。似乎数据模型不正确包括在IOS7上的SQL数据库?有人有解决方案/想法吗?感谢:)

  var myAppDel:AppDelegate = UIApplication.sharedApplication()。委托为AppDelegate 
var myContext:NSManagedObjectContext = myAppDel.managedObjectContext

println(NSEntityDescription.entityForName(User,inManagedObjectContext:myContext))
println(NSEntityDescription.entityForName(Contract,inManagedObjectContext:myContext))

var newContract:AnyObject = NSEntityDescription.insertNewObjectForEntityForName(Contract,inManagedObjectContext:myContext)

newContract.setValue(+ txtContract.text,forKey:contractName)
myContext.save(nil)

var newUser = NSEntityDescription.insertNewObjectForEntityForName(User,inManagedObjectContext:myContext)

newUser.setValue(+ txtUsername.text,forKey: userName)
newUser.setValue(+ txtPassword.text,forKey:userPass)
newUser.setValue(newContract,forKey:contracts)//保存关系

myContext.save(nil)

错误:
由于未捕获异常而终止应用程序'NSInternalInconsistencyException',reason:'+ entityForName:找不到此模型中名为Contract的实体。

解决方案

似乎是一个bug现在。如本文所述: https://devforums.apple.com/message/996259#996259 。 p>

此错误的解决方法是使用NSString,而不是传递String作为实体名称:

  let myEntity:NSString =EntityName
var fetchRequest = NSFetchRequest(entityName:myEntity)



i've tested swift on a little iPhone project using Xcode 6 beta 2. The project uses core data to access 2 entities (User/Contract) in a data model. Inside the IOS Simulator everything works fine.

Problem: When i build the app for IOS7 and test the app on my iPhone5s (running IOS 7.1.1) the program can only save data into the first entity (as defined in core data model = User). For all other entities the NSEntityDescription.entityForName(...) is "nil". In managedObjectModel (println(managedObjectModel)) all entities are included. Seems like the data model is not correct included into the SQL-Database on IOS7?! Does anyone have a solution/idea? Thanks :-)

    var myAppDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    var myContext:NSManagedObjectContext = myAppDel.managedObjectContext

    println(NSEntityDescription.entityForName("User", inManagedObjectContext: myContext))
    println(NSEntityDescription.entityForName("Contract", inManagedObjectContext: myContext)) 

    var newContract:AnyObject = NSEntityDescription.insertNewObjectForEntityForName("Contract", inManagedObjectContext: myContext)

    newContract.setValue("" + txtContract.text, forKey: "contractName")
    myContext.save(nil)

    var newUser = NSEntityDescription.insertNewObjectForEntityForName("User", inManagedObjectContext: myContext)

    newUser.setValue("" + txtUsername.text, forKey: "userName")
    newUser.setValue("" + txtPassword.text, forKey: "userPass")
    newUser.setValue(newContract, forKey: "contracts") // Save Relationship

    myContext.save(nil)

Error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an entity named 'Contract' in this model.'

解决方案

It seems to be a bug for now. As explained on this post: https://devforums.apple.com/message/996259#996259.

The workaround for this bug is to use a NSString instead of passing a String as the entity name:

    let myEntity: NSString = "EntityName"
    var fetchRequest = NSFetchRequest(entityName: myEntity)

这篇关于Swift:IOS7设备上的核心数据 - >第二个实体上的entityForName为nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆