[英]Simple Fetch in Swift 3 Core Data
I've been trying to implement a simple core data functionality within my app so that it shows a tutorial (a separate view controller with a scroll page) only the first time the app loads, and bypasses it every other time. 我一直在尝试在我的应用程序中实现一个简单的核心数据功能,以便仅在应用程序首次加载时才显示一个教程(带有滚动页面的单独的视图控制器),而每隔一次便绕开它。 I started with syntax from CoreData tutorials for Swift 2, but then had to tweak based on Swift 3's auto corrections and other tutorials that helped me overcome the errors i found along the way (such as SIGBRT).
我从Swift 2的CoreData教程的语法开始,但是后来不得不基于Swift 3的自动更正和其他教程进行调整,这些教程帮助我克服了在此过程中发现的错误(例如SIGBRT)。 The current problem I'm having is a EXC_BAD_INSTRUCTION error, which I need help fixing.
我目前遇到的问题是EXC_BAD_INSTRUCTION错误,我需要帮助修复。
I've got a scrollViewController with the following code: 我有一个带有以下代码的scrollViewController:
class ScrollViewController: UIViewController, NSFetchedResultsControllerDelegate {
@IBOutlet var mainScrollView: UIScrollView!
@IBOutlet weak var endTutorial: UIButton!
var imageArray = [UIImage]()
class Person: NSManagedObject {
@NSManaged var tutorialstatus: String?
}
func seed() {
let moc = DataController().managedObjectContext
let entityDes = NSEntityDescription.entity(forEntityName: "Person", in: moc)
let entity = Person(entity: entityDes!, insertInto: moc)
entity.tutorialstatus="test value"
do {
try moc.save()
} catch {
fatalError("Failure to save context: \(error)")
}
}
func fetch() {
let moc = DataController().managedObjectContext
let personFetch = NSFetchRequest<Person>(entityName: "Person")
do {
let fetchedPerson = try moc.fetch(personFetch)
print(fetchedPerson.first!.tutorialstatus)
} catch {
fatalError("Failed to fetch person: \(error)")
}
}
At the let fetchedPerson = try moc.fetch(personFetch)
line, when I try to compile, I see the error EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0) 在
let fetchedPerson = try moc.fetch(personFetch)
行中,当我尝试编译时,我看到错误EXC_BAD_INSTRUCTION(代码= EXC_1386_INVOP,子代码= 0x0)
I am using Xcode 8, Beta 8. I have a swift file called 'CoreDataStuf.xcdatamodeld which has an entity named "Person" with an attribute called tutorialstatus, which is a string. 我正在使用Xcode 8,Beta8。我有一个名为'CoreDataStuf.xcdatamodeld的快速文件,该文件具有一个名为“ Person”的实体,其属性为tutorialstatus,该属性是一个字符串。 I've also given that entity a class of Person.
我还给该实体提供了一类人。 I also have a DataController.swift file where i refer to the URL CoreDataStuf.
我也有一个DataController.swift文件,其中引用了URL CoreDataStuf。
这是有效的:
let fetchedPerson = try moc.fetch(personFetch as! NSFetchRequest<NSFetchRequestResult>)
try doing it in moc context like this 尝试在这样的Moc环境中进行操作
@IBOutlet weak var inValue: UITextField!
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
@IBAction func goBtn(_ sender: UIButton) {
var inVal = Int(inValue.text!)!
for i in inVal...inVal + 10 {
context.perform {
let nums = NSEntityDescription.insertNewObject(forEntityName: "Numbers", into: self.context) as! Numbers
let request:NSFetchRequest<Numbers> = Numbers.fetchRequest()
request.predicate = NSPredicate(format: "nums == %d", i)
do { let numbers = try? request.execute()
if (numbers?.count)! > 0 {
print ("Value \(i) alreayd existis")
} else {
nums.nums = Decimal(i) as NSDecimalNumber?
}
} catch let error {
print ("Error while fetching \(error)")
}
}
do {
try self.context.save()
print ("Value stored in DB \(i)")
} catch let error {
print ("Error while saving \(error)")
}
}
}
尝试:
let personFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Person")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.