Reputation: 141
I plan to create swift static library and use in ios swift app. I create a swift static libraryn call SimpleLib and it include a public class Greeting which return Hello World string:
//
// Greeting.swift
// SimpleLib
//
import Foundation
public class Greeting {
public func Hello() -> String {
return "Hello World";
}
public init() {
}
public static func SayMorning() -> String{
return "Hi, Morning";
}
}
The swift static library project look like:
And the module.modulemap is defined as following:
module SimpleLib {
header "SimpleLib-Swift.h"
export *
}
I build and produce a libSimpleLib.a file, I place the .a and other files (referred by other posts in internet to mention need put in app folder) in app folder:
In the app project, I include the Libs path in FREAMEWORK_SEARCH_PATHS, LIBRARY_SEARCH_PATHS and HEADER_SEARCH_PATHS and include .a file in Linked Framework
However, when I attempt to refer the Greeting class in the AppDelegate, I got the error - Use of unresolved identifier 'Greeting'
//
// AppDelegate.swift
// testStatic
//
import UIKit
import SimpleLib
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
var s = Greeting
return true
}
......
}
How to make Swift objects in static library can be referred in the App. What is the proper steps to export the class/functions in swift static library? Anyone success to build and use swift static library in iOS app?
Upvotes: 3
Views: 4017
Reputation: 161
This is the way to create a static library in Objective-C and Access in swift application. follow the below steps.
How import Objective-C static library project file in Swift Sample Codebase for debug.
Step 1. Copy-paste complete SDK source code in sample project directory
Step 2. Add static library project file in sample code.
Step 3. Add Library (TestStaticLib.a) dependancy and required C++ library or curl or lumberjack framework and etc
Select target-> Build Phases-> Add Library and framework.
Step 4. Link Dependency
Select on + and add universal library dependency.
Step 6. Set header search path for library .h files
Select target-> Build Setting-> Header Search Path
Put you library path like “$(SRCROOT)/ConnectSDKApp/TestStaticLib"
Step 7. Compile Universal library
Open library target separately and compile the Universal library.
Once compilation is done. Check-in library path in universal folder whether .a file created or not.
If created fine. Otherwise, check why it’s not created. .a file created through script. Which in
Library Universal target -> Build Phase and Run Script.
If .a file is not created no need to sample code. It will not compile the unit .a file not created.
FAQ.
Answers. You have not linked the correct header search path. Please check the step and set the correct header search path.
Answers. Link dependency of your universal library step 4
Question 3. Application crashing… because of this reason
reason: '*** -[__NSDictionaryM setObject:forKey:]: object cannot be nil (key: com.hac.mapService.TomTom)'
Answers.
Question 4. Universal .a file not getting creating after building success.
Answers. Check script may be something wrong with the script.
Question 5. What is the use of the script.
Answers. Script required to create a universal library that is required for both device and simulator to compile.
TestStaticLib.a file only compile the same time for both device and simulator when created with device and simulator architecture.
Want to access .a static in your Project.
Step 1. Add Universal library (TestStaticLib.a) in your sample code which will compile for both Simulator and Devices
Step 2. Add Library (TestStaticLib.a) dependancy and required C++ library or curl or lumberjack framework and etc
Select target-> Build Phases-> Add Library and framework.
Step 3. Import .h file in Bridge header
Compile.
Upvotes: -1
Reputation: 103
Make sure to add both the .a library and relevant .swiftmodule from the build product to app files. Then link the .a library in the "Linked Framework and Libraries" also make sure it's present in the Build Phases in "Link Binary with Libraries".
Lastly in build settings, under "Swift Compiler - Search Paths" > "Import Paths" add the path to the .swiftmodule folder. The paths are relative to the .xcodeproject.
Make sure that the classes and methods you are using are either open or public.
Upvotes: 0
Reputation: 876
Include your module.modulemap
and arm64.swiftmodule
path in app's SWIFT_INCLUDE_PATHS
setting.
Upvotes: 3
Reputation: 138031
The issue is that your class has the same name as your module. Try simple.simple()
to create an instance of your class, or alternatively, rename either the module or the class.
Upvotes: 0