Using Obj-C that relies on other Obj-C in Swift? - ios

First I want to note, I've read this and have used it before and my question is different:
How to call Objective-C code from Swift
I'm trying to use this library in Swift.
This library uses OCMock and when I try to compile, I get 'OCMock/OCMock.h' file not found at the line #import <OCMock/OCMock.h>. Here's how I normally fix it, but this time, it's not working.
Just like every other Obj-C library in Swift, I added it to my bridge header, which I already had made and am using for others:
#import "JSQMessages.h"
#import "JSQMessageData.h"
I then added $(PROJECT_DIR) to my Header Search Paths.
Here is the file structure.
From the project directory, it should look for OCMock/OCMock.h, and it is there, as seen in screenshot. So why isn't it working?

I just created a fresh swift iOS project, add JSQMessage using CocoaPod(http://cocoapods.org). No error. try following:
1. Create a new project
2. Create a podfile and add
pod 'JSQMessagesViewController'
3. Create a bridge header and add
#import <JSQMessagesViewController/JSQMessages.h> // import all the things
3. Run
pod install
4. Open project.xcworkspace and build it

Related

How to work with Swift file in Objective-C?

I want to use one third party library which is written in Swift but I am working in Objective-C environment. How to do that? Until now what I have done is:
Drag and drop the third party library into my project.
It is asking for the bridging header I clicked on yes.
In Build settings defines Modules no to yes in both.
Product Module Name showing like $(PRODUCT_NAME:c99extidentifier). I have tried with my project name also.
Now I am getting plenty of errors like below:
pDatePicker.swift:50:9: Consecutive declarations on a line must be separated by ';'
and
Ambiguous use of 'open'.
I am getting errors around 50 to 60 same as above errors. Where is my mistake? Can anybody help me?
Are you able to see projectName-Swift.h file in your project ?
Demo Example is available on github.
https://github.com/hasyapanchasara/UsingSwiftInObjectiveC
If yes then, Hope you have imported #import "projectName-Swift.h" in Objectice C .m file ?
#import "projectName-Swift.h"
Then only you would be able to access objective c files in swift code.
Refer
https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
Check in build setting
Objective-C Bridging Header
ProjectName-Bridging-Header.h has perfect location.
if there is problem with that path than bridging will not work properly , that why when you compile #import "projectName-Swift.h" is not getting generate.
Download
Download above sample project.
Make sure, that your swift version in settings is the same as this lib uses; if not, try to translate it by yourself, or set Use Legacy Swift Version to YES

Using Swift library in an Objective-C project

I am working in an Objective-C, iOS project, I used danielgindi Charts library which is a Swift library. I downloaded it using Cocoapods.
I am trying to import the library files into my Objective-C files using 'projectName-Swift.h' as mentioned in this question but I faced an error:
'projectName-Swift.h' file not found
If you have installed Swift library using cocoapods
To import Library in Objective C Code
Use
in Objective C
#import frameworkname;
In Swift
#import frameworkname
If your using cocoa pods try to make sure you have use_frameworks! in your cocoa pods pod file, otherwise the Swift Framework wont work. Also look at this for any other issues that may pop up: link
For making the bridging header visible to your compiler you need a little setup
Go to your Project Build Settings
Search for bridging header
Add the path to your
.h file (usually ProjectName/ProjectName-Swift.h)

Setting up XlsxReaderWriter - Swift

I'm trying to use the XlsxReaderWriter library from Github - https://github.com/renebigot/XlsxReaderWriter
I have followed the steps and now this is my project hierarchy - http://postimg.org/image/4daa89bo7/
I have added the project as a sub project and added it in the target dependencies and added the libXlsxReaderWriter.a to the binary like this - http://postimg.org/image/3l2z7uxyl/
The problem is that I can't use the library and that the libXlsxReaderWriter library is marked in red.
How can I set up the XlsxReaderWriter library correctly?
I've just committed some code plus a Swift bridge header file. You should use it as your bridge header file or #import it from your current one.
Let me know if everything run fine. I'm still using Obj-C (shame on me!), so I've never tested it with Swift.
Cheers

CocoaPods not building target for Crittercism

I added pod 'CrittercismSDK' to my Podfile and ran pod install, it finished with no error, all good.
Using import Crittercism gives No such module error. I looked into the Pods/ directory, there source code is there; however, the Pods project doesn't have a target called Pods-MyProject-Crittercism (but it does have targets for each dependency).
Build keeps failing because the import isn't found. What am I doing wrong?
PS: I'm using use_frameworks! directive in my Podfile, and I have another obj-c library that works fine, so I don't know why this one isn't working.
While this is not a general answer, I have found that:
Not using #use_frameworks
Using a Bridging-Header.h containing #import "Crittercism.h"
Not importing CrittercismSDK in the Swift class, but merely executing Crittercism.enableWithAppID("appId") does the trick.
See if below steps helps in your case. What version of pod/Xcode in use? It will be great if you can share your pod file, thanks.
Install dependencies using Cocoapods and the use_frameworks! flag.
As you need to use a Objective-C dependency, create a Bridging header. You can create one easily by importing an Objective-C class to your Swift project, than remove it (the wizard should ask you if need a bridging header). Otherwise, create a new header file. Then, navigate to your target configuration and enter the name of your file in Swift Compiler - Code Generation > Objective-C Bridging header.
Still in your target configuration, add a new entry in Search Paths > User Header Search Paths: Pods as value and flag it as recursive.
Remove any import instruction from your code, relative to your Objective-C library.
Build your project. You should have a success.

ModuleName-Swift.h file not found inside of CocoaPod Project

Issue
I have a CocoaPod project created using pod lib create. There are some Objective-C classes that can be used inside the example project — I am now trying to add a NSObject subclass written in Swift.
The Swift class was created and the bridging header was added to the classes directory.
I try to import the Swift bridging header into the .m file of another class inside the pod project:
#import "PROFlyoutViewController-Swift.h"
When I compile I get 'ModuleName-Swift.h' file not found
When the import statement is:
#import <ModuleName/ModuleName-Swift.h>
or
#import "ModuleName/ModuleName-Swift.h"
it compiles and is usable!
But...
I return to the file, indexing runs (I assume) and all of a sudden I get:
ModuleName/ModuleName-Swift.h file not found.
Autocomplete is broken on that file and any use of that class shows a warning.
Question
When using trying to use a Swift Bridging header within the files of a pod project, how should it be imported? What do I need to do in order to get autocomplete working and compilable?
Try using #import PROFlyoutViewController; instead as it is an external module.
See the below Example of my TableDemo project
To import Swift code into Objective-C from the same target
Import the Swift code from that target into any Objective-C .m file within that target using this syntax and substituting the appropriate name:
#import "TableDemo-Swift.h"
The Swift files in your target will be visible in Objective-C .m files containing this import statement. For information on using Swift from Objective-C code, see Using Swift from Objective-C.

Resources