I have a static library in Swift and I want to access this library class from another Swift project. The class in the library cannot be referred properly.
My static library called StaticLib
contains hierarchy,
Static Library Project - Hierarchy
StaticLib (project)
StaticLib
StaticLib.swift
Products
libStaticLib.a
Static Library Project - StaticLib.swift
public class StaticLib {
public class func test(string: String) {
Printer().printMe(string: string)
}
}
class Printer {
func printMe(string: String){
print("This is test tring: \(string)")
}
}
Host App - AppDelegate.swift
import StaticLib
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
StaticLib.test("How do you do?") // error
return true
}
}
Steps I Have Done -
- Click project in Xcode Project Navigator
- Click + to add new target
- Select Frameworks & Libraries > Static Library > Next
- Enter name
StaticLib
(in Swift) > Finish - Open StaticLib.swift and enter
public class StaticLib {
public class func test(string: String) {
Printer().printMe(string: string)
}
}
class Printer {
func printMe(string: String){
print("This is test tring: \(string)")
}
}
- Select
StaticLib
schema, Build > OK - Copy the
libStaticLib.a
into a new folder calledHostApp/Libs/
- Select project in Project Navigator > click main application target > select General
- In section Framework, Libraries, and Embedded Content click I see
libStaticLib.a
is already added by Xcode intelligence - Select main app schema > Build > OK
- Select AppDelegate.swift > add
import StaticLib
- Add
StaticLib.test("How do you do?")
- Try to Build
Compilation Error:
Use of unresolved identifier 'StaticLib'
How can I resolve this issue? Do I need to create any header file in library?
Note: I have embedded the libStaticLib.a
successfully and it's present in Host apps General -> Frameworks, Libraries and Embedded contents
tab.