What is an alternative to pch in swift? - ios

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.

Related

Could not build Objective-C module, when using swift in objective-c module

In an iOS application I have a subproject (not cocoapods) in which I have included a swift file and ObjC file (that is used by the swift file). XCode automatically created a bridging file but could not build it because apparantly bridging is not allowed in a framework. The workaround that I used was to add the objective-c header to the umbrella file and it worked. Now I need to use a swift class from ObjC. I have define module to set to YES, the generated file Framework-Swift.h . But when I try to import it in objective-c i get
Could not build Objective-C module
The closest I got after some googleing was this answer:
Ah gotcha. It looks like you're building a mixed Swift & Objective-C
pod - if that's the case, Xcode will try to import
within the generated -Swift.h header.
You'll need to create the header manually and add imports for the
Objective-C classes that you want to expose to Swift.
CocoaPods generates an umbrella header automatically and imports it
within the .modulemap, but Xcode doesn't use that when generating the
-Swift.h header
But I am unsure what header needs to be created manually.
Any ideeas or pointer about using swift in an objective-c framework ? In both ways ?
I also had similar issue when using Swift pods in my Swift project containing several targets. No Objective-C code at all. I tried to clear build folder, pods cache, derived data - nothing worked.
Solution:
Open the Build Settings for a target that contains your module code. Set the "Install Objective-C Compatibility Header" to "No"
There's a great and simple article that wraps up this case:
DEFINES_MODULE=YES
To use ObjC classes in Swift, create a bridging header and specify path to it in the build settings
To use Swift classes in ObjC, #import <ModuleName/ModuleName-Swift.h> in your *.m file (use #class SwiftClass; forward declaration in *.h file, if needed)
For swift classes and their members to be visible to objc
inherit your class from NSObject
prepend it with #objcMembers
make both the class and its members public

Import of ProjectName-Swift.h and Bridging-Header.h recursion

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>

How to import Objc Files into my Swift files within my custom cocoapods framework?

Simple explanation. I have created my own pod with name BSSelectableView, but within I need to use two files written in Objective-C. What do I need to achieve?
I need to make it visible within my Swift files, and also for people who will install my pod.
Your Target needs a Bridging Header.
This is an Objective-C-Header-File with the name [TargetName]-Bridging-Header.h
Within this file, you have to #import all Objective-C-Headers you want to use in your Swift-Files.
You don't have to import anything in your Swift files.
More information about Swift and Objective-C mixed projects
You need to create a [TargetName]-Bridging-Header file in your project.
import all the header files in bridging header file
Now you can use all objective c file in swift.

How to use an Obj-c Library/Custom interface in a Swift Project

Trying to understand how I can use this in my swift project.
https://github.com/Grouper/FlatUIKit
I have copied the classes folder into my project but am not sure how to use the various .h and .m files. How would I go about using these files within my storyboard to use the custom appearances?
Did some searching and wasn't really able to understand the various threads that I found.
A bit of a broad question so I'll try to provide some basics and hopefully that helps.
If you are unaware, there is a whole book written by Apple about how to use objective-c with swift. it's available on iBooks for free Using Swift with Cocoa and Objective-C (Swift 2.1) by Apple Inc
Here is the online link
You will need to create an objective-c bridging header and import your code through the header.
Your bridging header would look like this:
#import "XYZCustomCell.h"
#import "XYZCustomView.h"
#import "XYZCustomViewController.h"
If they don't have any modules then you can use them in your swift code and it should see them. According to Apple:
Use your custom Objective-C code with the same Swift syntax you use with system classes.
let myCell = XYZCustomCell()
myCell.subtitle = "A custom cell"
If you are importing an Objective-c framework then it should already contain and umbrella header file that takes care of the bridging header stuff for you. then you just import the framework name into the class that you are planning on using.
import MyCustomFramework
Link to the apple docs here
For this, you can use a bridging header, which is used to expose Objective C code to swift. To do this, make a new header file in your project. Import the header files you would like to use in the new header file like this:
#import <FlatUIKit/FlatUIKit.h>
And then, in the build settings of your project, define the Objective-C bridging header to be the header file you just created. Now in any swift files that you would like to use the library in just import the classes like this:
import FlatUIKit
Hope this helps!

Mixed language framework

I have a framework (let's call it MyKit) written in Objective-C that I'm extending with some Swift classes. I'm trying to get my head around it using this documentation: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_77
As far as I understand, I'm not supposed to have a bridging header class, and instead put all my includes in the umbrella header file (that I understand to be ).
So I write in MyKit.h:
#import <MyKit/ModelObjectA.h>
#import <MyKit/ModelObjectB.h>
#import <MyKit/ControllerObjectC.h>
I don't list ControllerObjectD.swift, even though it goes into here as well? Or should I include
#import <MyKit/ControllerObjectD-Swift.h>
?
ControllerObjectD uses ModelObjectA and ModelObjectB. Now that I don't have a bridge headerfile, I get compile errors in it because it cannot find these objects.
The documentation says "Swift will see every header you expose publicly in your umbrella header." and this is true when I import the framework into other projects, but the framework project cannot compile because it doesn't see it. I have turned on the "Define Modules" build setting.
Is there something I've misunderstood about the umbrella header, perhaps? Where can I say "hi project, this is the umbrella header file"? The framework compiles if I set the umbrella header file as bridging header, but that sounds like I've come back to the beginning this way?
Cheers
Nik
I believe your problem may be down to the Access Modifiers in your Swift class, however I've written a short guide and sample project to help you:
Sample project can be found here
There are two parts to having a mixed language framework:
Importing Objective-C into Swift
Importing Swift into Objective-C
1. Importing Objective-C into Swift
This is, for example, if you have an Objective-C class named Player that you want to add to a swift class called Game.
According to the documentation, you need to do these two steps to import the Player object into the Game object.
Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes.
In your umbrella header file, import every Objective-C header you want to expose to Swift.
#import <Framework/Player.h>
Make sure your Player header file in Objective-C is marked for public target membership in the framework:
Following these steps, it's possible to import the Player Objective-C class into the Game Swift Class:
import UIKit
public class Game: NSObject {
public let player: Player
public init(player: Player) {
self.player = player
super.init();
}
}
2. Importing Swift into Objective-C
For importing the Swift Game class into the Player object, we can follow a similar procedure.
As before; 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>
In my case, this works as:
#import <SwiftObjC/SwiftObjC-Swift.h>
and I assume, for you:
#import <MyKit/MyKit-Swift.h>
Therefore, make sure that all the properties, methods and classes you want to access are defined as public in your swift file or else they won't be visible to Objective-C .
I have uploaded my sample project showing how this all works on GitHub https://github.com/elliott-minns/SwiftObjCTestFramework
I hope this helps you with your problem.
I think you need to do bridging-header to access your obj-c code in swift.
More in the link
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
so it is a file with your imports from obj-c transfering to swift. names are very important so care about that. hope it helps
p.s. also you need to add this header path to your project settings
From the Documentation at Apple: A Swift class must be a descendant of an Objective-C class to be accessible and usable in Objective-C. For more information about what you can access from Objective-C and how the Swift interface is imported, see Swift Type Compatibility.
So your public swift class needs to extend NSObject for example.

Resources