SPM (Swift Package Manager) development and common issues

How to use SPM will not go into details. Its advantage is that ancient products such as Cocoapods are hard to come by, and the most important thing is that it can be binarized and has no intrusion into the xcproj project. Except for the network, it is simply a project library developed for the team. It's time to abandon the complicated and low-level cocoapods.

One: How to make it?

Here the use shall prevail, and the gui operation shall be created:

1. In the top menu of Xcode, select the operation as shown in the figure below
insert image description here

2. Fill in or change your own library name, here is "MyLibrary"
insert image description here

3. This creates a shell SPM. The most important part is the circled part in the picture.
Package.swift is a file describing the management of SPM, and it is also the basis for Xcode to recognize it after it is placed in a remote warehouse such as github. Source is where the source code is stored. Now it is a structure by default, and we can put the code we want to make into the library (don't forget the permission modifiers such as open public). In addition, there are readme.md and Tests folders. Since few people in China pay attention to these files, they are ignored here (delete the test code in the Tests folder, otherwise your own code will not compile).

insert image description here

Among them, Package.swift is important as the description and configuration file of the library. Here we need to understand a concept, that is, an spm does not only correspond to one library, but can correspond to multiple ones.

package = spm
library = 某个库

So it is not appropriate to say that spm = a collection of multiple libraries. Of course, it is no problem and very common for a spm to only correspond to one executable library.

import PackageDescription

let package = Package(
    name: "MyKit", // 库名称
    products: [
   		 // spm生成的可执行库(下面的target)和文件,一个spm可对应多个(例如一个RxSwift就对应多个可执行库)
        .library(
            name: "MyKit", // 这俩一般默认相同即可
            targets: ["MyKit"]), // 这俩一般默认相同即可
    ],
    // 此spm库依赖的其它spm库地址,如没有依赖,dependencies可无
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        .target(
            name: "MyKit", // 可执行库名称,也即在Source文件夹下的代码文件夹名称
            dependencies: []   // 此可执行库依赖的其它库的名称,如没有依赖。可无        
            exclude: ["Info.plist"], // 此库的权限描述plist清单,需要的权限等等
            resources: [
                .process("XXX.bundle") // 此库的资源文件路径
            ]), // 此库的资源文件
        .testTarget( // 此库的测试文件,如没有测试用例,可以删除
            name: "MyKitTests",
            dependencies: ["MyKit"]),
    ]
)

Two: How to publish?

If you are in a foreign country, you can directly operate in Xcode without network restrictions and publish it. However, due to network restrictions in China, you need to take other methods to deal with it.

1. First create a remote warehouse on github.

2. Submit the created spm file and push it to the remote end.

// 1.提交创建的spm文件或者更改的文件
git commit -m "first commit"
// 2.切换到主分支上
git branch -m main
// 3.设置远端仓库
git remote add origin https://github.com/xxx/GithubTest.git
// 4.推到远端仓库,并输入密码和token
git push -u origin main

During the push period, it is necessary to surf the Internet scientifically, otherwise the push will not be successful.

What if there is no tool for scientific Internet access? In fact, you don't need to put it on github. Since github cannot be accessed, gitlab or gitee can always be accessed normally, which is also a solution.

So what if you want to use xcode to operate? The xcode network is not a system proxy, so it is useless for you to use tools such as clash to set a global proxy, but you can set an access proxy specifically for Xcode, so that the access update is normal.

at last

spm can also introduce oc / c / c++, as for binary or static library, we can discuss together, there are few online materials in this regard

Guess you like

Origin blog.csdn.net/kicinio/article/details/132135746