Xcode projects allow you to mix Objective C, C++ and Swift in the same project based on these rules:
.m files can contain a mix of ObjC and C
.mm files can contain a mix of ObjC and C++
.swift files can contain only Swift
But you can have both .swift and .mm files in the same project. You need two things: A Bridging Header file that Swift uses to access the ObjC/C++ files. You also have to generate a -Swift.h file so that your ObjC/C++ files can talk the other way.
The current plugin.mm file is heavily organized around C++ calling ObjC methods.
Engineering is working on a Swift based “App” project but it’s not ready yet.
I know Renato started this thread about plugins, but Oliver, what are you wanting to use Enterprise and Swift for? Plugins or just having your SDK app talk to native things?
myApp communicates with Objective-C using Corona Enterprise.
Everything is fine at this point.
But now I need to call Swift functions from myApp. (someone developed functions in swift that I need to call)
I made a simple Xcode Objective-C project in which I call Swift function from Objective-C. It works
//TestClass is a Swift file import Foundation @objc class TestClass : NSObject { var someProperty: AnyObject = "Some Initializer Val" func someFunction(someArg:AnyObject) -\> String { var returnVal = "You sent me \(someArg)" return returnVal } } //Put this code at the top in AppDelegate.m #import "TestCall-Swift.h" //Put this code in didFinishLaunchingWithOptions method in AppDelegate.m TestClass\* myOb = [TestClass new]; NSLog(@"MyOb.someProperty: %@", myOb.someProperty); myOb.someProperty = @"Hello World"; NSLog(@"MyOb.someProperty: %@", myOb.someProperty); NSString \* retString = [myOb someFunction:@"Arg"]; NSLog(@"RetString: %@", retString);
I can send you this entire project if you want to try it.
But I need to call Swift functions from Corona SDK
And at this point, I didn’t succeed to do that. :wacko:
You cannot make plugins in Swift. Plugins use static .a libraries. Swift cannot produce these. Swift can create frameworks, but that’s not something we can use in Corona SDK. It will probably be a big engineering effort to get there.
There is no good Swift-Lua interface. Lua was built for C++. In Xcode, you can mix Objective-C and C++ (and C for completeness) in the same files. This makes Lua->C++ easy and you can make Objective-C calls as needed.
What you can do is have a Swift class and have a C++ wrapper. In other words lets say you have a Lua call:
library.show()
That would call a C++ function called show(). It would then take parameters you passed from Lua and turned into Objective-C data types like NSDictionary, NSString, etc. and it would call your Swift classe’s show() method.
We are going to produce a sample project that makes this all possible, but it needs to go through a code review before we can upload it to our Github repo. So we might have something soon if we don’t find further issues.
The project is setup to basically make a non-plugin plugin if that make sense. Its called Plugin in the code and gives you plugin like access (i.e. local library = require( “plugin.library” ) in your Lua code), it’s just not a distributed standalone plugin.
Looking forward to the sample. I am currently trying to integrate the Swift portion of the ios-Charts (https://github.com/danielgindi/Charts) into iOS.