How to import opencv2 framework in iOS Swift - ios

I downloaded the latest pre-built opencv2.framework from the OpenCV SourceForge page. then in Xcode6-Beta3, I added opencv2.framework as a required linked framework under the "General" tab of my Swift project settings:
This is the structure of the framework after the framework is added to the project:
If this was a Objective-C project I could add the following import statement:
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif
In my ViewController.swift file, if I do something similar I get a "no such module" error:
I tried the following variations that result in an error:
import opencv2/opencv.hpp
import opencv2/opencv
import opencv
import "opencv2/opencv.hpp"
import <opencv2/opencv>
What is the correct way to import opencv in to my Swift project?

It's easiest if you start with a simple project all ready to compile and run out of the box.
This xcode project works with Swift, XCode 6.1, OpenCV 2, and is MIT licensed:
http://whitneyland.com/2014/10/opencv-swift.html
Here's one I haven't tried yet but there are so few still it's worth looking at:
https://github.com/foundry/OpenCVSwiftStitch

You will need to create a bridge from C++ to Objective C or plain C. After that, you will need to include/import the bridge's header into an "Objective-C Bridging Header", which Xcode should have generated when you added a Swift source file in an existing project.

Related

'framework/header1.h' file not found in Swift Package Manager

I have an existing Objective C framework that is using the Carthage for building.
I am supporting the Swift PM for the existing Objective C framework and implemented the Swift PM using the below command in the current project.
swift package init
This command is initializing the Swift PM and creating the package.swift and I have configured it.
When I ran the Swift Package Manager in Xcode I got the error "framework/header1.h" file not found,
When I changed the import statement as
#import framework;
it's working fine for Swift Package Manager, but when I opened the .xcproject file of the framework in Xcode, its give me an error "Module framework not found".
Is there any way to import the header or framework, so that it will work fine for both Swift PM as well as Carthage?
After burning head for more than two days found two solutions for the above problem.
You can use the preprocessor definition SWIFT_PACKAGE
#if SWIFT_PACKAGE
#import "header1.h"
#else
#import <framework/header1.h>
#endif
2. Add the public header files in
Project setting > Build Phases > Headers > Public
Add publicHeadersPath in the Target section and make sure that Swift Package Library name in Package.swift must be the same as per the project framework.

Objective c and Swift using in Framework in Xcode

I've my own old framework which is only compatible in objective c, but Now I open in new xcode and added few swift classes (Xcode not gave me option to create bridging as this do in normal App when you add swift class in objective c project), Also Xcode gave me suggestion to update build setting and I updated those and currently I'm on Xcode 8.2.1. Now I try to build but I am getting few error as you can see in picture . I googled and found solution to delete derive data and quite xcode and open and clean. I done Everything but didn't workout.
Is bridging can be possible in framwork.
If not possible then how can I achieve.
Need help and suggestion.
When you are mixing Swift/Objc inside a framework, you can import Swift code for Objective-C use and import Objective-C from Swift.
First, ensure that Define Settings is set for the current framework target.
To import Swift into Objective-C:
In each Objective-C file that requires the Swift code, import the generated Swift header with the complete current Framework Path : #import <Framework/Framework-Swift.h>
To import Objective-C into Swift:
Ensure you have an umbrella header. The path can be found inside the Build Settings of the target. In this header, import all the needed Objective-C headers like normally in Objective-C: #import "Header"
Source: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-ID122

Can't reference framework .h header files from my Swift controller

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

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)

How can I make CorePlot work with swift?

I read the tutorial on how to use core plot (http://www.raywenderlich.com/13269/how-to-draw-graphs-with-core-plot-part-1)
The project I am working on is written in swift.
I did everything to import the CorePlot library.
To make sure I did it right, I created a new objective C file and called
#import "CorePlot-CocoaTouch.h"
it worked in the test objective-c file (built without errors)
I then put the import statement in the bridging header file, and got 27 errors ALL saying '... the current deployment target does not support automated __weak'
From this post (CorePlot in Swift can not call “numberForPlot”),
I know I can make core plot work in my swift project somehow.
Can anyone recommend something ?
Thank you for helping.
I followed these steps to import the library :
Static Library Install
You can also just copy the Core Plot library directly into your project in binary form.
Copy the CorePlotHeaders directory to your Xcode project
Copy the Core Plot library to your Xcode project.
Open your apps Target Build Settings, and for Other Linker Flags include this:
-ObjC
(-all_load used to be required as a linker flag, but this is no longer needed in Xcode 4.2)
Add the QuartzCore framework to the project.
Add the Accelerate framework to the project (release 2.0 and later).
Change your C/C++ Compiler in the project build settings to LLVM GCC 4.2 or LLVM 1.6.

Resources