I've been tasked with splitting out the model layer of an existing app into a separate framework. I'm trying accomplish this using cocoapods to make a pod which I'll import back into the main app.
The issue I'm having is the app I'm splitting the code out from is a mix of Swift and Objective-c. It's not cleanly split so the Swift code makes use of Objective-c code and the Objective-c code makes use of Swift code.
I'm struggling to understand how to achieve this, or if it's even possible? When you're in a framework the bridging header and the generated *-swift.h don't seem to work the same way. Then pods is another complication on onto.
I've tried to experiment with a simple project here. SPOILER ALERT. It doesn't compile.
https://github.com/Megatron1000/TestMixAndMatch
I've tried to find an example of a cocoapod that's setup in the same entangled language way but haven't been able to. Can anyone point me to one I can use as an example?
I have a (private) pod that contains Obj-C and Swift code. In the podspec I've simply added them like this
s.source_files = 'Project/*.{swift}',
'Project/legacy/*.{h,m}'
Technically in Xcode this project is a framework.
Here you can find the technical details how to combine them in a Project.
Technically if you create a framework you'll need to include the Obj-C headers in the umbrella header of the framework
If you want to use Swift methods in your Obj-C classes you'll need to include them as
#import <ProductName/ProductModuleName-Swift.h>
if you're building a framework or simply
#import "ProductModuleName-Swift.h"
if you're working on a regular project
Perhaps the best approach in this case is to simplify the design of the framework and just rewrite objective-c legacy code info swift. You mention that the codebase is already partially in Swift, which will make it even easier to complete the transition.
Related
My project comprises a C++ core, iOS and Android wrappers (ObjC / Java) and a Python-wrapping-CMake build-system.
C++ works fine. However an Obj-C header file (with .h extension) marks syntax errors everywhere:
Does there exist some extension for handling this? I can't find one anywhere.
Xcode is a complex combination of different applications like interface builder, instruments, device manager and many more these were separate applications at a time so putting all this together in some other IDE wouldn't be easy task however there are few people those are trying something like AppCoda but still too far from the actual product.
I'm making app with using Xamarin.
I have to bind some iOS framework which is written by Swift to CSharp.
Sad news is Xamarin does not support bind Swift framework at this moment, So I almost gave up.
But I have a question.
Header files of the library that I've built to framework looks Objective C. But All source code is written by Swift. (this framework : https://github.com/NordicSemiconductor/IOS-DFU-Library)
What is this?
Is this natural or they did something extra work to make it as Objective C when it's in framework?
This means that I can keep doing binding work?
Please correct me, if you have to.
As far as xamarin is concern, it doesn't matter if a Cocoa lib is written using objective C or swift, or C++ for that matters.
What you'll have to compile the lib into a *.framework compiled library using xcode. So swift or objc, well, doesn't matter.
Simply open the project in Xcode and build it. Obviously, don’t do build and run as you can’t run it (again unless you have a test suite). Now, look under the Products group in XCode and you should see a file called DFULibrary.framework.
Once you have the compiled binary, then you'll have to encapsulate it in a C# (or F#) iOS binding project (again, they say ios binding, not objc binding nor swift binding... guesss why :p ) as explain here : https://developer.xamarin.com/guides/cross-platform/macios/binding/objective-c-libraries/
Now, it's true that they say:
To do a comprehensive binding, you will want to understand the
Objective-C API definition and familiarize yourself with the .NET
Framework Design guidelines.
but I would pin that on a lack of update of the documentation. Just replace ObjC understanding by swift understanding :)
YOu will end up with a C# api anyway.
Enjoy
I have an existing app and I'd like to share a large number of classes from that app within a new app target. I'd like to leave the existing app as-is and treat it as a module (like a framework), importing it into the new app target which will be written in Swift.
Note this is NOT the simple documented case of adding Swift code to an existing Objective-C application target. I would like to see an example of using the module capability in Xcode to re-use code from an existing target in a new Swift target/module.
The existing app is built as an objective-C application (not a framework)
I would like to build a new app in Swift but use some of the non-gui code from the old app in the Swift app.
I have tried several times to see if there is a way I can import classes from the old iOS-objc app but I'm having difficulties using it in the new Swift app target, methods are not being found
I have turned on modules in this source app.
A) I would ideally like to import classes from the existing App target in the project without refactoring the original app.
B) If that is not possible I could potentially factor out the classes that are common into a separate objective-c framework. This I can get to work thanks to this (http://youtu.be/9us3uijFFpo) , but I'd like to see an example of A) if anyone can crack it. That use case is not covered in Apple's documentation.
But I've searched the web and I can't find any detailed examples of how to do this and my many various attempts to do so have been unsuccessful. There are plenty of references to adding Swift files into Obj-C apps, but nothing specifically about this kind of case.
You should read the chapter "Mix and Match" of this book from Apple:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
It shows you how to import objective class to your swift project.
In general you have to do some steps:
add a "Bridging-Header.h" to your project with name: "YourProjectName-Bridging-Header.h"
Place your Obc headers somewhere then import ObjcClass.h to your project. I recommend to use "Link to file" instead of "Copy to
project"
In your bridging-header file, you add #import "ObjcClass.h" to import your ObjcClass header to your swift project
You may eventually have to specify Headers location in "Build Setting"
We started a new OS X project based on Swift, only to find out that the management doesn't really like that just through the use of Swift, we cannot sell the application to users before 10.9, even if 30% of the market still has older OS X versions.
Reimplementing the software in Objective-C seems to be the only solution. As Apple advertised Swift to be binary-compatible with Obj-C, I'm wondering if a translation of Swift source code to Obj-C source code is theoretically possible, and if so, if you know about any implementation of that idea.
Couldn't find anything about this topic on the web so far, everyone seems to be searching for the other direction at the moment.
Of course it's possible. But I am afraid there's no tools to do that. You should rewrite all the codes manually.
As you mentioned, Swift and Objective-C codes can work together perfectly. So I think rewrite the codes one class/file by one should be a good way.
Apple's document Using Swift with Cocoa and Objective-C https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/ should be help.
I am trying to port a Windows FileMaker plugin to OS X and am thinking that I should be doing this in Cocoa not Carbon,
but I am struggling to get anything to compile as soon as I include Cocoa.h
Are you importing it into a .c file? Cocoa requires Objective-C, so you'll need to rename any files you want to import its header into from .c to .m.
Of course, how practical that is depends on FileMaker's plug-in API on the Mac (assuming it has one). If the API, including UI hooks, is entirely Carbon-based, switching the plug-in's source to Cocoa may be futile. I can't say for sure without knowing that API.
Actually the FileMaker Framework is C++. You can mix C++ and Objective-C code so you could use Cocoa instead of Carbon. Beware that the file would be .mm not .m
An Objective-C wrapper to the C++ framework would make it possible to use .m Objective-C. Unfortunately I'm totally ignorant of C++ (and expect to stay) so it is beyond my competence.