I have an xcode 6 swift project and I am trying to use C++ with it. In my project on the left I clicked 'new file' and I chose empty C++ file. Then I click yes and xcode generated Bridging-Header.h file.
In my new .cpp file I have #include <string>. I tried to compile the file and everything compiled fine. Later I realized that I forgot to include the .cpp file in the bridging-header.h because my swift files didn't have access to a function in my .cpp file. When I added the #import "cplusplusfile.cpp" in the bridging-header file, I get this error on this line:
#include <string> (!)'String' file not found
my goal is to get a string the user enters in a UITextField and send that string from my Swift files to a function in my C++ file so then I can use C++ to write that string to a file (I don't know how to read/write files in swift and couldn't find much on it..).
Would anyone know possible solutions? I looked online and other questions. One of the solutions was to change my cplusplusfile.cpp to cplusplusfile.mm. Unfortunately it didn't help :(
I'll answer something completely different cause I know you're going in the wrong direction, using C++ just to write a string to file is running around in circles, you want to use NSString and write that string directly to a file, in swift you can use the method
func writeToFile(_ path: String,
atomically useAuxiliaryFile: Bool,
encoding enc: UInt,
error error: NSErrorPointer) -> Bool
And give it a string which is the filename and the string to write get that by reading it directly from your UITextField by using myTextField.text, it should write that to file right away.
Good luck learning swift ! :)
also here's the class reference for NSString
Use following instead:
#import <string.h>
Related
This question already has an answer here:
Force Header Files to Compile as C++ in Xcode
(1 answer)
Closed 2 years ago.
I'm trying to use a 3rd party sample code as part of my Objective-C application.
However, when trying to compile my project I get a lot of compiler errors.
The objc syntax that is used in the sample is quite strange and I believe that it is compiled as something else within the project.
For example, as part of the header file the struct definition has default values:
File.h
struct Options
{
int count = 100;
}
...
In their project, the above header file would compile just fine, but when I try to compile it I see an Xcode error:
Expected ';' at end of declaration list
I'm not an objective-c expert, but from what I've read and also found as part of other posts data types cannot have default values, but somehow it compiles fine in the Xcode sample app.
Please let me know if you need more info or other examples of what would compile fine in their project and doesn't work when I copy and paste the code into mine.
Edit:
Force Header Files to Compile as C++ in Xcode
Individually header files doesn't compiled. Compiled source files where they included. If you include them in .c they will be compiled like C, if in .m - like Objective-c, if in .cpp - like C++, if in .mm - like Objective-C++.
My issue was that I was using this header file into a .m file which compiles it to an Objective-C standard. When I change the extension to .mm everything worked fine.
When you think of objective-C as actually being C its more clear why defaults are not set in .h files
This does not apply to the rules of default sizes which are needed when no flexible memory allocation for a data type is available by default, like for (as is) most data types in C.
Even this two sentences already show the difficulty to distinguish what is meant with "default".
Which is why you end up with definition of initiation processes and functions to declare what exactly is "default" in the other file.
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
int count;
} Options;
typedef struct {
int foo;
Options bar[100];
} Foo;
struct Bar {
Foo lotsof;
}
void MyInitiationFunction(Bar *b);
// definition of MyInitiationFunction in .m/.c file
#ifdef __cplusplus
}
#endif
but there is the concept of enum that can trick your mind where defined enumeration default states are what you build some stuff on, which is not a value.
To set up Receipt-Validation in my iOS app, I am now following this tutorial:
https://www.raywenderlich.com/9257-in-app-purchases-receipt-validation-tutorial
and reading at this point: Loading the Receipt.
While reading and attempting to understand what is going on I also try to integrate the code in my own app, by doing so getting a hands-on understanding of the process.
Here is one problem I am hitting at this moment:
On this line of code:
private func loadReceipt() -> UnsafeMutablePointer<PKCS7>? {
I get this error message:
Use of undeclared type 'PKCS7'
After searching the net and trying a few things, I guess it is related to the use of the use of the ReceiptVerifier-Bridging-Header.h file. But I am not sure how to set it in the project.
I will be glad if anyone has some tip allowing me to move forward.
Thanks in advance!
In case this can be useful, here is the meaningful contents of the bridging header file (ReceiptVerifier-Bridging-Header.h):
#import <openssl/pkcs7.h>
#import <openssl/objects.h>
#import <openssl/evp.h>
#import <openssl/ssl.h>
#import <openssl/asn1_locl.h>
The problem is that I had simply copied the bridge-header file, without doing the proper setting as explained here:
https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_objective-c_into_swift
I tried calling UnitySendMessage method in my iOS Framework project using swift language but it is showing this error-
Use of unresolved identifier 'UnitySendMessage'
Here is the code snippet for my swift file-
import Foundation
#objc public class Example : NSObject{
#objc open static let shared = Example()
#objc open func printMsg(){
print("\(#function) is called with message:");
UnitySendMessage("CallbackTarget", "OnCallFromSwift", "Hello, Unity!");
}
}
Getting stuck here, can you please tell me what am I missing?
check Answer here.
UnitySendMessage is in libiPhone-lib.a, and as far as I can tell, the
Unity folks don't include any header that declares it.
I just added the following to my code
extern void UnitySendMessage(const char *, const char *, const char
*);
If you look at the generated AppController.mm, they basically do the
same thing for the various UnitySendXXX functions, rather than
including a header.
Having said that, while you will be able to build a library that calls
UnitySendMessage, that library probably won't work unless it's linked
into a Unity project, since UnitySendMessage is going to require more
than just libiPhone-lib.a to actually work.
Just add this code to your Bridging-Header.h file:
#import "UnityInterface.h"
Make sure that your files are in the XCode project that was build from Unity, so the actual UnityInterface file that is created by unity is present.
Worked for me :)
This is a partial duplicate of Import a file in bridging-header which imports Swift header but I encounter the same issue as Rich
But what about enums declared in Swift? :(
I am porting an Obj-C iPad app to the iPhone. However I am a Swift developer who would really rather not rewrite existing functionality; replacing the UI instead.
I created a new target for the iPhone version. In my bridging header I import an obj-c class that uses #import "ProjectName-Swift.h". Since this file is autogenerated it doesn't exist when I build this new target. The linked answer is to add a #class but the legacy code makes use of an enum which is now giving the error "Expected a type".
// File that I am currently importing
-(void)setSmileyType:(SmileyFace)type andDelegate:(id<NumberRatingDelegate>)delegate;
// This line now throws an error "Expected a type"
//File that was previously auto imported
#objc public enum SmileyFace: Int {
#objc enum in Swift is exposed as a C-enum in ProjectName-Swift.h .
(Using a macro SWIFT_ENUM.)
You can put something like this into your Objective-C header files which are using the Swift enum:
typedef enum SmileyFace: NSInteger SmileyFace;
(Same as the first part of the generated code with the macro SWIFT_ENUM.)
Im pretty new to Xcode/Swift and want to install a third party library(SharkORM).
I drag'n'dropped the folder "SharkORM" into XCode and selected "Create groups". Then i created a file "Swift-Bridging-Header.h" and typed in #include “SharkORM.h” as described in the documentation. When i hold CMD and click on it it leads me to the interface declaration(good!?). Now when i try to use it: class MyClass: SRKObject { ... } i get an error: "Use of undeclared type 'SRKObject'". But i can CMD+click on it which leads me to the interface declaration again.
I tried to install with Cocoapod, too, with no success.
As posted on GitHub, it sounds like the header file you created has not been added to the build settings as the chosen bridging header.
That is the most likely scenario leading to the object not being defined in your swift code.
Check, if SharkORM.h contains SRKObject declaration. If not, find header file with it and place it to bridging header too