如何在swift中使用其他库?

时间:2016-01-09 16:07:13

标签: swift swift2

我正在快速学习,主要是从命令行学习。我不想创建ios / mac应用程序。只是想学习语言,可能会创建一些脚本。

在此上下文中,如何导入第三方库?说SwiftJSON。给出了在Xcode中导入的说明。但我不想使用Xcode。我有其他方法可以使用其他人开发的代码吗?

(我在Mac上开发)

2 个答案:

答案 0 :(得分:1)

Swift Package Manager是最好的方法。但是,SwiftJSON似乎没有定义Package.swift文件,这是新的。 [添加它很容易]。

在您的代码中,您定义了一个Package.swift文件,该文件表达了对SwiftJSON的依赖:

import PackageDescription

let package = Package (
  name: "MyPackage",
  dependencies: [
    .Package (url: "https://github.com/<swiftjson>", majorVersion: 0),
  ]
)

然后在您的代码中,只需import SwiftJSON,将您的'脚本代码'放在main.swift中,然后使用以下代码构建:

swift build

然后,您将在.build / debug / MyPackage

中拥有一个可执行文件

有关示例,请参阅https://github.com/apple/example-package-dealer

答案 1 :(得分:-1)

swift [-help] [input-filename [program-arguments]]

   swiftc [-emit-object|-emit-assembly|-emit-library]
     [-help]
     -o output-file
     input-filenames

   The full list of supported options is available via "swift -help" or
   "swiftc -help".

swiftc -help

OVERVIEW: Swift compiler

USAGE: swiftc [options] <inputs>

MODES:
  -dump-ast        Parse and type-check input file(s) and dump AST(s)
  -dump-parse      Parse input file(s) and dump AST(s)
  -dump-type-refinement-contexts
                   Type-check input file(s) and dump type refinement contexts(s)
  -emit-assembly   Emit assembly file(s) (-S)
  -emit-bc         Emit LLVM BC file(s)
  -emit-executable Emit a linked executable
  -emit-ir         Emit LLVM IR file(s)
  -emit-library    Emit a linked library
  -emit-object     Emit object file(s) (-c)
  -emit-sibgen     Emit serialized AST + raw SIL file(s)
  -emit-sib        Emit serialized AST + canonical SIL file(s)
  -emit-silgen     Emit raw SIL file(s)
  -emit-sil        Emit canonical SIL file(s)
  -parse           Parse input file(s)
  -print-ast       Parse and type-check input file(s) and pretty print AST(s)

OPTIONS:
  -application-extension  Restrict code to those available for App Extensions
  -assert-config <value>  Specify the assert_configuration replacement. Possible values are Debug, Release, Replacement.
  -D <value>              Specifies one or more build configuration options
  -embed-bitcode-marker   Embed placeholder LLVM IR data as a marker
  -embed-bitcode          Embed LLVM IR bitcode as data
  -emit-dependencies      Emit basic Make-compatible dependencies files
  -emit-module-path <path>
                          Emit an importable module to <path>
  -emit-module            Emit an importable module
  -emit-objc-header-path <path>
                          Emit an Objective-C header file to <path>
  -emit-objc-header       Emit an Objective-C header file
  -fixit-all              Apply all fixits from diagnostics without any filtering
  -fixit-code             Get compiler fixits as code edits
  -framework <value>      Specifies a framework which should be linked against
  -F <value>              Add directory to framework search path
  -gline-tables-only      Emit minimal debug info for backtraces only
  -gnone                  Don't emit debug info
  -g                      Emit debug info
  -help                   Display available options
  -import-underlying-module
                          Implicitly imports the Objective-C half of a module
  -I <value>              Add directory to the import search path
  -j <n>                  Number of commands to execute in parallel
  -L <value>              Add directory to library link search path
  -l<value>               Specifies a library which should be linked against
  -module-cache-path <value>
                          Specifies the Clang module cache path
  -module-link-name <value>
                          Library to link against when using this module
  -module-name <value>    Name of the module to build
  -nostdimport            Don't search the standard library import path for modules
  -num-threads <n>        Enable multi-threading and specify number of threads
  -Onone                  Compile without any optimization
  -Ounchecked             Compile with optimizations and remove runtime safety checks
  -output-file-map <path> A file which specifies the location of outputs
  -O                      Compile with optimizations
  -o <file>               Write output to <file>
  -parse-as-library       Parse the input file(s) as libraries, not scripts
  -parse-sil              Parse the input file as SIL code, not Swift source
  -parseable-output       Emit textual output in a parseable format
  -profile-coverage-mapping
                          Generate coverage data for use with profiled execution counts
  -profile-generate       Generate instrumented code to collect execution counts
  -save-temps             Save intermediate compilation results
  -sdk <sdk>              Compile against <sdk>
  -serialize-diagnostics  Serialize diagnostics in a binary format
  -target-cpu <value>     Generate code for a particular CPU variant
  -target <value>         Generate code for the given target
  -version                Print version information and exit
  -v                      Show commands to run and use verbose output
  -whole-module-optimization
                          Optimize input files together instead of individually
  -Xcc <arg>              Pass <arg> to the C/C++/Objective-C compiler
  -Xlinker <value>        Specifies an option which should be passed to the linker