简体   繁体   English

iOS 模块化 - 导入主应用程序 Xcode 项目的 Xcode 项目 [框架] 是否需要方案?

[英]iOS Modularization - Is a scheme required for a Xcode project [framework] that's imported into the main app Xcode project?

I have been creating Internal (local) Swift Packages and then creating frameworks that import those packages as modules to help create service providing [public] classes and structs.我一直在创建内部(本地)Swift 包,然后创建将这些包作为模块导入的框架,以帮助创建提供服务的 [公共] 类和结构。

Said frameworks are then imported into the main app Xcode project and it's been working very well to manage / modularize a very large code base.然后将所述框架导入主应用程序 Xcode 项目,并且它在管理/模块化非常大的代码库方面工作得很好。

1. If I am creating an Xcode Project should it have its own scheme? 1.如果我正在创建一个 Xcode 项目,它应该有自己的方案吗?

2. Should the scheme container of the project being the main app project? 2.项目的scheme容器应该是主app项目吗?

3. Is there a better way to go about this or am I doing this in the most widely accepted way possible (industry standard, if you will)? 3.有没有更好的方法来 go 关于这个,或者我是否以最广泛接受的方式(行业标准,如果你愿意)这样做?

Schemes are target specific, so each target in your project needs to have its own scheme.方案是特定于目标的,因此项目中的每个目标都需要有自己的方案。

To compile your packages into your final solution, technically you'll only need the schemes for the targets of your projects, ie for the frameworks you're compiling.要将你的包编译成你的最终解决方案,从技术上讲,你只需要项目目标的方案,即你正在编译的框架。

That said, most likely your packages already contain some test targets, which would need their own schemes to run.也就是说,您的包很可能已经包含一些测试目标,这些目标需要自己的方案才能运行。 Though technically these schemes could be created in your main project, they should be created on the lowest level the targets can be build and tested.尽管从技术上讲,这些方案可以在您的主项目中创建,但它们应该在可以构建和测试目标的最低级别上创建。

SPM for Modularized code模块化代码的 SPM

I think you're on the right track.我认为你在正确的轨道上。 Though I think it's simpler than you may think it is.虽然我认为它比你想象的要简单。 There is no need to make frameworks to consume your swift packages before your app can consume them.在您的应用程序可以使用它们之前,无需创建框架来使用您的 swift 包。 I have a project with 12 local Swift Packages to modularize the code.我有一个包含 12 个本地 Swift 包的项目来模块化代码。 No frameworks.没有框架。 Only 1 scheme.只有1个方案。 No hassle.没有麻烦。

Baseline基线

Before I try to answer this question—since it's a bit involved—I want to start by laying a baseline that describes a very simple app with no dependencies for starters.在我尝试回答这个问题之前——因为它有点牵扯——我想首先建立一个基线,描述一个非常简单的应用程序,对初学者没有依赖关系。

Imagine you just created a project and your file structure looks a bit like this:想象一下你刚刚创建了一个项目,你的文件结构看起来有点像这样:

RootDirectory/
    MyProject.xcodeproj
    MyProject/
        MyProjectApp.swift
        ContentView.swift
        Info.plist
        Assets.xcassets/

Create a Package创建 Package

I generally stay away from creating my local swift packages within Xcode.我通常不会在 Xcode 中创建本地 swift 包。 I do it from the command line like so:我从命令行这样做:

# Travel to the project's root directory
cd path/to/RootDirectory

# Create a directory to host the new package
mkdir MyPackage

# Move to the new directory
cd MyPackage

# Create the new Swift Package
swift package init

After that is done, the directory structure should look like this:完成后,目录结构应如下所示:

RootDirectory/
    MyPackage/
        Package.swift
        Sources/
        Tests/
    MyProject.xcodeproj
    MyProject/
        MyProjectApp.swift
        ContentView.swift
        Info.plist
        Assets.xcassets/

My Package.swift file looks like this:我的 Package.swift 文件如下所示:

// swift-tools-version:5.6
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "Core",
    platforms: [.iOS(.v15), .macOS(.v12), .watchOS(.v8), .tvOS(.v15)],
    products: [
        .library(
            name: "Core",
            targets: ["Core"]),
    ],
    dependencies: [],
    targets: [
        .target(
            name: "Core",
            dependencies: []),
        .testTarget(
            name: "CoreTests",
            dependencies: ["Core"]),
    ]
)

Consume the Local Package消费本地 Package

After that is done, I would add the package to the App's Xcode Project like so:完成后,我会将 package 添加到应用程序的 Xcode 项目中,如下所示:

添加包 选择本地套餐

After you've added the package, you want to make sure it shows up in the Linked Libraries area of the App Target's Build Phases:添加 package 后,您需要确保它显示在 App Target 的 Build Phases 的 Linked Libraries 区域中:

链接库部分

Then, make sure your app target's scheme knows to include your packages for building and/or testing like so:然后,确保您的应用程序目标的方案知道包含用于构建和/或测试的包,如下所示:

应用方案

Result结果

You can do that over and over again.你可以一遍又一遍地这样做。 Your project should look something like this at the end:您的项目最后应该看起来像这样:

本地包列表

You can create a pod file for your own framework.您可以为自己的框架创建一个 pod 文件。 It's very simple and easy to use for other projects.它非常简单,易于用于其他项目。 And versioning also easy.而且版本控制也很容易。

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

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