Xcode执行“类定义”后如何使用生成的类?核心数据? [英] How to use the generated classes after Xcode did "Class Definition" for Core Data?

查看:75
本文介绍了Xcode执行“类定义”后如何使用生成的类?核心数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xcode 11.3.1,并且在新项目中选择类定义作为实体。

I'm using Xcode 11.3.1 and I choose Class Definition for example Entity in a new project.

我似乎无法引用生成的类。即使在我按下build

I can't seem to reference the generated class. Even after I press build

let x = Project()
Use of unresolved identifier 'Project'


推荐答案

无法复制。我直接从模板中使用核心数据创建了一个新的香草Single View应用程序,并像您一样创建了一个实体:

Can't reproduce. I created a new vanilla Single View app with Core Data direct from the template and made one Entity just like yours:

然后我在视图控制器的 viewDidLoad 中添加了一行代码;它可以正常编译:

Then I added one line of code to the view controller's viewDidLoad; it compiles just fine:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let _ = Project()
    }

}

在像您这样的情况下,最好考虑如何。代码生成从字面上生成代码。您可以通过按住Control键并单击 Project 一词并选择跳转到定义来查看该代码:

In a situation like yours, it is good to think about how this works. The code generation literally generates code. You can see that code by Control-clicking on the word Project and choosing Jump To Definition:

//  Project+CoreDataClass.swift
//  
//
//  This file was automatically generated and should not be edited.
//

import Foundation
import CoreData

@objc(Project)
public class Project: NSManagedObject {

}

如果看不到类似内容,请退出Xcode,删除其中的内容

If you don't see something like that, quit Xcode, delete the contents of the DerivedData folder, open the project again, wait for indexing to finish, and try again.

如果 still 不起作用,则重新打开项目,等待索引完成,然后重试。在模型编辑器中将Codegen切换为Manual / None并手动生成文件:

If it still doesn't work, then switch Codegen to Manual/None in the model editor and generate the file manually:

浏览对话框后,您将直接在项目导航器中看到生成的文件:

After working your way through the dialogs, you'll see the generated files directly right in your project navigator:

t您的项目一定会编译!

At that point your project will definitely compile!

这篇关于Xcode执行“类定义”后如何使用生成的类?核心数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