Skip to content

Instantly share code, notes, and snippets.

@palaniraja
Created May 12, 2024 02:50
Show Gist options
  • Save palaniraja/4837ebeaf75fb76672f4edb4e6d74538 to your computer and use it in GitHub Desktop.
Save palaniraja/4837ebeaf75fb76672f4edb4e6d74538 to your computer and use it in GitHub Desktop.
Resource file exists in swift package manager vs app
import SwiftUI
import spmlib
struct ContentView: View {
let l = SPMLib()
var body: some View {
VStack {
Text("SPMLib version: \(l.version())")
Text("spm data.json exists: \(String(describing: l.fileExists(name: "data", type: "json")))")
Text("spm appdata.json exists: \(String(describing: l.fileExists(name: "appdata", type: "json")))")
Text("app data.json exists: \(String(describing: l.fileExistsInApp(name: "data", type: "json")))")
Text("app appdata.json exists: \(String(describing: l.fileExistsInApp(name: "appdata", type: "json")))")
}
.padding()
}
}
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "spmlib",
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "spmlib",
targets: ["spmlib"]
),
],
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: "spmlib",
resources: [.copy("data.json")]),
.testTarget(
name: "spmlibTests",
dependencies: ["spmlib"]),
]
)
import Foundation
public struct SPMLib {
public init() {}
public func version() -> String {
return "v1.0.1a"
}
public func fileExists(name: String, type: String) -> Bool {
let f = Bundle.module.url(forResource: name, withExtension: type)
return f != nil
}
public func fileExistsInApp(name: String, type: String) -> Bool {
let f = Bundle.main.url(forResource: name, withExtension: type)
return f != nil
}
}
@palaniraja
Copy link
Author

spm-app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment