I configured CocoaLumberjack properly using the swift pod.
And I can log whatever:
DDLogVervose("")
DDLogInfo("")
But I have to in every class to use it:
import CocoaLumberjack
Isnt there a way that I can globaly import ?
The only "trick" to have it available in all your classes is to use a pch (precompiled header) and import CocoaLumberjack there. It's a header that gets imported in all your files.
Related
I am trying to utilize an Objective-C framework inside the Swift package. For this, I would need to import the headers something like
#import<Framework/header.h>
Traditionally this can be achieved via bridging header. How can I set the bridging header via package manager settings?
This is bit tricky. You cannot import Objective-C framework's header files directly into SPM as you cannot write import statement such that #import<Framework/header.h>
The only feasible way is, write swift interface for your header files in Framework and then you would be able to import all methods. It can be done with module map as referred here
I frankly have a hard time understanding how imports work in Swift. When I make a new class it will start out with:
import foundation
As an alternative we could use import Swift or import UIKit depending on what libraries we need. BUT I've noticed that if I simply remove the imports my projects runs without any problems (even though I use classes from these libraries). This is where I need some help: I'm wondering if that is because I have internal frameworks that I import Swift/UIKit/Foundation and thereby get the import. So imports works like plague... if they touch a new class everything that class touches will have access to that import.
Yes, when a class that you use imports that framework, it is imported in your class too. This is to make things more clear, for example when class Foo's method abc has a parameter that needs UIKit, which is present in Foundation. Therefore, when you use the class Foo, UIKit is automatically imported.
As a side note, importing UIKit will import Foundation, in which it will also import Darwin. So it is really like plague. If a third party library (such as Charts) imports UIKit, it imports Foundation and Darwin too.
So we found the issue. Someone had added header file to each framework with:
#import <UIKit/UIKit.h>
Removing this line made everything less mad.
Now about how imports works. If a class A in library A import library B, class A will not have access to UIKit even though library B imports UIKit. This is how I'd expect it to work and how it actually works.
I have a project with large amount of obj-c and swift code inside it.
I have an obj-c class which imports ProjectName-Swift.h to let that class access swift code.
Now I need to expose that obj-c class to the swift so I can access it from swift code.
The problem is that after import in bridging header name of obj-c class project stops building with error that it cannot find ProjectName-Swift.h which import is stated in obj-c class.
I cannot remove import of ProjectName-Swift.h from obj-c class because after that class breaks.
What can I do ?
OK, had one answer and then re-read the question. Make absolutely certain that your import of the swift header is the Module Name of the project (not necessarily the project name):
Apple Documentation: Swift and Objective-C in the Same Project
More specifically, go to the section marked "Importing Swift into Objective-C" and read it. And even more specifically:
Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to “Yes”.
Import the Swift code from that framework target into any Objective-C .m file within that framework target using this syntax and substituting the appropriate names:
#import <ProductName/ProductModuleName-Swift.h>
I'm trying to import a framework made up of Objective-C header files.
I've created a bridging file but when I try to import a header the compiler doesn't recognise it.
This is error Xcode is showing:
Also if I try to reference the Framework in my Swift file as import ArcGIS it doesn't recognise that either.
What am I doing wrong?
You dont have to import ArcGIS header file. Guess you have used Carthage or CocoaPods. The only thing you need is to import ArcGIS
Check the documentation here
https://developers.arcgis.com/ios/swift/guide/install.htm
I was wondering what could be used instead of pch in swift.
Is there any alternative to pch or way to get rid of import in swift so that we don't need to do this for all classes.
I don't want to carry around/import all the time. What could be the best substitute of pch in swift.
You cannot define "Macros" in swift, so there is no ".pch" file for swift.
Also, Swift doesn't have separate "Header (.h) and Implementation (.m)", so there is no need of predefined headers which are needed to compile.
For more information refer to this question, Why .pch file not available in swift?
Class Import Problem:
In Swift, you don't need to write "import" statement for your project classes everywhere. You can access the project classes just by creating their objects.
Pods:
If you are using pods then it's necessary to import frameworks in
every class where you are using.
Third Party Swift Frameworks:
For external Frameworks/Modules, you can create a bridging header file
and replace the below line with your framework/module name:
#import ModuleName;
Reference: Implicitly import specific Swift module
There is no Swift equivalent for Objective-C .pch files.
Module is alternative for it.