3

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 -

  1. Click project in Xcode Project Navigator
  2. Click + to add new target
  3. Select Frameworks & Libraries > Static Library > Next
  4. Enter name StaticLib (in Swift) > Finish
  5. 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)")
        }
    }
  1. Select StaticLib schema, Build > OK
  2. Copy the libStaticLib.a into a new folder called HostApp/Libs/
  3. Select project in Project Navigator > click main application target > select General
  4. In section Framework, Libraries, and Embedded Content click I see libStaticLib.a is already added by Xcode intelligence
  5. Select main app schema > Build > OK
  6. Select AppDelegate.swift > add import StaticLib
  7. Add
    StaticLib.test("How do you do?")
  1. 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.

Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256

1 Answers1

1

I've passed your steps and all works, at least at Xcode 11.3.1 / iOS 13.3. The only thing... it seems... see comment

import StaticLib // << looks like you've forgot this one !!!

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {


        StaticLib.test("How do you do?") // error

        return true
    }
}

Update: Here are my steps -

  1. Click project in Xcode Project Navigator
  2. Click + to add new target
  3. Select Frameworks & Libraries > Static Library > Next
  4. Enter name StaticLib (in Swift) > Finish
  5. Open StaticLib.swift and enter
    public class StaticLib {
        public class func printme() {
            print("I'm swfit static lib!")
        }
    }
  1. Select StaticLib schema, Build > OK
  2. Select project in Project Navigator > click main application target > select General
  3. In section Framework, Libraries, and Embedded Content click + and select libStaticLib.a > Add
  4. Select main app schema > Build > OK
  5. Select AppDelegate.swift > add import StaticLib
  6. Add anywhere in code
    StaticLib.printme()
  1. Build > OK > Run ... see output

Update2: for external Lib project

+1 It is needed to copy StaticLib.swiftmodule (it is created at the same place as libStaticLib.a) into target project folder (I placed it at the level of .xcodeproj file)

+2 In main application target Build Settings set SWIFT_INCLUDE_PATHS = ${SRCROOT}

+3 Clean > Build

Note: the indicator that module is loaded is autocompletion for import - it should show StaticLib

Asperi
  • 228,894
  • 20
  • 464
  • 690
  • How did you add the library? Drag and drop or moved to location physically and refer it? Did u change anything in build setting? I am getting error. See comment in post. – Sazzad Hissain Khan Jan 23 '20 at 13:02
  • In step 8, it does not show the `libStaticLib.a`, unless I copy it into a folder in host app. Do I need to do that? However, if I copy the library its already shown in General tab. Note that both library and host app are not in same workspace. My Xcode version: 11.0 (11A420a) – Sazzad Hissain Khan Jan 23 '20 at 13:34
  • I have updated the post with all the steps I have done. Please see. – Sazzad Hissain Khan Jan 23 '20 at 13:44