你怎么知道什么时候在swift中浇铸一个项目?

时间:2022-11-03 21:28:10

I see it done all the time but don't know how other coders know when to cast an item. Here is an example that recently had me wondering how the coder knew to cast the item:

我看到它一直在做,但不知道其他程序员如何知道什么时候投项目。最近有一个例子让我想知道编码者是如何知道投出这个项目的:

let item = NSEntityDescription.insertNewObjectForEntityForName("Item", inManagedObjectContext: ad.managedObjectContext) as! Item

1 个解决方案

#1


1  

The insertNewObject(forEntityName:into:) API that you're describing returns a NSManagedObject, which is the way new managed objects are created, configured and returned to you to make use of.

您所描述的insertNewObject(forEntityName:into:) API返回一个NSManagedObject,这是创建、配置和返回新托管对象的方式。

In CoreData, all NSManagedObjects saved are actually subclasses of the NSManagedObject base class, so if you want to let item = from that call, you'll need to cast it to the actual subclass type it's supposed to be.

在CoreData中,所有保存的NSManagedObjects实际上都是NSManagedObject基类的子类,所以如果你想让item =从那个调用中,你需要将它转换为它应该是的实际子类类型。

Makes sense?

有道理吗?

#1


1  

The insertNewObject(forEntityName:into:) API that you're describing returns a NSManagedObject, which is the way new managed objects are created, configured and returned to you to make use of.

您所描述的insertNewObject(forEntityName:into:) API返回一个NSManagedObject,这是创建、配置和返回新托管对象的方式。

In CoreData, all NSManagedObjects saved are actually subclasses of the NSManagedObject base class, so if you want to let item = from that call, you'll need to cast it to the actual subclass type it's supposed to be.

在CoreData中,所有保存的NSManagedObjects实际上都是NSManagedObject基类的子类,所以如果你想让item =从那个调用中,你需要将它转换为它应该是的实际子类类型。

Makes sense?

有道理吗?