简体   繁体   English

在试图保存到核心数据的 try catch 中缺少 return 语句

[英]missing return statement in a try catch trying to save to core data

In my swift code below the goal is to use a helper method to save strings to core data.在我下面的 swift 代码中,目标是使用辅助方法将字符串保存到核心数据。 I am attempting to use a try and catch system.我正在尝试使用 try and catch 系统。 I am getting a error after the catch segment saying Missing return in instance method expected to return '[Info]?'在 catch 段之后我收到一个错误,说Missing return in instance method expected to return '[Info]?' . . I don't know what to put as the return statement我不知道应该把什么作为退货声明

import UIKit;import CoreData

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        DataBaseHelper.shareInstance.saveText("Jessica")
        
        
        DataBaseHelper.shareInstance.saveText("ashley")
    }


}

class DataBaseHelper {
    
    private class func getContext() -> NSManagedObjectContext {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
    
        return appDelegate.persistentContainer.viewContext
    }
    
    
    static let shareInstance = DataBaseHelper()
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    
    func saveText(_ string: String) -> [Info]? {
        let imageInstance = Info(context: context)
        imageInstance.txt = string
        do {
            try context.save()
            
            
        } catch{
       
        }
        
    }
  
    

class func fetchObject() -> [Info]?
{   
    let context = getContext()
    var user : [Info]? = nil
    do {
        user = try context.fetch(Info.fetchRequest())
        
        return user
        
    } catch {
        return user
    }
    
}

    
   
}

Change the definition of the saveText function to this.将 saveText function 的定义更改为此。

 func saveText(_ string: String) {
    let imageInstance = Info(context: context)
    imageInstance.txt = string
    do {
        try context.save()
    } catch let err {
        print(err.localizedDescription)
    }
    
}

Since you don't need anything to return from that function, you can remove the return type由于您不需要从该 function 返回任何内容,因此您可以删除返回类型

Alternatively, if you do want a return type for future use, make it like this so that it returns nil in the case of an error或者,如果您确实想要一个返回类型以供将来使用,请将其设置为这样,以便在出现错误时返回 nil

func saveText(_ string: String) -> Info? {
    let imageInstance = Info(context: context)
    imageInstance.txt = string
    do {
        try context.save()
        return imageInstance
    } catch{
        return nil
    }
    
}

I'd recommend not doing this since you are creating an object from the context already, this means that even though there was an error while saving the context, the working copy could have the data you created.我建议不要这样做,因为您已经从上下文创建了一个 object,这意味着即使在保存上下文时出现错误,工作副本也可能包含您创建的数据。 This creates unnecessary problems, as you would see the data present, but the data won't be saved.这会产生不必要的问题,因为您会看到数据存在,但不会保存数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM