nburk nburk - 3 months ago 12
iOS Question

After running `swift build` dependencies are downloaded by class is not found

I am trying to create an example with the Swift Package Manager where I am creating a small library, host it on my github and then use this library as a depdendency to create an executable Swift program.

This is my setup:

library



enter image description here

MyXMLHelper.swift

class MyXMLHelper {

static func enclose(text: String, withXMLTag tag: String) -> String {
let openingTag = "<\(tag)>"
let closingTag = "</\(tag)>"
return openingTag + text + closingTag
}

}


Package.swift

import PackageDescription

let package = Package(
name: "XMLHelper"
)


You're also welcome to check out the original version on my github.

executable (cmd line)



main.swift

import XMLHelper

print("Please enter a text you would like to enclose: ", terminator: "")
let text = readLine()!

print("Please enter the XML tag: ", terminator: "")
let tag = readLine()!

let result = MyXMLHelper.enclose(text: text, withXMLTag: tag)
print("Result: \(result)")


Package.swift

import PackageDescription

let package = Package(
name: "XML_application",
dependencies: [
.Package(url: "https://github.com/nikolasburk/xml-util",
majorVersion: 2),
]
)


After running
swift build
, the folder structure looks like this:

enter image description here

So,
swift build
successfully downloads the dependency, but then it also gives me the following error:

/Users/nburk/Projects/makeschool/tutorials/swift-package-manager-demo/xml-app/main.swift:9:14: error: use of unresolved identifier 'MyXMLHelper'
let result = MyXMLHelper.enclose(text: text, withXMLTag: tag)
^~~~~~~~~~~
<unknown>:0: error: build had 1 command failures
error: exit(1): /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-08-24-a.xctoolchain/usr/bin/swift-build-tool -f /Users/nburk/Projects/makeschool/tutorials/swift-package-manager-demo/xml-app/.build/debug.yaml


Does anyone know why that is? Why can't it find the class
MyXMLHelper
, shouldn't it be visible here?

Answer

This is probably because your class is not public.

In MyXMLHelper.swift, it should be declared like this:

public class MyXMLHelper {

or, if you want to be able to subclass it (Xcode 8 beta 6):

open class MyXMLHelper {
Comments