How to use c++ template class in objective C - iphone-sdk-3.0

I want to use a Template class of C++ in my Objective C project.
I have read that it is supported.
When I try to import the template class that is written in C++ I get lot of errors like
Cannot find protocol declaration for 'class'
etc..
Can anyone give me a simple example of this.
Waiting for reply.

You are putting the objective c++ code in a .mm file? You need to use .mm files to tell the compiler its allows to parse c++ constructs in addition to objective-c and c.
You can't just change the name of a header file from .h to .mm - the name of the file containing the #include / #import directive needs to change.
// file: main.m
#import "cppclassdef.h" //will not work
#import "cppclassdef.mm" // also will not work. additionally will confuse XCode which will try to compile the .mm file by itself.
// file: main.mm
#import "cppclassdef.h" // this is how to do it.

Related

Importing a Swift class of a framework in Objective-C (test) code

I'm trying to import a Swift class from my framework MyFramework in my unit test code:
In my Objective-C test file, I do:
#import MyFramework;
This line causes a compilation error:
Include of non-modular header inside framework module 'MyFramework': 'src/core-ios/MyFramework/Classes/MyFramework-Bridging-Header.h'
inside the "MyFramework-Swift.h" file. In that file, I can see a plain import from
#import "src/core-ios/MyFramework/Classes/MyFramework-Bridging-Header.h"
I'm not sure what to do. My framework "defines" modular headers, but it sound like the problem is that bridging header file which is included with its entire path rather than just doing #import <MyFramework/MyFramework-Bridging-Header.h>.
Any help appreciated!
While calling swift class in objective c
In Bridging-Header add #import "myClass.h". This is objective c class
where you are using swift class.
In myClass.m add #import < yourProjectName_swift.h >
In myClass.h or myClass.m add "#swiftcalss;"
first or second point may cause to your error.

Different C++ include statements throws errors in the Objective-C header

I'm trying to use OpenCV in Objective-C++ code which I will call from Swift. First I used this answer for connecting Objective-C and Swift. So after these manipulations I get three files:
Bridging-Header.h:
#import "opencvtest.h"
opencvtest.h:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#include <opencv2/core/core.hpp>
//#include <dlib/image_loader/load_image.h>
#interface CustomObject : NSObject
- (cv::Mat)cvMatFromUIImage:(UIImage *)image;
#end
opencvtest.mm:
#import "opencvtest.h"
Error:
/Users/user/Documents/XcodeProjects/Swift_HelloWorld/Swift_HelloWorld/Swift_HelloWorld-Bridging-Header.h:5:9: note: in file included from /Users/user/Documents/XcodeProjects/Swift_HelloWorld/Swift_HelloWorld/Swift_HelloWorld-Bridging-Header.h:5:
#import "opencvtest.h"
^ /Users/user/Documents/XcodeProjects/Swift_HelloWorld/Swift_HelloWorld/opencvtest.h:23:4: error: expected a type
- (cv::Mat)cvMatFromUIImage:(UIImage *)image; ^ <unknown>:0: error: failed to import bridging header '/Users/user/Documents/XcodeProjects/Swift_HelloWorld/Swift_HelloWorld/Swift_HelloWorld-Bridging-Header.h'
Also, if I add line #include <dlib/image_loader/load_image.h> in the opencvtest.h then I get:
/usr/local/include/DLIB/string/string.h:7:10: error: 'sstream' file not found
#include <sstream>
^
<unknown>:0: error: failed to import bridging header '/Users/user/Documents/XcodeProjects/Swift_HelloWorld/Swift_HelloWorld/Swift_HelloWorld-Bridging-Header.h'
I want to note that all these errors occurs only when I put these include statements in this header, when I used it in .mm file all works fine. But I need them in the header for declarations.
So, how can I fix it?
The interface of your Objective-C objects usable in Swift and thus visible in the bridging header cannot reference any C++ types. You need to write an Objective-C++ wrapper in such a way that C++ types are only mentioned in your .mm files and in headers used in the .mm files. You can hide the parts of the interface that use C++ by using extensions in Objective-C++ code. Please see the following question and questions referenced there for examples of how to do that:
objective C opencv wrapper for swift project does not see STL headers

How can I add forward class references used in the -Swift.h header?

