简体   繁体   English

使用prepareForSegue和使用Core Data传递数据之间有什么区别?

[英]What's the difference between passing data using prepareForSegue and using Core Data?

Is there anything different between the two? 两者之间有什么不同吗? I'm trying to figure out which one is better to use because they seem to do the same thing, or at least to me. 我试图弄清楚哪个更好用,因为它们似乎做同样的事情,或者至少对我来说。 I'm trying to pass data between multiple views. 我正在尝试在多个视图之间传递数据。

Core Data is an object database that usually has an SQL relational database as it's backing store. 核心数据是一个对象数据库,通常具有SQL关系数据库作为后备存储。 It is used for storing and manipulating complex data-sets, and presenting them to the user. 它用于存储和处理复杂的数据集,并将其呈现给用户。

PrepareForSegue is a method that gets called on a view controller before a new view controller is invoked from a segue. PrepareForSegue是一种在从Segue调用新视图控制器之前在视图控制器上调用的方法。 You can use prepareForSegue() as a way to pass data to the new view controller that is about to be displayed. 您可以使用prepareForSegue()作为将数据传递到将要显示的新视图控制器的方法。

The two things have very little in common. 这两件事没有什么共同点。

Since Core Data is a way to store app-wide persistent data, you can have multiple view controllers read and write data to a shared Core Data database, and in that way communicate information between view controllers, but that's about as far as the comparison goes. 由于Core Data是存储应用程序范围内的持久性数据的一种方式,因此您可以让多个View Controller将数据读写到共享的Core Data数据库中,并以这种方式在View Controller之间通信信息,但这仅是比较的结果。

To use an analogy, Core Data is a filing cabinet that everybody in an office has access to. 用类推,核心数据是一个文件柜,办公室中的每个人都可以访问。 If a clerk files some information in the filing cabinet, at any future time another employee can find that file and get the information, and so can anyone else in the company who has access to the cabinet. 如果业务员在文件柜中归档某些信息,则将来任何时候其他员工都可以找到该文件并获取信息,公司中有权访问该文件柜的其他人也可以找到该文件。 PrepareForSegue is a text message between 2 specific phone numbers. PrepareForSegue是2个特定电话号码之间的短信。 It transmits a burst of transient information, once and only once, between specific people. 它在特定的人之间一次且仅一次发送一连串的瞬态信息。

EDIT: 编辑:

There are other options for passing information between view controllers as well. 还有其他选项可以在视图控制器之间传递信息。

Check out this question/answer thread I created on the subject: 看看我在这个主题上创建的这个问题/答案主题:

How do you share data between view controllers and other objects in Swift? 如何在Swift中的视图控制器和其他对象之间共享数据?

If you want the data to persist then you must use SQLite3, Core Data or something like realm. 如果要保留数据,则必须使用SQLite3,Core Data或诸如realm之类的东西。

If you just want to pass the data around from one viewController to another then you would do it through something like prepareForSegue 如果你只是想从一个身边的viewController的数据传递到另一个,那么你会做它通过类似prepareForSegue

PrepareForSegue would be to pass (tempory) data between two viewControllers. PrepareForSegue将在两个viewController之间传递(临时)数据。 CoreData is a library to allow you to store data inside your app and re-use it whenever you want wherever you want. CoreData是一个库,可让您将数据存储在应用程序内部,并在需要时随时随地重复使用。

PrepareForSegue allows you to get a reference to the destination view so you can pass the variables you want. PrepareForSegue允许您获取对目标视图的引用,以便可以传递所需的变量。

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    let viewController = segue!.destinationViewController as ViewController
    //here you can access view variables as viewController.variable
}

CoreData instead allows you to persist data to a SQL Database, maybe not what you want. 相反,CoreData允许您将数据持久存储到SQL数据库,也许不是您想要的。

But beware, you can only pass data from de origin view to the destination one. 但是请注意,您只能将数据从原始视图传递到目标视图。

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

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