Swift 3 introduced package manager, with which I can add dependencies to my project.
For example, in my Package.swift , I can declare my dependency by:
import PackageDescription
let package = Package(
name: "my-project",
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 0, minor: 16),
]
)
Now, I need to add Alamofire to my project, how can I get it through Swift 3 package manager?
You can't use Alamofire yet because they haven't released Swift 3 support.
Once they've released this, I'm sure they will make it obvious on their GitHub readme how to import it.
Luckily Vapor comes with a great HTTP and even WebSockets client. Read more about the HTTP client in the documentation: https://vapor.github.io/documentation/http/client.html
Well, today you just have to add the following line in your Package.swift file:
.Package(url: "https://github.com/Alamofire/Alamofire.git", majorVersion: 4)
Really easy :)
Related
We have two swift packages that we've created. One has some bare bones swift code and the second has some Swift UI code. When we create a Swift UI application, it can see classes from either of the swift packages. However, we can't get the Swift UI package to see code from the other bare bones swift code package.
Both of the packages are being hosted on github. In the Swift UI package, we added a package dependency to package.swift like this:
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/myorg/myswiftpackage", from: "1.0.1")
],
When we try to use the classes from myswiftpackage, we get a "Module Not Found" error on the import statement.
What are we missing that allows a package to use code from another package?
Thanks Joakim, that was the answer. We had only listed it in the dependencies list, but both the targets also needed to have the package name listed as a dependency. Here's an example both references in case anyone needs it.
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/myorg/myswiftpackage", from: "1.0.1")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "MySwiftUiPackage",
dependencies: ["MySwiftPackage"]),
.testTarget(
name: "MySwiftUiPackageTests",
dependencies: ["MySwiftUiPackage", "MySwiftPackage"]),
]
The documentation about SPM is quite poor and most of the articles about it were published when SPM began.
I have implemented an algorithm (Hungarian algorithm) in Swift and was looking forward to publish it as a library in Github. I have had to use the SPM for the first time in this project to resolve another dependency and once it has started to work, it was perfect.
Now, I am not being able to use my library from another project. I have decided to start with a new fresh git repo cause I couldn't get the previous to work.
The library is named Hume and it has inside a Hume.swift file which defines a Hume class.
The steps I have gone through are these:
Created directory for the library
swift package init --type library
Populated the files with the code
Pushed to a GitHub repo + tagged the version
Created directory for the executable which is going to use the library
swift package init --type executable
Add the dependency in Package.swift
swift build
At this point, swift clones the repo of the library and compiles it without problems (as main.swift contains just a Hello world).
swift package generate-xcodeproj
An when I open the project and try to import my library, it seems the module name is recognized but when I try to declare an object, it says I can't declare a variable of a type that is a module.
This is the Package.swift file in the library:
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "Hume",
products: [
.library(
name: "Hume",
targets: ["Hume"]),
],
dependencies: [
.package(url: "https://github.com/aleph7/Upsurge.git", from: "0.10.2"),
],
targets: [
.target(
name: "Hume",
dependencies: ["Upsurge"]),
.testTarget(
name: "HumeTests",
dependencies: ["Hume"]),
]
)
The Library has only one file with this style:
import Upsurge
class Hume {
// Attributes
....
init(matriz:[[Double]]){
....
}
public func resuelve() -> (Double, [(Int, Int)]){
....
}
}
This is the Package.swift in the dummy executable:
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "aa",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/Jasagredo/Hume.git", from: "0.1.1"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "aa",
dependencies: ["Hume"]),
]
)
The output when building:
~/Desktop/aa swift build
Fetching https://github.com/Jasagredo/Hume.git
Fetching https://github.com/aleph7/Upsurge.git
Cloning https://github.com/Jasagredo/Hume.git
Resolving https://github.com/Jasagredo/Hume.git at 0.1.1
Cloning https://github.com/aleph7/Upsurge.git
Resolving https://github.com/aleph7/Upsurge.git at 0.10.2
Compile Swift Module 'Upsurge' (30 sources)
Compile Swift Module 'Hume' (1 sources)
Compile Swift Module 'aa' (1 sources)
Linking ./.build/x86_64-apple-macosx10.10/debug/aa
~/Desktop/aa
But when I edit main.swift in the dummy executable, I find this error:
import Hume
var a = Hume(matriz: [[1,1],[1,1]]) //Cannot call value of non-function type 'module<Hume>'
Also, Xcode doesn't auto-suggest me the class Hume. I just don't know what I'm doing wrong.
Any help is appreciated.
I have finally managed to get things to work. The problem was the class (and it's init method) weren't declared as public. The rest of the configuration is correct.
I have the latest release versions of Swift and Xcode.
I am trying to use the Swift Package Manager to use this:
.package(url: "https://github.com/matejkosiarcik/Stopwatch.git", from: "0.1.0")
However, I have spent multiple days trying how to figure out how to embed this in my command line tool, or even in a brand new command line tool so that in Main.swift it doesn't give the error:
No such module 'Stopwatch'.
Would someone please explain how to do it for this exact package?
Thank you.
Note that the modules in a Swift Package are specified as targets, not as products.
Suppose you have the following product spec in your Package.swift:
products: [ .library(name: "MyProduct", targets: ["MyTarget1", "MyTarget2"])]
Then in some other package you use this product as a dependency in Package.swift:
.target(name: "TargetInSomeOtherPackage", dependencies: ["MyProduct"])
And in swift code you import modules:
import MyTarget1
import MyTarget2
In your case, you defined your product: .library(name: "Stopwatch", targets: ["lib"]). This means that you can use Stopwatch as a dependency, and import module lib in your Swift code. There is no module Stopwatch that you can import.
I'm trying to use updated SPM for Swift4 with the following Package.swift file - PackageDescription API Version 4
import PackageDescription
let package = Package(
name: "Name",
dependencies : [
.package(url: "url", .branch("swift4"))
],
exclude: ["Tests"]
)
I have a correct version of SPM also:
Apple Swift Package Manager - Swift 4.0.0-dev (swiftpm-13081.9)
But I can not build the library by swift build command. I see the following error:
... error: type 'Version' has no member 'branch'
You're missing the tools version specifier in your manifest; add the following as the first line of your Package.swift:
// swift-tools-version:4.0
By default if that line is omitted, it'll default to manifest version 3 and also compiler version 3. For more information see SE-0152 or Swift Package Manager Manifest API Redesign on swift.org.
I'm trying to figure out how to use Alamofire in a command line app.
I can't use frameworks, so I've added the Alamofire source code into the app (so no import statement) and I'm able to directly reference the request() and other methods.
Is there a cleaner way to encapsulate Alamofire or is this a limitation in Swift 2.X at the moment?
For those who gonna need it here is the solution.
If you don't have the file xcodeproject please generate
$ swift package generate-xcodeproj
You may need to fix the Deployment Target
Now time to add the Alamofire
import PackageDescription
let package = Package(
name: "SuiteTest",
dependencies: [
.package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.0.0")),
.package(url: "https://github.com/JohnSundell/Files", from: "4.0.0")
],
targets: [
.target(
name: "SuiteTest",
dependencies: ["Files", "Alamofire"]),
.testTarget(
name: "ASuiteTestTests",
dependencies: ["Assure-SuiteTest"]),
]
)
I hope I have helped anyone who has the same problem :)