简体   繁体   中英

Swift function from external library not importing

I am trying to create an external swift library full of functions I can call in other projects. I followed the basic steps to creating a library in Swift

I ran

swift package init

my package.swift looks like

import PackageDescription

let package = Package(
    name: "TestProject",
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
        .library(
            name: "TestProject",
            targets: ["TestProject"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    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: "TestProject",
            dependencies: []),
        .testTarget(
            name: "TestProject",
            dependencies: ["TestProject"]),
    ]
)

and ran swift package generate-xcodeproj to make the project.

then uploaded the project to Github and loaded it into a different project using Swift Package Manager. It loaded in just fine.

I created a simple SwiftUI project for testing my library. in the library, I have added the function

func test() -> String{
    return("This was good!")
}

and then in the project, in my ContentView.swift file I added import TestProject in the import statements (which did build correctly) and attempted to call test() in the file by setting the value of the default Text("Hello World") to Text(test()) to see if it would work.

I was prompted with an error stating that test() was not a defined

Use of unresolved identifier 'test'

I'm not sure where I'm going wrong as far as importing my library and I've found surprisingly little while trying to look into this.

my TestProject.swift file located in `/Sources/TestProject/' as generated by swift on the init

struct SwiftSciduct {
    var text = "Hello, World!"
}

func test() -> String{
    return("This was good!")
}

my contentView.swift file

import SwiftUI
import TestProject

struct ContentView: View {
    var body: some View {
        Text(test())
    }
}

#if DEBUG
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

Kind of at a loss of why this function isn't useable in the main project.

You cannot access test() because it has defaulted to the internal access level. You need to mark it as public or open in order for other modules to see it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM