Plugin development using Swift

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.

Rob

Thanks Rob!

As I’m not an ObjC or Swift expert, I think I have to wait…

Do you know when this Swift based “App” project will be ready?

Best

Olivier

No I don’t.

Rob

Ok Rob,

Do you think it would be possible to have this in a “special support way”?

I need this stuff very quickly…

Thanks

Olivier

Olivier,

using a Bridging header is very simple. It is basically a include file to indicate to the swift project which obj-c libs you are importing.

Here is an example:

file “MyProject-Bridging-Beader.h”:

#ifndef MyProject\_Bridging\_Header\_h #define MyProject\_Bridging\_Header\_h #import \<CommonCrypto/CommonCrypto.h\> #import "NSData+AESCrypt.h" #import "NSString+AESCrypt.h" #endif

On this example my project is importing 3 obj-c files. These obj-c are external libraries that I downloaded and added to my project.

The last thing is that for you to indicate to the compiler to use your bridging header file.

You do that by:

On: Target->Build Settings ->  Swift Compiler - Code Generation.  

a ) Make sure that  “Install Objective-C Bridging Header” is set to YES

b ) Make sure that “Object-C Bridging Header” is pointing to your bridging file

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?

Rob

Thank you Renato, Thank you Rob.

 

I have an SDK app, let’s call it  myApp

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:

Thank you for your help

Olivier

So here’s the deal.

  1. 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.

  2. 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.

  3. 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.

Rob

Thank you Rob for your time and your explanation.

I Really appreciate this.

I look forward to trying this non-plugin plugin  :slight_smile:

Olivier

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.

Hi Rob,

any news ?

Thanks

Olivier