How to create Package (SWIFT PACKAGE MANAGER) using Terminal
1. Create a folder and get the path in the terminal
Ex::: cd /Users/vishnu.duggisetty/Documents/PracticeSP
2. Run the command “swift package init --type library”. This will create the Package library using folderName. creates a new Swift package with the necessary directory structure and files
3. Define Package Dependencies:
Edit the Package.swift file to define any dependencies your network layer package may have. For example, if you want to use Alamofire for networking, you can include it as a dependency:
import PackageDescription
let package = Package(
name: "PracticeSPM",
platforms: [
.iOS(.v15)
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "PracticeSPM",
targets: ["PracticeSPM"]),
],
dependencies: [
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.0.0")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "PracticeSPM",
dependencies: ["Alamofire"]
),
.testTarget(
name: "PracticeSPMTests",
dependencies: ["PracticeSPM"]),
]
)
4. Implement Network Layer Functionality:
Create Swift files within the Sources directory to implement your network layer functionality. This may include defining request/response models, implementing API client classes, and handling network errors. Here's an example of how you might structure your network layer:
import Alamofire
public class APIClient {
public static let shared = APIClient()
private let sessionManager: Session
private init() {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 30
self.sessionManager = Session(configuration: configuration)
}
public func request(_ urlRequest: URLRequestConvertible, completion: @escaping (Result<Data, Error>) -> Void) {
sessionManager.request(urlRequest).responseData { response in
switch response.result {
case .success(let data):
completion(.success(data))
case .failure(let error):
completion(.failure(error))
}
}
}
}
5. Create new project. Go to File —> Add Package Dependencies —> Add Local —> select the package.
6. For resolve packages: Go to File —> Packages —> Resolve Package Versions.
No comments:
Post a Comment