2

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 :(

MB41
  • 552
  • 2
  • 8
  • 24

2 Answers2

0

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

Mostafa Berg
  • 3,211
  • 22
  • 36
  • This, +1. The question is mind boggling, if you are going to ask anyways, which is perfectly fine, don't get me wrong, at least ask the right question that solves the problem! – JustSid Dec 04 '14 at 21:31
  • @Mostafa Torbjørn Berg Thank you! I didn't know there's documentation on how to read and write to file in swift that's why I was going for C++ since I'm much more familiar with it. – MB41 Dec 04 '14 at 21:40
  • Do you by any chance know how to get the directory path of your project? – MB41 Dec 04 '14 at 22:57
  • @Billy no problems !, there's very good documentation at `http://developer.apple.com`, I assume now you're asking for iOS ? if so you should know that in iOS apps are sandboxed and you can't write or read anything outside your app's directory, you can only read/write in a place called Document directory, here's a question that perfectly answers that ! http://stackoverflow.com/questions/24055146/how-to-find-nsdocumentdirectory-in-swift – Mostafa Berg Dec 04 '14 at 23:36
  • I figured it out: `let dir = NSHomeDirectory().stringByAppendingPathComponent("Documents")` Do you know how I would be able to read line by line? Using `var textinfile = String(contentsOfFile: myfilepath!, encoding: NSUTF8StringEncoding, error: nil)` will return ALL the text in the file. I want to be able to read a line examine it then write it to a file, read the next line and write it into the file on the next line. @MostafaTorbjørnBerg – MB41 Dec 04 '14 at 23:54
  • You should post any more questions you have as separate questions so people can answer, it's a common mistake to post questions in comments, as soon as you create a new question you'll get much more exposure :) – Mostafa Berg Dec 08 '14 at 20:17
-1

Use following instead:

#import <string.h>
gagarwal
  • 4,224
  • 2
  • 20
  • 27
  • 1
    I just tried that and when I type: void printString(string text); It says unknown typename 'string' :( – MB41 Dec 04 '14 at 21:26