Swift namespace in objective-c project - objective-c

There is a problem with my old source code. I'm going to add new swift files to the project done with objective-c. Everything works fine except compiler asks to add Swift namespace to all types like String, Array, Void etc in swift files.
So I have to write
let foo: Swift.String
instead of simply String.
Not a big deal but why?

Related

Writing tests for swift classes with XCTest written in Objective c

I'm facing a problem trying to write a test for class written in Objective-c and gets injected with a class written in Swift.
Example in the Test file:
SomeSwiftClass *swiftVar = [SomeSwiftClass new];
SomeObjectiveCClass *objVar = [[SomeObjectiveCClass alloc] initWithSwiftClass:swiftVar]
But the complier doesn't recognize the Swift class, and it doesnt support importing the "Target-Swift.h" too..
How can I write a test for both Objective c and Swift at the same TestFile ?
There are quite a few things that need to be in place if you want to use a Swift class in Objective-C.
The bridging header is required "Target-Bridging-Header.h"
Precede the class definition with #objc in the .swift file.
If you're still stuck, try deleting your .swift files and adding them to the project again, and Xcode will ask to generate the bridging header for you.
Check out the documentation: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-ID138
Or this link:
Swift to Objective-C header not created in Xcode 6
I managed to get it working, so if you post more of your code or share what you're doing I might be able to help.

Can Swift code embed in existing Objective-C file - or must one add that code to a discrete Swift file?

I have an Objective-c file which used to work with the older XCode's
with the emergence of XCode 6.x - code no longer performs
I found the same functionality in code written in Swift online
I read Apple's documentation about using bridging header's to allow the use of Swift with Objective-C. I was able to get that setup ok, I do believe.
What is not clear is - can I simply cut and paste the Swift code into my Objective-C file (a viewcontroller.me file actually), or am I limited to having to use a Swift file from the start ?
If I have to use a new file in my (very simple) app, Im not quite sure how to pull off having two viewcontroller.m files do the work where one used to work fine, so this method obviously creates a new problem for me.
Thanks for your assistance in advance !
I would recommend just creating a class or struct for your swift code, and access that class or struct in your Objective-C ViewController.

Xcode pluginDidLoad not getting called when adding objective-c file in swift plugin

I'm working on a plugin for Xcode. It is supposed to be written in Swift.
When I start with a fresh plugin project (I'm using this Xcode Plugin template which is also available via Alcatraz) the project compiles and runs fine.
The pluginDidLoad method is getting called right after Xcode starts. As soon as I add any Objective-C file (and a bridging header of course) the pluginDidLoad method is not getting called anymore.
The Objective-C file might be as simple as an empty class that is a subclass of NSObject.
Removing the target-memberbership (for the plugin-target) from the newly created Objective-C (.m) file the aforementioned mentioned method is getting called again.
Has anyone developed a Xcode plugin in Swift that also uses Objective-C files before and got this working?
Update
It seems that my original solution only works with Swift only projects because Xcode always takes the objective c class if you have one.
So here is another trick: Extend the NSObject class by the function class func pluginDidLoad(bundle: NSBundle) {} and initialize your plugin there. Then it doesn't matter on which class it is called. You might have to check that also all Swift classes subclass NSObject. I pushed it to my repository that you can have a look
Original Post
I think I could reproduce the problem now. To simplify the problem, let's say that we have only two swift classes PluginMain and PluginHelper.
As you said, sometimes the plugin isn't getting called for some mysterious reason. I was struggling with the problem again and I was wondering how Xcode knows which class is the main class. So I came up with the idea to put the following initializer in both classes PluginMain and PluginHelper
class func pluginDidLoad(bundle: NSBundle) {
let appName = NSBundle.mainBundle().infoDictionary?["CFBundleName"] as? NSString
if appName == "Xcode" {
//sharedPlugin = SwiftySafe(bundle: bundle)
//initialize your shared plugin
}
}
By putting a breakpoint or log message in pluginDidLoad in both classes, I notices that Xcode isn't ignoring the plugin, it is just loading the wrong class (e.g. PluginHelper instead of PluginMain).
The Solution
It turns out that Xcode uses the class that is compiled first as the main class and calls pluginDidLoad only on that. So you can change that by reordering the "Compiled Sources" under your target settings->Build Phases. Move your main class so that it is on top. In the following image you find an example from my project. SwiftySafe is my main class.
My example
You will find my project here https://github.com/creinders/SwiftySafe if you want to compare the settings.
The pluginDidLoad method is called on the principal class. When the principal class is a Swift class, you have to include the module name in the NSPrincipalClass Info.plist key.
So if your target name is MyPlugin and your principal class is MyClass, set NSPrincipalClass to MyPlugin.MyClass.
Also make sure that MyClass inherits from NSObject.
Sometimes you need to tell Xcode to reload bundle. Run this and restart Xcode
defaults delete com.apple.dt.Xcode DVTPlugInManagerNonApplePlugIns-Xcode-7.3

Xcode 6 crashing when using Objective-C subproject inside Swift

I have a fairly large Obj-C library that's a subproject in Xcode in several Obj-C projects and it works well.
I'm trying to use that same library in a Swift project but it's crashing Xcode when it gets to a variable declaration of one of the types from my library.
The headers seem to be working because auto-complete sees the proper types from my library. I've added my library (libMylib.a) in Link Binary with Libraries just like in my other Obj-C projects.
class thing {
var width: CGFloat
// more variables
var foo: MyClass // this is a class contained in my subproject
// functions, etc..
}
It crashes Xcode if the var foo line is there, not even when I create or access it. If it's just there. That's why I think it has something to do with not linking the subproject properly or similar.
Not sure what I'm missing.
Thanks.

Adding a C++ Header to Objective-C project

I've got a strange error working with Photoshop connection API in OSx.
I need to include the header of a cpp file to my project... I start from the adobe example and I included the code in this way:
#include "PSCryptor.h"
which contains the PSCryptor class :
class PSCryptor
{
public:
...
As soon as i try to use PSCrypor object, like with this code
static PSCryptor *sPSCryptor = NULL;
I get this error:
Unknown type name 'class'; did you mean 'Class'?
Could you help me to understand which is my error?
The file is being included in Objective-C files — that is, they have the extension ".m" or they are specifically configured to be compiled as Objective-C (probably the former). Thus, the compiler tries to interpret the code as Objective-C, but C++ is not valid Objective-C, so it complains.
What you need to do is use Objective-C++ instead. Simple fix: change the extension of the files that use that header from ".m" to ".mm".