私は現在、iPhone アプリケーションで C++ で記述したコードを使用しようとしています。Objective-C++ を使用して C++ コードをラップすることについて読んだことがあります。呼び出しようとしている C++ 関数は、引数 2 std::string を取り、std::string を返します。
// ObjCtoCPlusPlus.mm
#import <Foundation/Foundation.h>
#include "CPlusPlus.hpp"
#import "ObjCtoCPlusPlus.h"
@implementation Performance_ObjCtoCPlusPlus : NSObject
- (NSString*) runfoo: (NSString*)list
{
std::string nodelist = std::string([[list componentsSeparatedByString:@"*"][0] UTF8String]);
std::string lines = std::string([[list componentsSeparatedByString:@"*"][1] UTF8String]);
std::string result = Performance_CPlusPlus::run(nodelist, lines);
return [NSString stringWithCString:result.c_str()
encoding:[NSString defaultCStringEncoding]];
}
- (void) exp
{
Performance_CPlusPlus::explanation();
}
@end
Swift から目的の C++ 関数を呼び出しています
// I am calling the function from the viewController.swift
@IBAction func button(sender: AnyObject) {
let z : String = "0 1/1 2";
let q : String = "a b Y";
let x = Performance_ObjCtoCPlusPlus.runfoo((q + "*" + z) as NSString)
}
エラー: タイプ NSString の値を予期される引数タイプ PerformanceObjCtoCPlusPlus に変換できません。私が得ているエラーは、swift の String 型を NSString* に変換できないためだと思います。この問題を解決するための回避策はありますか?