Swift Package Manager动态库

时间:2019-09-25 01:44:57

标签: swift xcode11 swift-package-manager

我最近将一堆框架转换为使用Swift Package Manager。我的Vue.config.productionTip = false; Vue.config.devtools=false; new Vue({ el: "#app", data: { longMode: false, columns: 7, tasks: [] }, created() { this.tasks = this.newTasks; }, watch: { newTasks(tasks) { console.log("watch: newTasks()-------------"); console.log(tasks); this.tasks = tasks; this.$forceUpdate(); } }, computed: { newTasks() { let tasks = null; if(this.longMode) { tasks = [ {start: 0, end: 3}, {start: 2, end: 9}, {start: 7, end: 13} ]; } else { tasks = [ {start: 0, end: 4}, {start: 2, end: 6} ]; } this.delay(2000).then(() => { console.log("return new tasks:"); console.log(tasks); return tasks; }) } }, methods: { toggle() { this.longMode = !this.longMode; this.columns = this.longMode ? 14 : 7; this.tasks = this.newTasks; console.log("clicked! longMode: " + this.longMode); }, delay(t) { return new Promise(function(resolve) { setTimeout(resolve, t) }); }, getLineStyle(index) { let rowIndex = 2; console.log("getLineStyle("+rowIndex+")"); console.log(this.$refs); if(this.tasks && this.tasks.length>0) { console.log(this.tasks); let task = this.tasks[rowIndex]; console.log(task); let start = task.start; let end = task.end; let startRef = 'cell'+start; let endRef = 'cell'+end; let startLeft = this.$refs[startRef][0].offsetLeft; let endLeft = this.$refs[endRef][0].offsetLeft; let taskWidth = (endLeft - startLeft) + 'px'; let styleObject = {left: startLeft, width: taskWidth}; return styleObject; } } } })看起来像这样:

body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}

#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}

table {
border-collapse: collapse;
table-layout: auto;
/*fixed;*/
border: 1px solid #012656;
}

.cell {
padding: 5px;
border: 1px solid #012656;
cursor: pointer;
}

td.cell:nth-child(7n-1),
td.cell:nth-child(7n) {
background: #eee;
}

.line {
position: absolute;
top: 4px;
height: 6px;
border-radius: 3px;
background: red;
overflow: hidden;
}

由于该库以前是一个框架,所以我想在我的应用程序中将其链接为动态库(<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <div id="app"> <table class="gantt-table"> <tr @click="toggle"> <td v-for="(cell, colIndex) in columns" :ref="'cell'+colIndex" :key="'col-'+colIndex" class="cell" > {{ colIndex+1 }} </td> </tr> <tr v-show="newTasks && newTasks.length > 0" v-for="(row, rowIndex) in newTasks" :key="'tr-'+rowIndex" > <!-- <div style="position: relative;"> <div class="line" :style="getLineStyle(rowIndex)"></div> </div> --> <td v-for="(cell, colIndex) in columns" :key="'row-'+rowIndex+'col-'+colIndex" class="cell" > <div>&nbsp&nbsp</div> </td> </tr> </table> </div>)。根据包装文档中的库产品定义,如果需要,我可以将库的Package.swift指定为// swift-tools-version:5.1 import PackageDescription let package = Package( name: "MDFToolbox", platforms: [ .macOS(.v10_13), .iOS(.v12), .tvOS(.v12), .watchOS(.v3) ], products: [ .library(name: "MDFToolbox", targets: ["MDFToolbox"]) ], dependencies: [ .package(url: "git@github.com:Swinject/Swinject.git", from: "2.7.0"), ], targets: [ .target(name: "MDFToolbox", dependencies: ["Swinject"]), ] )

  

库的可选类型,用于确定如何链接到库。将此参数保留为未指定状态,以便让Swift Package Manager在静态或动态链接之间进行选择(推荐)。如果您不同时支持两种链接类型,请对该参数使用.static或.dynamic。

如果我将其保留为.dylib,则在我在应用程序项目中链接此程序包时,Xcode默认会构建一个静态库,这不是我想要的。

如果我在库的Package.swift中将类型设置为type,则Xcode会构建一个.dynamic,但不会嵌入到应用程序中,从而导致链接器错误:

nil

我还看不到将.dynamic添加到复制文件构建阶段的简便方法...

那么建议SPM通过Xcode构建和链接动态库的推荐方法是什么?这是尚不支持的东西吗?

1 个答案:

答案 0 :(得分:0)

我知道了!

如果要创建框架库,则需要像这样在Package.swift中强制它成为一个库:

.library(name: "MDFToolbox", type: .dynamic, targets: ["MDFToolbox"])

完成此操作后,将在链接到库的项目的Xcode项目设置中看到一个embed选项。选择目标时,向下滚动到Frameworks, Libraries, and Embedded Content部分。您应该看到一个选项来嵌入动态库依赖项:

enter image description here