Using data types from a objective-c++ framework in a swift datamodel - ios

I want to use the openCV2 framework in my iOS app, however, I am more comfortable with swift. I can read and understand obj-c well enough to understand the framework and write the methods I need from it in a bridging header, but I'm not comfortable writing the whole app in obj-C. Unfortunately, there are some data types, (cv::Mat, cv::MserManager, etc) that I will need to use in my datamodel, or possibly elsewhere. Is there a way to include datatypes in my bridging header so that I can work with them in swift?

You cannot use C++ types in code called from Swift. However, you can use them in Objective-C++ files, the ones that have the .mm extension. You can mix Objective-C and C++ code in Objective-C++, and can expose Objective-C methods that don't reference C++ in their declarations to Swift via the bridging wrapper. These functions can still use C++ in their implementations, which are not visible in Swift via the bridging header.
You also need to be careful about the language linkage (remember extern "C"?).
Here are some answers that provide examples:
1) Video processing with OpenCV in IOS Swift project
2) Include C++ header file in Swift
3) How to access Swift-objects from a c++ class?

Unfortunately, you cannot bridge C++ and Objective-C++ directly into Swift. On the bright side, you can still work with the openCV2 framework in your app, but you'll need to write C or Objective-C wrappers for your types as described in a related question here: Can I mix Swift with C++? Like the Objective - C .mm files

Related

importing Pure Swift Framework in Objective-C Project