I'm integrating Swift code into a large Objective-C project, but I'm running into problems when my Swift code refers to Objective-C classes. For example, suppose I have:
An Objective-C class called MyTableViewController
An Objective-C class called DeletionWorkflow
I declared a Swift class as follows:
class DeletionVC: MyTableViewController {
let deleteWorkflow: DeletionWorkflow
...
}
If I now try to use this class by importing ProjectName-Swift.h into Objective-C code, I get undefined symbol errors for both MyTableViewController and DeletionWorkflow.
I can fix the problem in that individual source file by importing DeletionWorkflow.h and MyTableViewController.h before I import ProjectName-Swift.h but this doesn't scale up to a large project where I want my Swift and Objective-C to interact often.
Is there a way to add forward class references to ProjectName-Swift.h so that these errors don't occur when I try to use Swift classes from Objective-C code in my app?
You can create another header file that forward declares or imports the necessary classes, and then imports ProjectName-Swift.h. For example, create a file named ProjectName-Swift-Fixed.h with the contents:
// ProjectName-Swift-Fixed.h
// Forward declarations for property classes
#class DeletionWorkflow;
// Imports for superclasses
#import "MyTableViewController.h";
#import "ProjectName-Swift.h"
Then, instead of #import "ProjectName-Swift.h" in your codebase, use #import "ProjectName-Swift-Fixed.h.
This is a little silly, but it sounds like your "workaround" is what Apple intended, at least for now. From the interoperability guide:
If you use your own Objective-C types in your Swift code, make sure to import the Objective-C headers for those types prior to importing the Swift generated header into the Objective-C .m file you want to access the Swift code from.
In this devforums thread, someone mentioned they already filed a bug in Radar. You probably should too.

Compile cxx files in xcode

I am new at IPHone development , I included a library with .cxx as implementation class and a .h header but still i get error at compiling , Please any help?
I get errors at lines like below
class StackEvent;
The Objective-C class that is including any C or C++ code must be renamed with a ".mm" suffix rather than ".m" in the implementation class.
Any Objective-C file (.m) that includes any header containing C++ code, either directly or by chained #includes, will likely fail with compile errors.
You need to rename all your .m files to .mm if they can "see" your C++ header.
If that causes too much renaming, then you'll have to limit your #includes, for example by take the C++ #include out of your header file and putting it only in the .mm files that need it.

__OBJC__ in Objective-C

What does __OBJC__ mean in Objective-C?
#import <Availability.h>
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif
It means the objective C compiler is being used. So you can create hybrid header files that can be used when compiling objective C or C or C++.
You could use it in a header file like this, if you wanted to publish a header file that defined an objective c object that you wanted to make available to c and c++ programmers/code :
#ifndef MYHEADER_H
#define MYHEADER_H
#ifdef __OBJC__
// Put objective C things in this block
// This is an objc object implemented in a .m or .mm file
#implementation some_objc_object {
}
#end
#endif
#ifdef __cplusplus
#define CLINKAGE "C"
// c++ things that .m or .c files wont understand go in here
// This class, in a .mm file, would be able to call the obj-c objects methods
// but present a c++ interface that could be called from c++ code in .cc or .cpp
// files
class SomeClassThatWrapsAnObjCObject
{
id idTheObject;
public:
// ...
};
#endif
// and here you can declare c functions and structs
// this function could be used from a .c file to call to a .m file and do something
// with the object identified by id obj
extern CLINKAGE somefunction(id obj, ...);
#endif // MYHEADER_H
This looks like your precompiled header file.
The precompiled header is shared between all C-dialect files in your project. It's as if all your .c, .cpp, .m and .mm files have an invisible #include directive as the first line. But the Cocoa header files are pure Objective C - trying to include them in a C/C++ source will yield nothing but syntax errors aplenty. Thus the #ifdef.
If your project only contains Objective C files (.m/.mm), which is the typical case, the #ifdef is not really necessary. But Xcode, which generated this header in the first place, protects you all the same.
Even if it's not a PCH file, this #ifdef only makes sense if the file is to be included from both Objective C and plain C/C++. But it does not hurt regardless.
Its just a macro symbol. In this case if that symbol is defined then your program should import the Apple Cocoa frameworks (Foundation and AppKit).
This woudl be the case if you were developing an objective-c / cocoa application. In other words, if you were developing a C++ / carbon application, the __OBJC__ symbol would not be defined and those objective-c dependant frameworks would not be imported.