简体   繁体   English

如何在我的主要 Xcode 项目中添加引用类的 Swift package?

[英]How do I add a Swift package that references classes in my main Xcode project?

My main goal is to make a swift package with two states that are toggled on and off by a flag.我的主要目标是制作一个 swift package 具有两种状态,由一个标志打开和关闭。

One of the states enables code that references classes and variables defined in another project of mine that imports this package.其中一种状态启用引用在我的另一个项目中定义的类和变量的代码,该项目导入此 package。 These sections of code are my own personal version of the library with special functionality specific to my project.这些代码部分是我自己的库的个人版本,具有特定于我的项目的特殊功能。 The other state disables those code sections and enable code that allows the package to be used as library for the general public.另一个 state 禁用这些代码部分并启用允许 package 用作公众库的代码。

The way I've tried to do this is by using preprocessor macros to turn sections of code on and off.我尝试这样做的方法是使用预处理器宏来打开和关闭代码段。 The following flag is defined in a custom branch of my package that I use when I add this package into my other project:以下标志在我的 package 的自定义分支中定义,当我将此 package 添加到我的其他项目中时使用该分支:

(Package.swift file in my swift package) (我的 swift 包中的 Package.swift 文件)

swiftSettings: [
  .define("FULL_WEEKDAY_PICKER"),
]),

(code in my swift package) (我的 swift 包中的代码)

#if FULL_WEEKDAY_PICKER
// use code referenced in my other project
#else
// do other thing
#endif

This does not work however, and I get errors, because my package has no knowledge of the code in my other project.但是,这不起作用,并且出现错误,因为我的 package 不知道我的其他项目中的代码。 My first idea was to add it as local dependency to this package.我的第一个想法是将它作为本地依赖添加到这个 package 中。 My project has a git repository.我的项目有一个 git 存储库。

dependencies: [
    // Dependencies declare other packages that this package depends on.
    .package(path: "../HueCircadianSchedule"), // <--- name of my other project
],

.target(
    name: "DayOfWeekCollectionView",
    dependencies: ["Sol"], //<--- target of my other project

But even adding these settings crashes Xcode for me, so I don't think this is the right step to take.但即使添加这些设置也会让我崩溃 Xcode,所以我认为这不是正确的步骤。

Alternatively, I tried dragging and dropping my package into my other project.或者,我尝试将我的 package 拖放到我的其他项目中。 I don't get any errors in my package, but the project has no knowledge of the code in the package I just dropped in.我的 package 中没有任何错误,但该项目不知道我刚刚加入的 package 中的代码。

How do I reference code defined in my project in my package and then use that package in my project?如何在我的 package 中引用我的项目中定义的代码,然后在我的项目中使用该 package? Do I need a Package.swift in my project?我的项目中是否需要 Package.swift? Should I be taking a different approach to accomplish what I'm trying to do?我应该采取不同的方法来完成我想做的事情吗? You can look at my projects here:你可以在这里查看我的项目:

My project我的项目

My swift package我的 swift package

It sounds like your package is not a proper stand-alone package.听起来您的 package 不是合适的独立 package。 Re-design it to be a true stand-alone package, ie, either move all the required code from your other project into the package, or allow the package's user to extend the package's functionality somehow.将其重新设计为真正的独立 package,即,要么将所有需要的代码从其他项目移至 package,要么允许包的用户以某种方式扩展包的功能。

Basically instead of the package referencing external stuff directly, allow the external stuff to make itself known to the general services of the package.基本上不是 package 直接引用外部内容,而是让外部内容让 package 的一般服务知道自己。 For example, define a protocol in the package and the entity (data source, UI, delegate, or whatever it is) conforming to the protocol in your project.例如,在 package 中定义一个协议,并在项目中定义符合该协议的实体(数据源、UI、委托或其他任何东西)。 Then you supply that thing when you use the package, and the package only needs to know the interfaces defined in the protocol.然后你在使用package时提供那个东西,package只需要知道协议中定义的接口。

Or, alternatively, refactor the package in such a way that it doesn't need call any external thing, but rather the external thing calls the package.或者,或者,重构 package,使其不需要调用任何外部事物,而是外部事物调用 package。

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

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