I want to import a Swift framework called "Beethoven" into Objective-C project. I import this framework via cocoapods.
The problem is that framework is written in pure swift. Since the classes are not subclass of NSObject they can't be used directly in my objC classes.
I am newbie in Swift and intermediate in objC, but I think there may be 2 solutions for that:
1-Modifying the whole library: which is probably not an optimal solution.
2-Using a new class which behaves as an interface between swift framework and my objC code. I think second solution will be a better alternative in terms of time and effort needed.
Actually below given post explains modifying the pure swift classes but I don't know how to apply this in my case.
How to use Swift non-NSObject subclass in Objective-C
As I mentioned I am not a very capable programmer and there may be better solutions for that problem. I would be grateful if someone can help me and suggest the best solution for integrating pure Swift framework into my objC project.
NSObject is usually not the only problem. Swift has many features that are not supported by Objective-C. To name a few: swift value types (struct's), swift enums with attached values. If a library uses any of those, it will not be possible to auto-generate an Objective-C header (.h) for it.
Adapting the code of the library on the spot might work, but it is likely more work long term in case if you ever need to update that library again.
Your 2nd approach sounds better: create a layer that is compatible with Objective-C on top of the library which exports methods that need to be exported with #objc and adapts the types. If you go that way consider making a PR contribution to the original library so that everyone could use it in Objective-C projects, and you share responsibility of updating it when the swift code changes.
The 3rd approach would be actually to rewrite some parts of you app to Swift. That might or might not be easier depending on the size of the part of the app that is using the library and how well it is isolated from the rest of the app.

Swift framework for Swift and Objective C projects

I am building a new framework. The project is to be coded into Swift language, however the clients using this framework have the freedom of using either swift or Objective-C framework.
The question is how do I start. There could be numerous issues like
using structs in swift code but it cannot be made available in
objective C framework.
optionals are missing objective c
Even if I
write different set of files for Swift and Objective C, how will I
map them onto different frameworks under the same project.
Enums with other than Int as rawValue can't be used.
Tuples would not work
I know there have been a few questions around this but none have any satisfactory answer.
PS - Any link to a tutorial or blog would be super helpful too
I did this and got some unexpected results: I have trouble integrating the framework in Swift application. Objective-C works just fine.
You mentioned some of the caveats here.
I suggest doing this iteratively while writing test application in Objective-C which uses all the features. This way if there is some feature that does not cross Swift to Objective-C boundary well, it will be discovered as early as possible.
Your remarks about issues are generally correct with one small exception: optionals are not missing from Objective-C, they appear as nullable/nonnull modifiers on variables and method parameters. Although this does not replace optionals fully, it helps detecting issues early in the process.
Here is a random list of some other issues I discovered:
Bridging between Swift Error and NSError used in Objective-C. The conversion is not always as smooth at it could be, so better use NSError in exported code.
If you mix Objective-C and Swift in your framework, you cannot use bridging header, instead using modulemap files which tend to turn pretty large and complex.
Since you cannot embed frameworks inside a framework, you have to make sure that the application sets ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES flag for its target. This has to be clearly indicated in the documentation. Also, when creating fat library for your framework, you have to strip these files from the distributed SDK.
And, as I said in the beginning, I still have no success using the resulting mixed language framework in Swift application.
Hope, this will add to your list of things to take into account when developing the library.

using an ios framework for native c++ code for ios

I have written a c++ library that needs opencv which is an image processing library. I want to now use this c++ library on ios. To do that I am going to copy my code to a mac and build to produce a cocoa touch static library.
Since, this has a dependency on opencv, I downloaded its ios framework. But now I am confused whether a framework can be used from c++ code or just from objective c/c++ ? Do I have to recompile this library so that i get c++ libraries or I can use the framework in my c++ code?
Yes, it can be used with c++. You will have to make sure you Type is set to "Objective C++ Source" for where you are making the framework calls.
I mix my C++ code with frameworks all the time.
Note this goes both ways. If you have Obj-C interacting with C++, you'll need to either have the file be a .mm or be of the "Objective C++ Source" Type.
The Type selection is in the File Inspector for files.

Using SNMP++ library in application written in Swift

I'm new in Swift and I would like if someone could tell me how can I use SNMP++ lib (written in C++) in a simple iOS application written in Swift? how to import the lib and begin coding? Thank You.
In 2016, Dariusz Stojaczyk wrote C code to construct probes and deconstruct response of SNMP v1 messages hidden in a project focused on BER. That project contains the code necessary to take one or more SNMP OIDs and construct a full SNMP message, then take a response and decode it, but has no networking code to actually do this.
I took this code and added an ObjectiveC wrapper that can be used to use the existing code to construct a SNMPv1 message, send it to an endpoint using a Socket, and receive a response with a user settable timeout using a dispatch block.
This code is far simpler than any of the big libraries that offer additional services like walking, v2/v3, and traps. If all you want to do is read one or more OIDs, then you should appreciate this code.
Objective-C can be mixed with C++.
That's called Objective-C++ (.mm files)
So if you can get the code into those files, in theory you can create a bridging header to deal with linking your swift files: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
Swift 2.2 does not work with C++, and neither will Swift 3. So it's not coming any time soon which means you simply can't develop a Swift app with a C++ library.
However Objective-C works with C++, so although you don't have to write your entire app in Objective-C, any code that deals with a C++ library will have to be written in Objective-C (or in C...).
You can then link your Objective-C code to you Swift app with a bridging header.

Swift: How do you use an Objective-C function from a linked library?

Background
I'm new to iOS development and playing with Swift for the sake of learning. As a small challenge, I'm trying to get the SSID of network to which a device is currently connected.
This is well-covered ground on Stack using Objective-C: iPhone get SSID without private library, for example ... but the Objective-C / Swift dance is causing me some conceptual challenges.
Specifically, the suggested solution (discussed in the above-linked post) is to call the function CNCopyCurrentNetworkInfo() -- but, according to Apple's documentation, this function is not available in Swift.
So far
I've included (I think correctly) SystemConfiguration.framework by adding it to the Linked Frameworks and Libraries of the project. I've also brought it into the View Controller using import SystemConfiguration.
Questions
Do I need to also use import SystemConfiguration, or is that already done due to including the framework with my project?
Is there another / better way to include the SystemConfiguration library?
The big one: How, once the required library is imported, do you call the Objective-C function from the library?
Thank you!
I know this is not directly answering your question, but it's an answer to your problem.
You can do everything in swift in your case.
After importing the library,
import SystemConfiguration.CaptiveNetwork
You can immediately call:
CNCopySupportedInterfaces()
and it will work.
Confirmed in Xcode 6.3.2.
Apple's interoperability book (from Understanding the Swift Import Process):
Any Objective-C framework (or C library) that’s accessible as a module
can be imported directly into Swift. This includes all of the
Objective-C system frameworks—such as Foundation, UIKit, and
SpriteKit—as well as common C libraries supplied with the system.
Regarding your specific questions:
The opposite is the case: you do not need to manually include Apple's frameworks. Xcode will automatically make them available to you given a valid import statement (such as import SystemConfiguration in your case), even – or rather: especially – in a playground!
ditto...
Given the above import statement, SystemConfiguration is indeed imported since you can call its functions (e.g. SCCopyLastError() // --> __NSCFError) and access its constants (e.g. kCFErrorDomainSystemConfiguration // --> com.apple.SystemConfiguration). Unfortunately, CaptiveNetwork does not seem to be imported with it (e.g. CNCopySupportedInterfaces() // --> Use of unresolved identifier).
You should be able, however, to use this framework on the Objective C side and simply call your own wrapper functions from Swift. You just need to remember to include them among the imports listed in your bridging header (see Swift and Objective-C in the Same Project for more about bridging headers).

Resources