Created
May 12, 2024 02:50
Resource file exists in swift package manager vs app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"]), | |
] | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Author
palaniraja
commented
May 12, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment