Import swift framework in swift project issue - swift

I checked all answers to similar problem and none helped.
I created a simple swift framework for testing. When I drag it to a Objective-C project, I can then import the header and use my simple class within Objective-C classes, np. Now when I do the same with a swift project, I can't reference my class (gives unidentified etc ...), I can import the header without error, just xcode can't see the class. That class is public and has one simple public method.
What does xcode need to be able to see that class in swift project when the process is so easy in OBJ-C project?

Related

multiple project with one workspace xcode [duplicate]

Note: I know How to call Objective-C code from Swift, but I don't know below,
I want to use this EsptouchForIOS's Demo in my project. The demo is write in OC, it has a storyboard and controller. I want to know how to integrate the demo in my swift project, and use that storyboard and it's controller in my swift project.
I'll start writing from the very beginning. Suppose you have a project in Objective-C and now you want to continue your project's development in Swift. Follow the below guidelines: (This intends to your specific needs)
First choose to add a new file from File->New->File. In this process select your language as Swift. In the final step here, you will be prompted to Create Bridging Header. Select that:
Now build your project once (⌘+B). You may get an error like this:
Change your target's minimum deployment to the version that Swift supports. (Example in the below screenshot)
To use Objective-C resources in Swift files:
Now that you've got one ProjectName-Bridging-Header.h file in your project. If you want to use any Objective-C class in your Swift files, you just include the header file of that class in this bridging header file. Like in this project, you have ESP_NetUtil and ESPViewController class and their header files too. You want to expose them to Swift and use them later in Swift code. So import them in this bridging header file:
Build once again. Now you can go to your Swift file. And use the Objective-C classes as like you use any resource in swift. See:
N.B: You must expose all the class headers (that you're intending to use later in Swift) in that bridging header file
To use Swift resources in Objective-C files:
Now you may wonder, I've successfully used Objective-C resources in Swift. What about the opposite? Yes! You can do the opposite too. Find your Target->Build Settings->Swift Compiler - General->Objective-C Generated Interface Header Name. This is the header file you will be using inside your Objective-C classes for any Swift to Objective-C interoperability. To know more check here.
Now inside any of your Objective-C class, import that interface header and use Swift resources in Objective-C code:
You will get more understanding from the official apple documentation.
You can checkout the worked out version of your linked project here with Objective-C-Swift interoperability.
So according to your question, you have added an objective C bridge in your swift project using How to call Objective-C code from Swift.
Now, import all headers (.h) files of your objective-c source code (demo project) that you want to direct use in swift file.
For example, your demo project has EsptouchForIOS following header (file with extension .h) files in project source code.
ESPAppDelegate.h, ESPDataCode.h, ESPTouchDelegate.h
import a header file in your bridge, which you want to use in your swift code. Suppose in your swift code you want touch delegate ESPTouchDelegate then write,
#import "ESPTouchDelegate.h"
Here is snapshot of your demo integration in my Test Swift project with bridge
and import statements.
Now, there is function/method in an objective C file getValue
which is used/accessed in swift project/file.
Similarly, you can import as many files (source headers) as you want in bridge and use the same files (source code) in swift.
I have never tried to use objective-c from swift project. But I normally used swift classes from my objective-c project. I usually follow this instructions https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html from apple developer website.

Add Swift Package to a custom framework

Pretty new to creating frameworks with SPM dependencies. So I made a new framework project, added some of my classes/files as well as a SPM dependency (CocoaLumberjack logger). Framework compiles fine.
When I look for my framework product that I'm planning on embedding into some other project I see that it is in my Products folder. Alongside with it I see CocoaLumberjack module. Inside of my framework there is not much beside the exec file.
When I try to embed my framework into some other projects. Nothing compiles because it says that CocoaLumberjack module is missing.
Does anyone know how to fix this? Am I missing an important step or soemthing?
Well, there are numerous isses you could have faced during importing framework itself. It also depends if you use framework as binary or source code. I assume you were using source code approach as you are the creator of framework. You can however check all approaches here: in this SO question . Lets look at all the steps you need to implement in order to successfully use framework with SPM dependencies in your swift project.
create SPM properly and also link all additional SPM dependencies tutorial here. Make sure all your classes, structs etc. and their coresponding initializer has correct access level property. If you plan to use them outside of the package, use public initializers..
2)Once you created you SPM package, link it to framework. For the sake of this answer I created testFramework and linked one of my custom SPM package called VodApiPackage . This package also contains dependency to another BaseTvApiServicePackage.
I also added TestPrinter file containing simple function for creating error declared in my SPM package. This function servers only for checking that everything is working properly and will be user later. It is also declared public.
import Foundation
import VodApiPackage
public struct TestPrinter {
public init () {}
public func makeTest() {
let x = VodApiError.customErr(msg: "testMsg")
print(x.localizedDescription)
}
}
Open your project and make link to framework, you can also check this nice tutorial. The most important step from tutorial is step 5 and 6. Where you drag .xcproj into your project and link libraries and framework
make sure your library and SPM dependencies are correctly linked in your project. Check sample project below.
Build and test using your framework and its packages:
import UIKit
import testFramework
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
testmodel()
TestPrinter().makeTest()
}
}

Building reusable libraries with Swift that use bridging headers

I'm trying to get the hang of Swift, and am beginning by just doing a dumb port of a few applications I've written.
These applications have some core logic in common, for which I've used a Framework target in Xcode to share this with those projects. I'm having trouble coming up with an equivalent in Swift.
I know Swift compiles down to modules, which seems like what I want. I want a Swift module that I can share with my other projects. the major problem I seem to be having though, is that you cannot have a Framework with Swift if it also uses a bridging header starting in Beta 4, which I need to call some APIs (like Security.framework) that don't have Swift bindings. The compiler (Beta 5) fails with this error message:
<unknown>:0: error: using bridging headers with framework targets is unsupported
What can I do to create a reusable Swift module that also needs to use bridging headers? Alternatively, how can I use things in Security.framework without a bridging header? (Alternatively Aternatively, is there something other than a Framework I should be using to create a module that doesn't have any of these problems?)
To import Objective-C code to swift within the same framework target, just import each Objective-C header file in the umbrella header file. Apple's official document has mentioned that already: https://developer.apple.com/library/ios/documentation/swift/conceptual/buildingcocoaapps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_81 see the Importing Code from Within the Same Framework Target part.
The cocoa built in frameworks have been migrated as modules in swift. To use the Objective-C's Security.framework, you just need to add a line:
import Security
at the header of the swift file.

Objective-C Bridging Header for frameworks

I've made a framework that requires the sqlite3 framework. How do I add a Objective-C Bridging Header for my framework that imports sqlite3 into my Swift file?
I already have a bridging header file for my project, but not for my framework.
I found a Objective-C Bridging Header setting in the target Build Settings. It was hidden by default. Check All instead of Basic.
In recent Xcode versions this solution would give the error Using bridging headers with framework targets is unsupported.
The workaround I've been using is to make the C-header public in the file inspector and import it in MyFramework.h like this example:
#import <MyFramework/MyObjectiveC.h>
How to change the C-header to public
Open your C-header and view the inspector by clicking in the upper right corner. To view the file inspector, click the file icon in the upper right corner.
just import your sqlite3 framework in your objective-c bridging file. You then automatically can use it in Swift.
Apple Docs:
Interoperability is the ability to interface between Swift and Objective-C in either direction, letting you access and use pieces of code written in one language in a file of the other language. As you begin to integrate Swift into your app development workflow, it’s a good idea to understand how you can leverage interoperability to redefine, improve, and enhance the way you write Cocoa apps.
One important aspect of interoperability is that it lets you work with Objective-C APIs when writing Swift code. After you import an Objective-C framework, you can instantiate classes from it and interact with them using native Swift syntax.
EDIT: You even can import an Objective-C framework or Swift framework or a mixed-language framework just into your swift file with import yourFramework
Apple Docs:
Importing External Frameworks
You can import external frameworks that have a pure Objective-C
codebase, a pure Swift codebase, or a mixed-language codebase. The
process for importing an external framework is the same whether the
framework is written in a single language or contains files from both
languages. When you import an external framework, make sure the
Defines Module build setting for the framework you’re importing is set
to Yes.
You can import a framework into any Swift file within a different
target using the following syntax:
SWIFT
import FrameworkName
You can import a framework into any Objective-C .m file within a
different target using the following syntax:
#import FrameworkName;

Swift: how can I create external interface for static library (public headers analog in Objective-C .h)

I need to create a static library with Swift,
and I need to know how can I implement interface for the library.
In Objective-C I can mark needed headers as public in build phases,
but there is not any headers and any interfaces in Swift.
What should I do with Swift?
Simply put: you don't.
Swift is not a language that separates headers and implementations. When you create a library or framework based on Swift and only for consumption by Swift, the Xcode default build setting of DEFINES_MODULE already does the job for you. This will create a .swiftmodule file, which will be used by import in other Swift projects.
If you want your code to be importable from Objective-C though, you might want to check if the SWIFT_INSTALL_OBJC_HEADER build setting is also enabled (which it is by default for frameworks as far as I know). Then the Swift compiler will generate a <ProductName>-Swift.h file for you, which you can import in Objective-C code to access your Swift classes and functions.
If you want your Swift framework to expose certain classes to the interface, simply mark the 'entities' (functions, variables etc.) public:
public class Class {}
public var variable: Int
public func function() { }
By default, all entities have internal access.
public entities are intended for use as API, and can be accessed by any file that imports the module, e.g. as a framework used in several of your projects.
internal entities are available to the entire module that includes the definition (e.g. an app or framework target).
private entities are available only from within the source file where they are defined.
Source: the official Swift blog.