Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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
Swift 在保存另一页时将数字存储在重置的核心数据中_Swift_Core Data_Numbers_Reset - Fatal编程技术网

Swift 在保存另一页时将数字存储在重置的核心数据中

Swift 在保存另一页时将数字存储在重置的核心数据中,swift,core-data,numbers,reset,Swift,Core Data,Numbers,Reset,我将数字存储在用户添加的核心数据中,这样做可以很好地保存数据,即使当我翻开它保存的页面时也可以。但当我在另一个页面上保存数据,然后返回时,它会将所有数字重置为0 下面是我的代码 func saveStats() { storeTranscription18CW(pageText: self, textFileUrlString: "") } 而且 func storeTranscription18CW (pageText: Any, textFileUrlStri

我将数字存储在用户添加的核心数据中,这样做可以很好地保存数据,即使当我翻开它保存的页面时也可以。但当我在另一个页面上保存数据,然后返回时,它会将所有数字重置为0

下面是我的代码

func saveStats() {


            storeTranscription18CW(pageText: self, textFileUrlString: "") 
}
而且

func storeTranscription18CW (pageText: Any, textFileUrlString: String) {

    let context = getContext()

    //retrieve the entity that we just created
    let entity =  NSEntityDescription.entity(forEntityName: "TextInputs", in: context)

    let CWConvert = Double(CWeight.text!)

    let transc = NSManagedObject(entity: entity!, insertInto: context)
        transc.setValue(CWConvert, forKey: "cWeight")


    //save the object
    do {
        try context.save()
        print("saved!")
    } catch let error as NSError  {
        print("Could not save \(error), \(error.userInfo)")
    } catch {

    }
}
打电话

func getTranscriptions18CW () {
    //create a fetch request, telling it about the entity
    let fetchRequest: NSFetchRequest<TextInputs> = TextInputs.fetchRequest()
    do {
        //go get the results
        let searchResults18CW = try getContext().fetch(fetchRequest)

            for trans in searchResults18CW as [NSManagedObject] {
                if let result = trans.value(forKey: "cWeight") {
                    if let str = result as? String {
                        CWeight.text = str
                    }else {
                        CWeight?.text = "\(result)"
                    }
                }
            }
            //get the Key Value pairs (although there may be a better
    }catch {
        print("Error with request: \(error)")
    }
}
这是其他字段的函数,当我在这些字段上按save时,CWeight.text将重置为0

 func savePressed() {
    if indexPageSum == 22 {
        storeTranscription(pageText: self, textFileUrlString: "")
            saveButtonP22.isUserInteractionEnabled = false
            saveButtonP22.alpha = 0.4

    }
而且

  func storeTranscription (pageText: Any, textFileUrlString: String) {
    let context = getContext()

    //retrieve the entity that we just created
    let entity =  NSEntityDescription.entity(forEntityName: "TextInputs", in: context)

    let transc = NSManagedObject(entity: entity!, insertInto: context)

    //        set the entity values
    if indexPageSum == 22 {
        transc.setValue(page22TextView.text, forKey: "page22")

    }

    //save the object
    do {
        try context.save()
        print("saved!")
    } catch let error as NSError  {
        print("Could not save \(error), \(error.userInfo)")
    } catch {

    }
}
打电话给

func getTranscriptions () {
    //create a fetch request, telling it about the entity

    let fetchRequest: NSFetchRequest<TextInputs> = TextInputs.fetchRequest()

    do {
        //go get the results
        let searchResults = try getContext().fetch(fetchRequest)

            for trans in searchResults as [NSManagedObject] {

                if indexPageSum == 22{
                    let result22 = trans.value(forKey: "page22")
                    if result22 != nil {
                        page22TextView?.text = result22! as! String
                    }
                }
func getTransscriptions(){
//创建一个获取请求,告诉它有关实体的信息
let fetchRequest:NSFetchRequest=TextInputs.fetchRequest()
做{
//去看看结果
让searchResults=try getContext().fetch(fetchRequest)
用于将搜索结果转换为[NSManagedObject]{
如果indexPageSum==22{
让结果22=转换值(forKey:“第22页”)
如果结果22!=nil{
page22TextView?.text=result22!as!字符串
}
}

希望这是有意义的,您可以提供帮助。

请显示
getContext()
。Updated@shallowthough这仍然没有帮助。我现在可以说
DataController()
。我问的原因是,我认为您可能由于某种原因使用了不同的上下文,但这些上下文无法正确同步。其他请确保
Double(CWeight.text!)
不返回
nil
。是的,这很奇怪,因为当我编辑另一个时,其他的没有改变。CWeight.text没有返回nil它实际上保存了它,即使在我关闭应用程序时,它仍然保存了它。当我在另一个页面上编辑textView并保存它时,它只会更改为0
func getTranscriptions () {
    //create a fetch request, telling it about the entity

    let fetchRequest: NSFetchRequest<TextInputs> = TextInputs.fetchRequest()

    do {
        //go get the results
        let searchResults = try getContext().fetch(fetchRequest)

            for trans in searchResults as [NSManagedObject] {

                if indexPageSum == 22{
                    let result22 = trans.value(forKey: "page22")
                    if result22 != nil {
                        page22TextView?.text = result22! as! String
                    }
                }